Spring结合Mybatis和声明式事务
Mybatis-Spring
📖文档地址:mybatis-spring s
🍎 Spring引入Mybatis第一种方式
public class StudentMapperImpl implements StudentMapper {
//我们的所有操作都通过sqlSessionTemplate来执行
private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Override
public List<Student> query() {
return sqlSessionTemplate.getMapper(StudentMapper.class).query();
}
}
<!--datesource-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/newhk?serverTimezone=GMT"/>
<property name="username" value="root"/>
<property name="password" value="990901cxp.."/>
</bean>
<!--bytis-spring-->
<!--SqlSessionFactory对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--绑定mybatis-->
<property name="configLocation" value="classpath:spring-mybatis.xml"/>
<!--映射mapper-->
<property name="mapperLocations" value="classpath:com/cxp/mapper/*.xml"/>
</bean>
<!--配置SqlSessionTemplate-->
<!--因为没set方法只能用构造方法注入-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<!--通过构造方法注入-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!--将studentMapper注入Spring容器中-->
<bean id="studentMapper" class="com.cxp.mapper.impl.StudentMapperImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean>
📖注意因为SqlSessionTemplate中没有set方法所以中能使用构造注入
🍎 Spring引入Mybatis第二种方式
public class StudentMapperImpl2 extends SqlSessionDaoSupport implements StudentMapper {
@Override
public List<Student> query() {
StudentMapper mapper = getSqlSession().getMapper(StudentMapper.class);
return mapper.query();
}
}
<!--datesource-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/newhk?serverTimezone=GMT"/>
<property name="username" value="root"/>
<property name="password" value="990901cxp.."/>
</bean>
<!--bytis-spring-->
<!--SqlSessionFactory对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--绑定mybatis-->
<property name="configLocation" value="classpath:spring-mybatis.xml"/>
<!--映射mapper-->
<property name="mapperLocations" value="classpath:com/cxp/mapper/*.xml"/>
</bean>
<!--配置SqlSessionTemplate-->
<!--因为没set方法只能用构造方法注入-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<!--通过构造方法注入-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!--将studentMapper注入Spring容器中-->
<bean id="studentMapper2" class="com.cxp.mapper.impl.StudentMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
Spring-TX
📖 回顾事务
- 把一组业务当成一组业务来做:要么都成功,要么都失败!
- 事务在项目开发中,十分重要,涉及到数据的一致性,不能马虎
- 确保事务的完整性和一致性;
📖事务的ACID原则
-
原子性
-
隔离性
- 多个业务操作同一个资源,防止数据损坏
-
一致性
-
持久性
- 事务一旦提交,无论发生什么问题,结果都不会再改变。被持久化的写入到储存器中
📖 Sprinng事务管理
- 声明式事务:AOP
- 编程式事务:需要在代码中,进行事务管理
<!--=======================配置是声明式事务========================-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--通过构造器注入-->
<constructor-arg ref="dataSource"/>
<!--通过set注入-->
<!-- <property name="dataSource" ref="dataSource"/>-->
</bean>
<!--==============结合AOP实现事务织入========================-->
<!--配置事务通知-->
<tx:advice id="txDriver" transaction-manager="transactionManager">
<tx:attributes>
<!--给那些方法配置事务、
配置事务的传播特性:propagation (新特性)
-->
<tx:method name="query" propagation="REQUIRED"/>
<tx:method name="add" propagation="REQUIRED"/>
<tx:method name="delete" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--==============配置事务切入========================-->
<aop:config>
<!--切入点-->
<aop:pointcut id="txPointcut" expression="execution( * com.cxp.mapper.impl.*.*(..))"/>
<aop:advisor advice-ref="txDriver" pointcut-ref="txPointcut"/>
</aop:config>
🍊 思考为啥需要事务
- 如果不配事务,可能存在数据不一致的情况;

浙公网安备 33010602011771号