Spring与Mybatis的整合
一、 回顾MyBatis常规操作流程
//mybatis常规操作流程
public void mybatis_basic() throws Exception {
// 获取sqlSession工厂
// 注意,此处没有mybatis-config.xml
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 获取sqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 获取接口的实现类对象,然后执行方法完成功能
AccountDao mapper = sqlSession.getMapper(AccountDao.class);
mapper.update(1, 1300);
}
观察之前操作mybatis
代码可以发现,mybatis
操作数据库过程中,最关键的点是获取sqlSessionFactory
对象,而想要获取到该对象,必须将mybatis-config.xml
配置文件整合进来,所以我们使用spring
整合mybatis
的关键点就是: 读取mybatis-config.xml
配置文件,然后就可以得到一个工厂对象。有了工厂对象后就可以获取session
,再进一步获取mapper
,完成功能。
其中可以使用mybatis-config.xml
文件,也可以不使用,spring
可以直接读取mybatis-config.xml
文件,也可以直接在spring
配置好mybatis
内容,从而省去mybatis-config.xml
。
二、 Spring整合MyBatis
常规的mybatis
开发中,一般需要四种文件:
mybatis-config.xml
整体配置文件XxxMapper.xml
映射文件XxxDao.java
映射接口- 测试文件
现在我们要借助Spring
完成一个完整mybatis
功能,相关配置如下:
映射文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.au.db.AccountDao">
<update id="update">
update t_account set balance = #{1} where id = #{0}
</update>
</mapper>
映射接口:
// 映射接口
public interface AccountDao {
//更新余额
void update(int accountId, double balance);
}
oracle.properties
内容:
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:XE
user=system
password=123456789
Spring
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">
<!-- 1. 读取src下数据库配置文件oracle.properties,然后下面可以用${key}去引用文件中的value值了 -->
<context:property-placeholder location="classpath:oracle.properties" />
<!-- 2.dbcp数据源 -->
<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>${driver}</value>
</property>
<property name="url">
<value>${url}</value>
</property>
<property name="username">
<value>${user}</value>
</property>
<property name="password">
<value>${password}</value>
</property>
</bean>
<!-- 3.配置sqlSessionFactory -->
<!-- 3.1不使用mybatis-config.xml配置文件 -->
<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.au.db"></property>
<property name="configurationProperties">
<props>
<prop key="cacheEnabled">true</prop>
</props>
</property>
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations"
value="classpath:com/au/db/mybatis/AccountMapper.xml" />
</bean>
<!-- 3.2 使用mybatis-config.xml-->
<!-- 直接读取mybatis-config.xml文件,里面和之前配置的一样,就是mybatis-config.xml中不用配置数据源 -->
<!--
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
-->
<!-- 4.自动扫描映射接口所在的包以及这个包下面子包,找到映射接口,将来可以通过接口的名字首字母小写作为beanName,从spring容器中拿出自动生成的该接口的实现类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.au.db" />
</bean>
<!-- 最关键就是第3和4步骤,第3步用来配置mybatis文件,第4步用来处理接口与映射文件的映射,然后直接getBean(接口名首字母小写) -->
</beans>
测试代码:
@Test
public void mybatis_update(){
try {
String path = "com/au/db/mybatis/spring_mybatis.xml";
ClassPathXmlApplicationContext container = new ClassPathXmlApplicationContext(path);
//注意,获取的是 接口的实现类对象,获取后可以通过该对象调用里面的方法
AccountDao dao = (AccountDao) container.getBean("accountDao");
dao.update(1, 1260);
System.out.println(dao.getClass());
container.destroy();
}catch(Exception e) {
e.printStackTrace();
}
}