0

0

MyBatis入门(六)---mybatis与spring的整合

黄舟

黄舟

发布时间:2016-12-21 14:35:51

|

1428人浏览过

|

来源于php中文网

原创

一、整合需要

1.1、方法

上一章中的数据

需要sPRing通过单例方式管理SqlsessionFactory

spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession

(spring和mybatis整合自动完成)

持久层的mapper都需要由spring进行管理

二、创建项目整合环境

2.1、创建项目

46.png

2.2、数据

db.properties

#数据库配置信息
#驱动
driverClass=com.MySQL.jdbc.Driver
#连接url
jdbcUrl=jdbc:mysql://localhost:3306/mybatis?character=utf8#用户名
user=root
#密码
passWord=root
#连接池中保留的最小连接数
minPoolSize=10#连接池中保留的最大连接数。Default: 15 maxPoolSize=20#最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 maxIdletime=1800#当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3acquireIncrement=3#连接池中初始化连接数 应在minPoolSize与maxPoolSize之间取值。默认为3
initialPoolSize=15

 

元典智库
元典智库

元典智库:智能开放的法律搜索引擎

下载

2.3、confinguration

br/>  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-config.dtd">
   
   
   
   
   
   
   
   
   

   

2.4 POJO类与接口

 

package com.pb.ssm.po;import java.util.Date;/**
*

* @ClassName: Author

* @Description: TODO(作者)

* @author 刘楠

* @date 2015-10-31 下午12:39:33

* */public class Author {    //作者id
   private Integer authorId;    //作者姓名
   private String authorUserName;    //作者密码
   private String authorPassword;    //作者邮箱
   private String authorEmail;    //作者介绍
   private String authroBio;    //注册时间
   private Date registerTime;    
   
   
   public Integer getAuthorId() {        return authorId;
   }    public void setAuthorId(Integer authorId) {        this.authorId = authorId;
   }    public String getAuthorUserName() {        return authorUserName;
   }    public void setAuthorUserName(String authorUserName) {        this.authorUserName = authorUserName;
   }    public String getAuthorPassword() {        return authorPassword;
   }    public void setAuthorPassword(String authorPassword) {        this.authorPassword = authorPassword;
   }    public String getAuthorEmail() {        return authorEmail;
   }    public void setAuthorEmail(String authorEmail) {        this.authorEmail = authorEmail;
   }    public String getAuthroBio() {        return authroBio;
   }    public void setAuthroBio(String authroBio) {        this.authroBio = authroBio;
   }    public Date getRegisterTime() {        return registerTime;
   }    public void setRegisterTime(Date registerTime) {        this.registerTime = registerTime;
   }
   @Override    public String toString() {        return "Author [authorId=" + authorId + ", authorUserName="
               + authorUserName + ", authorPassword=" + authorPassword                + ", authorEmail=" + authorEmail + ", authroBio=" + authroBio                + ", registerTime=" + registerTime + "]";
   }
   
   
}

 

接口

 

package com.pb.ssm.mapper;import com.pb.ssm.po.Author;public interface AuthorMapper {    /**
    *
   
   * @Title: findAuthorById
   
   * @Description: TODO(根据id查找)
   
   * @param @param id
   * @param @return    设定文件
   
   * @return Author    返回类型
   
   * @throws
    */
   public Author findAuthorById(int id);    
   /**
    *
   
   * @Title: addAuthor
   
   * @Description: TODO(添加)
   
   * @param @param author
   * @param @return    设定文件
   
   * @return int    返回类型
   
   * @throws
    */
   public int addAuthor(Author author);    /**
    *
   
   * @Title: updateAuthor
   
   * @Description: TODO(更新)
   
   * @param @param author
   * @param @return    设定文件
   
   * @return int    返回类型
   
   * @throws
    */
   public int updateAuthor(Author author);    
   /**
    * 删除
   
   * @Title: delteAuthor
   
   * @Description: TODO(根据ID删除)
   
   * @param @param id
   * @param @return    设定文件
   
   * @return int    返回类型
   
   * @throws
    */
   public int delteAuthor(int id);
}

 

mapper.xml

 

 

br/>  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">INSERT INTO author(author_username,author_password,author_email,author_bio)
VALUES(#{authorUserName},#{authorPassword},#{authorEmail},#{authroBio})
update authorauthor_username=#{authorUserName},author_password=#{authorPassword},author_email=#{authorEmail},author_bio=#{authroBio},register_time=#{registerTime}where author_id=#{authorId}delete from author where author_id=#{authorId}

 

三、使用Mybatis配置文件.xml整合

3.1、写applicationContext.xml



 

3.2、测试

 

package com.pb.ssm.mapper;import java.io.InputStream;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Before;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.pb.ssm.po.Author;public class AuthorMapperTest {    private ApplicationContext applicationContext;

   @Before    public void setUp() throws Exception {
       applicationContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");
   }

   @Test    public void testFindAuthorById() {
       

       AuthorMapper authorMapper = (AuthorMapper) applicationContext.getBean("authorMapper");
       Author author = authorMapper.findAuthorById(2);
       System.out.println(author);
   
   }

   @Test    public void testAddAuthor() {        // 获取会话工厂
       AuthorMapper authorMapper = (AuthorMapper) applicationContext.getBean("authorMapper");
       
       Author author=new Author();
       author.setAuthorUserName("程序猿");
       author.setAuthorPassword("QWERdlfdad");
       author.setAuthorEmail("QWER@QQ.com");        
       
       int  num = authorMapper.addAuthor(author);
   
       System.out.println("num="+num);
       System.out.println("添加后的ID:"+author.getAuthorId());
   }

   @Test    public void testUpdateAuthor() {        // 获取会话工厂
       AuthorMapper authorMapper = (AuthorMapper) applicationContext.getBean("authorMapper");
       Author author = authorMapper.findAuthorById(13);
       author.setAuthroBio("天天写代码");
       author.setAuthorUserName("码农");        int num=authorMapper.updateAuthor(author);
   
       System.out.println("num="+num);
       System.out.println(author);
   }

   @Test    public void testDeleteAuthor() {        // 获取会话工厂
       AuthorMapper authorMapper = (AuthorMapper) applicationContext.getBean("authorMapper");        int num= authorMapper.delteAuthor(13);
       
   }

}

 

 

 

四、不使用mybatis配置文件

4.1、写ApplicationContext.xml

 



 

更改Mapper.xml,因为不能使用别名,所以type要写POJO类的全路径

br/>  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">INSERT INTO author(author_username,author_password,author_email,author_bio)
VALUES(#{authorUserName},#{authorPassword},#{authorEmail},#{authroBio})
update authorauthor_username=#{authorUserName},author_password=#{authorPassword},author_email=#{authorEmail},author_bio=#{authroBio},register_time=#{registerTime}where author_id=#{authorId}delete from author where author_id=#{authorId}

 

测试类同上

 以上就是MyBatis入门(六)---mybatis与spring的整合的内容,更多相关内容请关注PHP中文网(www.php.cn)! 

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
2026赚钱平台入口大全
2026赚钱平台入口大全

2026年最新赚钱平台入口汇总,涵盖任务众包、内容创作、电商运营、技能变现等多类正规渠道,助你轻松开启副业增收之路。阅读专题下面的文章了解更多详细内容。

54

2026.01.31

高干文在线阅读网站大全
高干文在线阅读网站大全

汇集热门1v1高干文免费阅读资源,涵盖都市言情、京味大院、军旅高干等经典题材,情节紧凑、人物鲜明。阅读专题下面的文章了解更多详细内容。

40

2026.01.31

无需付费的漫画app大全
无需付费的漫画app大全

想找真正免费又无套路的漫画App?本合集精选多款永久免费、资源丰富、无广告干扰的优质漫画应用,涵盖国漫、日漫、韩漫及经典老番,满足各类阅读需求。阅读专题下面的文章了解更多详细内容。

50

2026.01.31

漫画免费在线观看地址大全
漫画免费在线观看地址大全

想找免费又资源丰富的漫画网站?本合集精选2025-2026年热门平台,涵盖国漫、日漫、韩漫等多类型作品,支持高清流畅阅读与离线缓存。阅读专题下面的文章了解更多详细内容。

12

2026.01.31

漫画防走失登陆入口大全
漫画防走失登陆入口大全

2026最新漫画防走失登录入口合集,汇总多个稳定可用网址,助你畅享高清无广告漫画阅读体验。阅读专题下面的文章了解更多详细内容。

13

2026.01.31

php多线程怎么实现
php多线程怎么实现

PHP本身不支持原生多线程,但可通过扩展如pthreads、Swoole或结合多进程、协程等方式实现并发处理。阅读专题下面的文章了解更多详细内容。

1

2026.01.31

php如何运行环境
php如何运行环境

本合集详细介绍PHP运行环境的搭建与配置方法,涵盖Windows、Linux及Mac系统下的安装步骤、常见问题及解决方案。阅读专题下面的文章了解更多详细内容。

0

2026.01.31

php环境变量如何设置
php环境变量如何设置

本合集详细讲解PHP环境变量的设置方法,涵盖Windows、Linux及常见服务器环境配置技巧,助你快速掌握环境变量的正确配置。阅读专题下面的文章了解更多详细内容。

0

2026.01.31

php图片如何上传
php图片如何上传

本合集涵盖PHP图片上传的核心方法、安全处理及常见问题解决方案,适合初学者与进阶开发者。阅读专题下面的文章了解更多详细内容。

2

2026.01.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Spring中文手册
Spring中文手册

共0课时 | 0人学习

马士兵spring视频教程
马士兵spring视频教程

共25课时 | 9.1万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号