0%

mybatis整合spring

mybatis整合spring

当前spring是最热门的框架之一,很多框架都需要和spring整合,将组件交给spring的IOC容器来管理,下面来带大家来简单地配置一下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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>

<!-- mybatis整合spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.3</version>
</dependency>

<!-- spring 相关依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.29.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.29.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.29.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.29.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.29.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.29.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.29.RELEASE</version>
</dependency>

mybatis配置文件

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
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

<settings>
<!-- 开启二级缓存 -->
<setting name="cacheEnabled" value="true"/>
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!-- 延迟加载的全局开关。当开启时,所有关联对象都会延迟加载。 特定关联关系中可通过设置 fetchType 属性来覆盖该项的开关状态。 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 开启时,任一方法的调用都会加载该对象的所有延迟加载属性。 否则,每个延迟加载属性会按需加载 -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>

<typeAliases>
<!-- typeAlias为某个类起别名 -->
<typeAlias type="com.zhanghe.study.mybatis.model.User" alias="User"/>

<!-- 为某个包下所有类批量起别名
默认值为类名首字母小写
-->
<package name="com.zhanghe.study.mybatis.model"/>
</typeAliases>

<!-- 数据库厂商标识 -->
<databaseIdProvider type="DB_VENDOR">
<!-- name为使用DatabaseMetaData#getDatabaseProductName()返回的厂商名称
value为所设置的别名,使用databaseId时使用别名即可 -->
<property name="MySQL" value="mysql"/>
<property name="Oracle" value="oracle"/>
</databaseIdProvider>

</configuration>

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
40
41
42
43
44
45
46
47
<?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"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
xmlns:tx="http://www.springframework.org/schema/tx"
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 http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.zhanghe.study.mybatis"/>
<!-- 引入数据库配置文件 -->
<context:property-placeholder location="jdbc.properties"/>

<!-- 数据源 -->
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>

<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"/>
</bean>



<!-- 创建sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<!-- mybatis全局配置文件的位置 也可以不使用mybatis全局配置 所有信息都可以在该bean中使用property来配置 -->
<property name="configLocation" value="mybatis-config1.xml"/>
<!-- mapper映射文件的位置 -->
<property name="mapperLocations" value="classpath*:mapper/**/*.xml"/>
</bean>
<!-- 亲测两种方式都可以 -->
<!-- 方式一:扫描所有的mapper接口,使得接口自动注入 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zhanghe.study.mybatis.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!-- 方式二:扫描所有的mapper接口,使得接口自动注入 -->
<!--
<mybatis:scan base-package="com.zhanghe.study.mybatis.mapper"/>
-->
<!-- 事务管理器 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

单测结果

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
40
41
42
43
44
45
46
/**
* mybatis、spring整合测试
* @author zh
* @date 2021/1/15 16:08
*/
public class MybatisSpringTest {

private ApplicationContext context;

private SqlSessionFactory sqlSessionFactory;

@Before
public void beanFactory(){
context = new ClassPathXmlApplicationContext("applicationContext.xml");
sqlSessionFactory = context.getBean(SqlSessionFactory.class);
}

@Test
public void testbeanAutoWired(){
UserMapper userMapper = context.getBean(UserMapper.class);
System.out.println(userMapper);
UserService userService = context.getBean(UserService.class);
userService.print();

}

@Test
public void testTwoLevelCache(){
SqlSession session = sqlSessionFactory.openSession();
// mybatis为接口创建代理对象
UserMapper userMapper = session.getMapper(UserMapper.class);
User user = userMapper.selectUser(8);
System.out.println(user);
userMapper.updateUser(user);
session.close();
SqlSession session1 = sqlSessionFactory.openSession();

UserMapper userMapper1 = session1.getMapper(UserMapper.class);
User user1 = userMapper1.selectUser(8);
System.out.println(user1);
System.out.println(user == user1);

session1.close();
}

}

参考地址http://mybatis.org/spring/zh/getting-started.html