Spring整合Mybatis

一、使用Mybatis基本步骤

  • 编写实体类
  • 编写核心配置文件
  • 编写接口
  • 编写Mapper.xml
  • 测试

二、整合Mybatis

导入依赖

  • mybatis
  • mysql数据库
  • spring相关的
  • aop织入
  • mybatis-spring
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.24</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.11</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>

2.1 方法一(推荐)

2.1.1 编写数据源配置

1
2
3
4
5
6
7
<!--DataSource:使用Spring数据源替换Mybatis数据源配置-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="nc20011019"/>
</bean>

2.1.2 sqlSessionFactory

1
2
3
4
5
6
7
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--绑定Mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/nichu/mapper/*.xml"/>
</bean>

2.1.3 sqlSessionTemplete

1
2
3
4
5
<!--sqlSessionTemplete:就是我们使用的sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能使用构造注入sqlSessionFactory,因为他没有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>

2.1.4 需要给接口加实现类

1
2
3
4
5
6
7
8
9
10
11
public class StudentMapperImpl implements StudentMapper{
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<Student> getStudentList() {
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
return mapper.getStudentList();
}
}

2.1.5 将自己写的实现类注入到Spring中

1
2
3
<bean id="studentMapper" class="com.nichu.mapper.StudentMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>

2.1.6 测试

1
2
3
4
5
6
7
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
StudentMapper studentMapper = context.getBean("studentMapper", StudentMapper.class);
for (Student student : studentMapper.getStudentList()) {
System.out.println(student);
}
}

2.2 方法二

2.2.1 配置数据源和sqlSessionFactory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!--DataSource:使用Spring数据源替换Mybatis数据源配置-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="nc20011019"/>
</bean>

<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--绑定Mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/nichu/mapper/*.xml"/>
</bean>

2.2.2 编写接口实现类

1
2
3
4
5
6
7
//继承SqlSessionDaoSupport类可以直接getSqlSession()
public class StudentMapperImpl2 extends SqlSessionDaoSupport implements StudentMapper{
@Override
public List<Student> getStudentList() {
return getSqlSession().getMapper(StudentMapper.class).getStudentList();
}
}

2.2.3 Spring中注入sqlSessionFactory

1
2
3
<bean id="studentMapper2" class="com.nichu.mapper.StudentMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
-------------本文结束感谢您的阅读-------------