一.什么是事务
事务的原则是:处于事务中的sql代码块会保持一致状态,即要么都能改变数据库,要么都不能改变数据库
ACID原则:
- 原子性
- 一致性
- 隔离性
- 多个业务可能操作同一个业务,防止数据损坏
- 持久性
- 事务一旦提交,无论体系发生什么问题,结果都不会受影响,将被持久化到存储器
二.spring中的事务管理
- 声明式事务:AOP(不会改动源代码,只需要再spring容器中注入)
- 编程式事务:需要在代码中,完成事务处理
测试环境:
接口:
package top.lostyou.dao; import org.apache.ibatis.annotations.Param; import top.lostyou.pojo.User; import java.util.List; import java.util.Map; public interface UserMapper { List<User> selectUser(); int insertUser(User user); int deleteUser(@Param("id") int id); }
sql语句:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="top.lostyou.dao.UserMapper"> <select id="selectUser" resultType="user"> SELECT * FROM mybatis.user </select> <insert id="insertUser" parameterType="user"> INSERT INTO mybatis.user(id, name, pwd) VALUES (#{id},#{name},#{pwd}) </insert> <delete id="deleteUser" parameterType="int"> DELETE FROM mybatis.user WHERE id=#{id} </delete> </mapper>
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:tx="http://www.springframework.org/schema/tx" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="dateSources" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&uesUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dateSources"/> <property name="configLocation" value="classpath:mybatis.xml"/> <property name="mapperLocations" value="classpath:top/lostyou/dao/*.xml"/> </bean> <!--配置声明式事务:也可以叫开启事务--> <bean id="transactionManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dateSources"/> </bean> <!--结合AOP实现事务植入--> <!--配置事务通知--> <tx:advice id="tx" transaction-manager="transactionManger"> <!--配置给那些方法实现事务--> <tx:attributes> <tx:method name="insertUser"/> <tx:method name="deleteUser"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!--配置事务切入--> <aop:config> <aop:pointcut id="poincut" expression="execution(* top.lostyou.dao.*.*(..))"/> <aop:advisor advice-ref="tx" pointcut-ref="poincut"/> </aop:config> </beans>
实现类:
package top.lostyou.dao; import org.mybatis.spring.support.SqlSessionDaoSupport; import top.lostyou.pojo.User; import java.util.List; import java.util.Map; public class UserImp extends SqlSessionDaoSupport implements UserMapper { public List<User> selectUser() { User user = new User(6, "mmmmm", "321654"); int id = 5; insertUser(user); deleteUser(id); return getSqlSession().getMapper(UserMapper.class).selectUser(); } public int insertUser(User user) { return getSqlSession().getMapper(UserMapper.class).insertUser(user); } public int deleteUser(int id) { return getSqlSession().getMapper(UserMapper.class).deleteUser(id); } }
测试类:
import org.springframework.context.support.ClassPathXmlApplicationContext; import top.lostyou.dao.UserMapper; import top.lostyou.pojo.User; import java.util.List; public class myTest { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("User.xml"); UserMapper userImp = context.getBean("userImp", UserMapper.class); List<User> users = userImp.selectUser(); for (User user : users) { System.out.println(user); } } }
标签:事务,spring,UserMapper,User,public,user,import,id,申明 From: https://www.cnblogs.com/5ran2yl/p/17146131.html