Spring整合MyBatis
基础的整合步骤(方法1)
- 导入jar包
- spring相关jar包 + mybatis相关jar包 + mysql相关jar包
编写配置文件
beans.xml(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" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.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://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="chenyu123"/> </bean> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!-- SqlSessionTemplate --> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> <bean id="userDao" class="com.eric.dao.UserDaoImpl"> <property name="sqlSession" ref="sqlSessionTemplate"/> </bean> </beans>
mybatis配置
<?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> <typeAliases> <package name="com.eric.vo"/> </typeAliases> <mappers> <mapper resource="com/eric/vo/user.mapper.xml" /> </mappers> </configuration>
实现
entity类
public class User { private int id; private String name; private String pwd; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
sql语句映射文件
<?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.eric.vo.UserMapper"> <select id="selectAll" resultType="User"> select * from user </select> </mapper>
dao层(省略了UserDao接口)
public class UserDaoImpl implements UserDao{ //mybatis-spring整合包中的类 //相当于SqlSession的封装 private SqlSessionTemplate sqlSession; public void setSqlSession(SqlSessionTemplate sqlSession) { this.sqlSession = sqlSession; } @Override public List<User> selectUser() { return sqlSession.selectList("com.eric.vo.UserMapper.selectAll"); } }
测试
public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); UserDao userDao = (UserDao) context.getBean("userDao"); System.out.println(userDao.selectUser().size()); } }
- 导入jar包
整合方法2
dao类继承
SqlSessionDaoSupport
//spring整合mybatis方法2 public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{ @Override public List<User> selectUser() { return getSqlSession().selectList("com.eric.vo.UserMapper.selectAll"); } }
在配置文件中不需要管理
SqlSessionTemplate
<!-- 第二种方式,直接注入sqlSessionFactory --> <!-- 部分配置 --> <bean id="userDao" class="com.eric.dao.impl.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean>
整合方式3
- mybatis使用注解
- 不需要dao层的impl
- 可以直接配置service层
UserMapper映射类
public interface UserMapper { @Select("select * from user") public List<User> selectUser(); }
UserServiceImpl类
public class UserServiceImpl implements UserService{ UserMapper userMapper = null; public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } @Override public List<User> selectUser() { return userMapper.selectUser(); } }
spring配置文件
<!-- 部分配置文件 --> <!-- 第三种整合方式 --> <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.eric.dao.UserMapper"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> <!-- 不需要配置dao层 --> <!-- 直接配置service层 --> <bean id="userService" class="com.eric.service.impl.UserServiceImpl"> <property name="userMapper" ref="userMapper"></property> </bean>
整合方式4
- 不需要mybatis配置文件
在spring配置文件中,原本引用mybatis配置文件的地方改为
<!-- 部分配置文件 --> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:com/eric/vo/*.mapper.xml"/> </bean>
- 直接通过
<property>
引用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:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.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://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="chenyu123"/> </bean> <!-- 声明式事务配置 --> <!-- 配置事务管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置事务通知 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <!-- 配置那些方法使用什么样的事务 --> <!-- 配置事务的传播特性 --> <!-- REQUIRED表示支持当前事务,如果没有事务,就新建一个事务 --> <tx:method name="add" propagation="REQUIRED"/> <tx:method name="insert" propagation="REQUIRED"/> <tx:method name="update" propagation="REQUIRED"/> <tx:method name="delete" propagation="REQUIRED"/> <tx:method name="remove*" propagation="REQUIRED"/> <tx:method name="get" read-only="true"/> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 配置aop --> <aop:config> <!-- 一般情况下,pointcut是在service层,示例代码为了简便未包含service层 --> <aop:pointcut expression="execution(* com.eric.dao.impl.*.*(..))" id="pointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> </aop:config> <!-- 声明式事务配置结束 --> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!-- SqlSessionTemplate --> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> <bean id="userDao" class="com.eric.dao.impl.UserDaoImpl"> <property name="sqlSession" ref="sqlSessionTemplate"/> </bean> </beans>
- 当事务发生错误时,能够回滚不会影响数据库中的数据