十三、mybatis与spring的整合
直接上图:

看不清文件夹里面有;
整合步骤:
- 编写数据源,是因为没有数据源,但是我们可以使用spring的数据源类,来配置我们的连接信息;
- 获得我们的数据工厂
- 获得mybatis中的sqlsession,在spring中是sqlSessionTemplate
- 获得我们的mapper对象,可以使用一个注入,使用setter注入;
- 编写实体类,直接对数据结果进行一个返回;使用注入,将sqlsession注入我们的实体类,简单一点;
- 测试
==这一点可以把我们的spring的特点反映出来了,就是一切交给spring托管,我们不需要在写一些重复的实例化对象的语句,配置地狱就是这么来的==
applicationContext:
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"?> <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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://rm-wz917wbvou67a757quo.mysql.rds.aliyuncs.com:3306/mybatis?useUnicode=true&characterEncoding=utf8&useSSL=true"/> <property name="username" value="saxon"/> <property name="password" value="19990707"/> </bean> <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sessionFactory"> <property name="dataSource" ref="datasource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:com/saxon/Dao/UserDao.xml"/> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sessionFactory"/> </bean> <bean id="user" class="com.saxon.Dao.UserDaoImpl"> <property name="sqlSessionTemplate" ref="sqlSession"/> </bean> <context:annotation-config/> </beans>
|
mybatis-config.xml:
1 2 3 4 5 6 7
| <?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>
</configuration>
|
userDaomapper:
1 2 3 4 5 6 7 8 9
| <?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.saxon.Dao.UserDao"> <select id="getUser" resultType="com.saxon.pojo.User"> select * from saxon where id=#{id}; </select> </mapper>
|
其他方法:
继承一个类SqlSessionDaoSupport直接获得sqlsession;
1 2 3 4 5 6 7 8
| public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
@Override public User getUser (int id) {
return this.getSqlSession ().getMapper (UserDao.class).getUser (1); } }
|
注入:
由于SqlSessionDaoSupport需要一个参数就给他注入一个参数:
1 2 3
| <bean id="user" class="com.saxon.Dao.UserDaoImpl"> <property name="sqlSessionTemplate" ref="sqlSession"/> </bean>
|
不注入的话就使用默认自己创建一个,就会出错;
1
| This class needs a SqlSessionTemplate or a SqlSessionFactory. If both are set the SqlSessionFactory will be ignored.
|
十四、spring事务管理