MyBatis:与Spring集成配置

整理历史项目,Mybatis 配置文件及整合Spring,主要配置记录。只做参考,实际开发已不使用此配置。

sqlSessionFactory

Mybatis 最原始的使用,在 Mapper 实现类中注入sqlSessionFactory,使用sqlSessionFactory执行CRUD操作。

sqlMapConfig.xml

使用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
<?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>
<!-- 配置化环境:数据源,事务,可配置多个环境 -->
<environments default="development">
<environment id="development">
<!-- 使用jdbc管理事务,事务控制是由mybatis来执行 -->
<transactionManager type="JDBC" />
<!-- 配置数据库,也是由mybatis来管理 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://192.168.0.20:3306/mybatis?characterEncoding=utf-8" />
<property name="username" value="java" />
<property name="password" value="123" />
</dataSource>
</environment>
</environments>
<!-- 引入映射文件,可配置多个 -->
<mappers>
<!-- 配置每一个mapper.xml文件 -->
<!-- <mapper resource="mapper/User.xml" /> -->

<!-- 批量扫描包下的mapper.xml文件 -->
<package name="com.mybatis.dao" />
</mappers>

</configuration>

applicationContext.xml

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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 第一:数据源 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>

<!-- 第二:sqlSessionFactory工厂管理 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 加载Mybatis核心配置文件 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
<!-- 定义别名 -->
<property name="typeAliasesPackage" value="com.mybatis.domain"></property>
</bean>

<!-- 第三:批量扫描接口包路径:使用接口代理方式开发,交由spring管理-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mybatis.dao"></property>
</bean>
</beans>
作者

光星

发布于

2018-04-30

更新于

2022-06-17

许可协议

评论