Spring学习(四)声明式事务
1、事务概述
- 要么都执行,要么都不执行
- 事务管理是企业级应用程序开发中必不可少的技术, 用来确保数据的完整性和一致性.
- 事务四个属性ACID
- 原子性(atomicity):事务的原子性要求事务中的所有操作要么都执行,要么都不执行。
- 一致性(consistency):所有数据都处于满足业务规则的一致性状态。一致性原则要求:一个事务中不管涉及到多少个操作,都必须保证事务执行之前数据是正确的,事务执行之后数据仍然是正确的。一个事务中不管涉及到多少个操作,都必须保证事务执行之前数据是正确的,事务执行之后数据仍然是正确的
- 隔离性(isolation):隔离性原则要求多个事务在并发执行过程中不会互相干扰。
- 持久性(durability):持久性原则要求事务执行完成后,对数据的修改永久的保存下来,不会因各种系统错误或其他意外情况而受到影响
2、Spring事务管理
2.1、编程式事务管理
使用原生的JDBC API进行事务管理
- 获取数据库连接Connection对象
- 取消事务的自动提交
- 执行操作
- 正常完成操作时手动提交事务
- 执行失败时回滚事务
- 关闭相关资源
编程式事务管理需要将事务管理代码嵌入到业务方法中来控制事务的提交和回滚
缺点
- 必须在每个事务操作中包含额外的事务管理代码
- 代码冗余
2.2、声明式事务管理
- 大多数情况下声明式事务比编程式事务管理更好:它将事务管理代码从业务方法中分离出来,以声明的方式来实现事务管理。
- 事务管理代码的固定模式作为一种横切关注点,可以通过AOP方法模块化,进而借助Spring AOP框架实现声明式事务管理
DataSourceTransactionManager:在应用程序中只需要处理一个数据源,而且通过JDBC存取
2.3、使用Spring-Mybatis整合Mybatis
导入相关jar包
<dependencies>
<!--mysql-connector-java-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--Spring-Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<!--aop-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<!--JDBC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
pojo实体类
public class User {
private Integer id;
private String name;
private String password;
private String email;
private Date birthday;
public User() {
}
public User(Integer id, String name, String password, String email, Date birthday) {
this.id = id;
this.name = name;
this.password = password;
this.email = email;
this.birthday = birthday;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
", birthday=" + birthday +
'}';
}
}
userMapper.xml配置
<?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.song.mapper.UserMapper">
<select id="selectUser" resultType="User">
select * from users
</select>
</mapper>
mybatis-config.xml配置
<?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.song.pojo"/>
</typeAliases>
</configuration>
Spring中spring-mybatis的配置
<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
http://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/jdbc?useSSL=false&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="824008"/>
</bean>
<!--配置SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--关联Mybatis-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/song/mapper/UserMapper.xml"/>
</bean>
<!--注册sqlSessionTemplate , 关联sqlSessionFactory-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--利用构造器注入-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!--注册bean-->
<bean id="userMapperImpl" class="com.song.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
私有化sqlSessionTemplate
public class UserMapperImpl implements UserMapper {
//sqlSession不用我们自己创建了,Spring来管理
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
public List<User> selectUser() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.selectUser();
}
}
测试
扫描二维码关注公众号,回复:
11549756 查看本文章
@Test
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("mybatis-spring.xml");
UserMapper bean = (UserMapper) context.getBean("userMapperImpl");
List<User> userList = bean.selectUser();
for (User user : userList) {
System.out.println(user);
}
}
要和 Spring 一起使用 MyBatis,需要在 Spring 应用上下文中定义至少两样东西:一个 SqlSessionFactory 和至少一个数据映射器类。
在 MyBatis-Spring 中,可使用 SqlSessionFactoryBean来创建 SqlSessionFactory
可以使用SqlSessionDaoSupport来实现Spring mybatis的整合
2.4、事务实现
代码环境还是上面的环境
增加一个public void deleteUser(@Param(“id”) int id);方法
public class UserDaoImpl extends SqlSessionDaoSupport implements UserMapper {
public List<User> selectUser() {
Map<String, Object> map = new HashMap<String, Object>();
//出生日期
Date parse = null;
try {
parse = new SimpleDateFormat("yyyy-MM-dd").parse("2020-7-1");
} catch (ParseException e) {
e.printStackTrace();
}
map.put("id",4);
map.put("name","小张");
map.put("password","123456");
map.put("email","[email protected]");
map.put("birthday",parse);
UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
mapper.addUser(map);
mapper.deleteUser(2);
return mapper.selectUser();
}
public int addUser(Map<String, Object> map) {
UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
return mapper.addUser(map);
}
//删除
public int deleteUser(int id) {
UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
return mapper.deleteUser(id);
}
}
xml配置(删除方法故意写错)
<delete id="deleteUser" parameterType="int">
deletes from users where id = #{id}
</delete>
<insert id="addUser">
insert into users (id,name,password,email,birthday) values (#{id},#{name},#{password},#{email},#{birthday})
</insert>
Spring中事务开启
<!--配置事务-->
<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="delete" propagation="REQUIRED"/>
<tx:method name="update" propagation="REQUIRED"/>
<tx:method name="search*" propagation="REQUIRED"/>
<tx:method name="get" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--配置aop织入事务-->
<aop:config>
<aop:pointcut id="txPointcut" expression="execution(* com.song.mapper.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
测试
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("mybatis-spring.xml");
UserMapper mapper = (UserMapper) context.getBean("userMapper");
List<User> user = mapper.selectUser();
for (User user1 : user) {
System.out.println(user1);
}
}
Spring定义了7种类传播行为
若想了解更多的Spring建议查看 Spring官网
谢谢大家的阅读! 若上面有写错的 欢迎纠正哦!