Spring整合MyBatis

Posted 张坤

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring整合MyBatis相关的知识,希望对你有一定的参考价值。

1)导入依赖
mybatis-spring mybatis的Spring兼容包,帮助Spring整合MyBatis
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
c3p0 连接池
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
spring-test Spring整合JUnit测试
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
spring-context Spring包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
spring-jdbc 数据源需要依赖此包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
junit必须4.12以上
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
2)Spring配置MyBatis
1、扫描包
2、配置数据源DataSource 设置为C3P0连接池数据源
3、配置工厂SqlSessionFactory
4、配置扫描器扫描Mapper接口,帮助创建接口代理对象
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描组件-->
<context:component-scan base-package="com.mx.mybatis"/>
<!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--配置数据源属性-->
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/database?serverTimezone=CST&amp;useSSL=false"/>
<property name="user" value="username"/>
<property name="password" value="password"/>
<property name="minPoolSize" value="10"/>
<property name="maxPoolSize" value="100"/>
<property name="initialPoolSize" value="10"/>
</bean>
<!--配置工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置数据源属性-->
<property name="dataSource" ref="dataSource"/>
<!--配置实体类包-->
<property name="typeAliasesPackage" value="com.mx.mybatis.entity"/>
<!--配置Mapper文件的位置 *通配符代表任意名称-->
<property name="mapperLocations" value="mapper/*Mapper.xml"/>
</bean>
<!--配置Mapper接口的扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--配置Mybatis工厂的名称 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--配置Mapper接口所在的包-->
<property name="basePackage" value="com.mx.mybatis.dao"/>
</bean>
</beans>
3)Spring整合JUnit测试
//指定JUnit版本
@RunWith(SpringJUnit4ClassRunner.class)
//引入Spring的配置文件
@ContextConfiguration(locations = "classpath:spring-mybatis.xml")
public class MyBatisSpringTest {
 
@Autowired
private UserMapper userMapper;
 
@Test
public void testUserMapper(){
List<User> users = userMapper.selectAll();
users.forEach((user)->System.out.println(user));
}
}
 
 
 
 
 

以上是关于Spring整合MyBatis的主要内容,如果未能解决你的问题,请参考以下文章

请教mybatis+spring+atomikos的整合问题

7.Spring:整合Mybatis

基于Spring+SpringMVC+MyBatis开发书评网Spring和MyBatis的整合

Spring-整合Mybatis

最新最全面的Spring详解——Spring-Mybatis整合

Spring Boot:Spring Boot整合Mybatis案例