最新Spring整合MyBatis详解教程

目录
1、导入相关jar包

  1. junit
  2. mybatis
  3. mysql
  4. spring相关
  5. aop织入
  6. mybatis-spring
  7. lombok(选用)
    2、回顾:建立一个Mybatis程序
  8. IDEA连接数据库
  9. 编写MyBatis核心配置文件
  10. 创建数据库表对应实体类
  11. 编写Mapper接口
  12. 编写Mapper.xml配置文件
  13. 编写测试类
  14. 给Mapper.xml添加注册
  15. 测试运行
    3、spring整合:方式一
  16. 引入spring配置文件
  17. 使用Spring的数据源替换MyBatis的数据源配置
  18. 通过spring创建sqlSessionFactory
  19. 创建sqlSession对象
  20. 新建接口实现类
  21. 创建实现类对象
  22. 修改测试类
  23. 运行测试
    4、spring整合:方式二
  24. 新建接口实现类
  25. 创建接口实现类对象
  26. 修改测试类
  27. 测试运行
    image-20200811201331833
    mybatis-spring官方文档:http://mybatis.org/spring/zh/index.html

首先新建一个空的maven项目

1、导入相关jar包

  1. junit

    junit
    junit
    4.13
    test

    1
    2
    3
    4
    5
    6
  2. mybatis

    org.mybatis
    mybatis
    3.5.5

    1
    2
    3
    4
    5
  3. mysql

    mysql
    mysql-connector-java
    8.0.21

    1
    2
    3
    4
    5
  4. spring相关

    org.springframework
    spring-webmvc
    5.2.8.RELEASE
org.springframework spring-jdbc 5.1.3.RELEASE 1 2 3 4 5 6 7 8 9 10 11 5. aop织入 org.aspectj aspectjweaver 1.9.4 1 2 3 4 5 6. mybatis-spring org.mybatis mybatis-spring 2.0.5 1 2 3 4 5 7. lombok(选用) org.projectlombok lombok 1.18.12 1 2 3 4 5 最后为了防止maven配置文件无法被导出或生效,加入以下代码 src/main/resources **/*.properties **/*.xml true src/main/java **/*.properties **/*.xml true 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

2、回顾:建立一个Mybatis程序
更详细过程可以看我之前的博客:第一个MyBatis程序

  1. IDEA连接数据库
    大家连接自己的数据库即可,这里的实验表格为:
    image-20200811181559385

  2. 编写MyBatis核心配置文件
    image-20200811181900880

<?xml version="1.0" encoding="UTF-8" ?> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
  1. 创建数据库表对应实体类
    image-20200811181927962
    package pojo;

import lombok.Data;

@Data
public class User {
private int id;
private String name;
private String pwd;
}
1
2
3
4
5
6
7
8
9
10

  1. 编写Mapper接口
    image-20200811182020272
    package mapper;

import pojo.User;

import java.util.List;

public interface UserMapper {
public List getUser();
}
1
2
3
4
5
6
7
8
9

  1. 编写Mapper.xml配置文件
    image-20200811182204453
<?xml version="1.0" encoding="UTF-8" ?> select * from mybatis.user 1 2 3 4 5 6 7 8 9
  1. 编写测试类
    image-20200811182242082
    public class Test {
    @org.junit.Test
    public void test() throws IOException {
    String resource = “mybatis-config.xml”;
    InputStream in = Resources.getResourceAsStream(resource);
    SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
    SqlSession sqlSession = sessionFactory.openSession(true);
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    List users = mapper.getUser();
    for (User user : users) {
    System.out.println(user);
    }
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14

  2. 给Mapper.xml添加注册
    在核心配置文件mybatis-config.xml中加入以下代码

1 2 3
  1. 测试运行
    image-20200811194703348
    到此,mybatis程序的创建已成功,接下来将进行修改,整合spring

3、spring整合:方式一
通过SqlSessionTemplate创建SqlSession

接下来将在上述实验环境下进行修改

  1. 引入spring配置文件
    在resource目录下新建spring-dao.xml
    image-20200811183008850
<?xml version="1.0" encoding="UTF-8"?>

1 2 3 4 5 6 7 8
  1. 使用Spring的数据源替换MyBatis的数据源配置
    在spring配置文件中加入以下代码,配置数据源信息
1 2 3 4 5 6 7 8 此时,就可以删除mybatis核心配置文件mybatis-config.xml中的数据源配置 image-20200811184444369
  1. 通过spring创建sqlSessionFactory
    在 MyBatis 中,是通过 SqlSessionFactoryBuilder 来创建 SqlSessionFactory
    而在 MyBatis-Spring 中,则使用 SqlSessionFactoryBean 来创建
    在spring配置文件中加入以下代码,创建sqlSessionFactory
1 2 3 4 唯一的必要属性:用于 JDBC 的 DataSource,注入上述创建好的数据源

还可以配置其他属性,完全取代mybatis-config.xml中的配置
image-20200811185806672
这里加入两个属性配置:

configLocation:指定mybatis的xml配置文件路径
mapperLocations:注册Mapper.xm映射器

1 2 3 4 5 6 7 8 这里,我们在spring配置文件中注册了Mapper.xml,就可以删除mybatis-config.xml中的Mapper.xml的注册 image-20200811185526572
  1. 创建sqlSession对象
    在mybatis中,SqlSession 提供了在数据库执行 SQL 命令所需的所有方法

而在spring-mybatis,没有SqlSession了,而是SqlSessionTemplate,这两个是同一个东西

在spring配置文件中加入以下代码,创建sqlSession

这里使用上述创建好的sqlSessionFactory 作为构造方法的参数来创建 SqlSessionTemplate 对象。

1 2 3 4 5
  1. 新建接口实现类
    上一步我们创建了sqlSession对象,此时需要创建一个类来使用sqlSession

image-20200811191543680
在该类中添加一个 SqlSession 属性,并添加 set方法 用于后续sqlSession的注入

public class UserMapperImpl implements UserMapper {
//原来所有操作都通过SqlSession来执行,现在都是SqlSessionTemplate
private SqlSessionTemplate sqlSession;

public void setSqlSession(SqlSessionTemplate sqlSession) {
    this.sqlSession = sqlSession;
}

public List<User> getUser() {
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    return mapper.getUser();
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13

  1. 创建实现类对象
    利用spring创建上述接口实现类对象,取名为userMapper,并注入上述创建好的sqlSession对象
1 2 3
  1. 修改测试类
    此时,无需像先前一样创建sqlSessionFactory和sqlSession,我们只需获得创建好的实体类对象UserMapper,然后调用该对象的方法即可

public class Test {
@org.junit.Test
public void test() throws IOException {
ApplicationContext context = new ClassPathXmlApplicationContext(“spring-dao.xml”);
UserMapperImpl userMapper = (UserMapperImpl) context.getBean(“userMapper”);
List users = userMapper.getUser();
for (User user : users) {
System.out.println(user);
}
}
}
1
2
3
4
5
6
7
8
9
10
11

  1. 运行测试
    image-20200811194703348
    运行成功!!!

4、spring整合:方式二
通过SqlSessionDaoSupport获得Sqlsession

接下来将在上述实验环境下进行修改

  1. 新建接口实现类
    新建一个接口实现类,继承SqlSessionDaoSupport类,直接可以获得SqlSession对象
    image-20200811205114097

public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {
public List getUser() {
SqlSession sqlSession = getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.getUser();
}
}
1
2
3
4
5
6
7

  1. 创建接口实现类对象
    通过Spring来创建

在spring配置文件中创建上述实体类对象userMapper2,并设置sqlSessionFactory属性为上述创建好的sqlSessionFactory

1 2 3
  1. 修改测试类
    同样,无需像先前一样创建sqlSessionFactory和sqlSession,我们只需获得创建好的实体类对象UserMapper2,然后调用该对象的方法即可

public class Test {
@org.junit.Test
public void test() throws IOException {
ApplicationContext context = new ClassPathXmlApplicationContext(“spring-dao.xml”);
UserMapperImpl2 userMapper = (UserMapperImpl2) context.getBean(“userMapper2”);
List users = userMapper.getUser();
for (User user : users) {
System.out.println(user);
}
}
}
1
2
3
4
5
6
7
8
9
10
11

  1. 测试运行
    image-20200811210202877
    成功运行!!

THE END…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值