方式一 手动配置获取SqlSession
导入依赖
给Mapper接口添加实现类
就是将sqlSession的使用流程编写出来,但用SqlSessionTemplate替代,这个template就是一个模板,用来替换作用。
public class UserMapperImpl implements UserMapper {
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<User> getUserList() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.getUserList();
}
}
编写Spring配置,实现各类注入
数据源配置即设置数据库连接所需数据,替代了mybatis的配置文件
目的肯定是仿mybatis获取最后的mapper对象,那么逐层向上需要SqlSession,再上需要SqlSessionFactory
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:com/lms/mapper/UserMapper.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<bean id="userMapper" class="com.lms.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
该方式就是基于原本mybatis操作,将sqlsession生成操作交给springIOC进行自动配置,在测试时只需要读取配置文件,获取mapper的实体bean即可。
测试
public static void main(String[] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring-dao.xml");
UserMapper userMapper = classPathXmlApplicationContext.getBean("userMapper", UserMapper.class);
for (User user : userMapper.getUserList()) {
System.out.println(user);
}
}
方式二 使用support类直接生成sqlSession
数据源及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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="541202"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:com/lms/mapper/UserMapper.xml"/>
</bean>
<bean id="userMapper2" class="com.lms.mapper.UserMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
Mapper实体类
不需要再使用template编写整个mybatis流程,直接用SqlSessionDaoSupport辅助获取sqlSession执行流程。
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {
@Override
public List<User> getUserList() {
return getSqlSession().getMapper(UserMapper.class).getUserList();
}
}
该方法省去了手动对SqlSession的配置与获取,借助support跳过配置直接获取sqlSession。
声明式事务
事务
一组业务当做一个业务处理:要么都成功,要么都失败
保证了完整性和数据一致性
事务ACID:
原子性Atomicity:事务操作要么都发生,要么都不发生
一致性Consistency:事务前后数据完整性保持一致,数据符合逻辑
隔离性Isolation:并发情况出现时,数据库为每个用户开启的事务不能被其他事务的数据影响
持久度Durability:事务一旦被提交,事务对数据库数据的改变是永久性的
Spring事务管理
声明式事务:AOP管理
编程式事务:代码管理
配置声明式事务
接口实现类:
public class UserMapperImpl implements UserMapper {
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<User> getUserList() {
addUser(new User(5, "2", "3"));
deleteUser(new User(1, "2", "3"));
return sqlSession.getMapper(UserMapper.class).getUserList();
}
@Override
public int addUser(User user) {
return sqlSession.getMapper(UserMapper.class).addUser(user);
}
@Override
public int deleteUser(User user) {
return sqlSession.getMapper(UserMapper.class).addUser(user);
}
}
该类就进行一连串事务的发生,其中delete的sql语句有错误,无法执行,在没有生命事务之前,插入仍会执行成功。
事务配置:
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd">
<!--正常数据库配置已省略-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add" propagation="REQUIRED"/>
<tx:method name="update" propagation="REQUIRED"/>
<tx:method name="delete" propagation="REQUIRED"/>
<tx:method name="query" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="txPointCut" expression="execution(* com.lms.mapper.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
采用AOP切入面拦截进行事务处理,此处配置完成后,delete执行失败,插入语句也不会生效。