声明事务
声明事务-实现方式
声明式事务管理的配置类型
- 5种类型:独立代理、共享代理、拦截器、tx拦截器、全注解(前三种Spring官方团队以及不推荐使用了)
tx拦截器具体实现
说明:
所需jar包
1.Spring(基础jar包) beans、core、context、expression 2.测试jar包 junit、spring-test 3.Spring集成 Jdbc spring-jdbc、mysql-connector-java(驱动) 4.Spring-tx事务 spring-tx 5.连接池 commons-dbcp2、commons-pool 6.AOP spring-aop、aopalliance 7.aspectJ相关jar包 spring-aspects、aspectjweaver
本地数据库配置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/books
username=root
password=root
XML配置
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--扫描包-->
<context:component-scan base-package="cn.akwangl.service"/>
<!--引入本地数据库配置-->
<context:property-placeholder location="classpath:jdbc.properties" />
<!--添加数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
p:driverClassName="${driverClassName}" p:url="${url}" p:username="${username}" p:password="${password}"/>
<!--jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource"/>
<!--事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 注释模式事务:启动使用注解实现声明式事务管理的支持 -->
<!--<tx:annotation-driven transaction-manager="txManager" />-->
<!--配置tx事务拦截器-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!--所有以find开头查询方法只有读的权限-->
<tx:method name="find*" read-only="true"/>
<!--其它方法使用默认值-->
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置切面 -->
<aop:config>
<!--配置切点-->
<aop:pointcut id="bookService" expression="execution(* cn.akwangl.service.impl.BooksServiceImpl.*(..))"/>
<!--配置通知-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="bookService"/>
</aop:config>
</beans>
Model
/**
* 书籍实体类
* @author 阿坤
*/
public class Books {
private Long id;
//ISBN号
private String isbn;
//书名
private String name;
//价格
private Double price;
//发布日期
private Date pubdate;
//set、get方法...
}
Dao
@Service
public class BooksServiceImpl implements BooksService {
@Resource(name = "jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Override
public void save(Books books) {
String sql="insert into books(isbn,name,price,pubdate) values(?,?,?,?)";
jdbcTemplate.update(sql, books.getIsbn(),books.getName(),books.getPrice(),books.getPubdate());
}
@Override
public void updata(Books books) {
String sql="update books set(isbn=?,name=?,price=?,pubdate=?)";
jdbcTemplate.update(sql, books.getIsbn(),books.getName(),books.getPrice(),books.getPubdate());
}
@Override
public void delete(Long id) {
String sql="delete * from books where id = ?";
jdbcTemplate.update(sql, id);
}
@Override
public Books findOne(Long id) {
String sql = "select * from books where id =?";
Books books = jdbcTemplate.queryForObject(sql, new BooksRowMapper(), id);
return books;
}
@Override
public List<Books> findAll() {
String sql="select * from books";
List<Books> booksList = jdbcTemplate.query(sql, new BooksRowMapper());
return booksList;
}
private class BooksRowMapper implements RowMapper<Books>{
@Override
public Books mapRow(ResultSet resultSet, int i) throws SQLException {
Books books = new Books();
books.setId(resultSet.getLong("id"));
books.setIsbn(resultSet.getString("isbn"));
books.setName(resultSet.getString("name"));
books.setPrice(resultSet.getDouble("price"));
books.setPubdate(resultSet.getDate("pubdate"));
return books;
}
}
}
Test类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:Spring-tx2.xml")
public class Books_Test {
@Autowired
private BooksService booksService;
/**
* 说明:
* 在XML文件中已经配置了只读属性
* TransientDataAccessResourceException: PreparedStatementCallback; SQL [insert into books(isbn,name,price,pubdate) values(?,?,?,?)];
* Connection is read-only. Queries leading to data modification are not allowed;
* 报这个错是正常的,你只要在XML中将read-only改为false即可,不写默认为(false)
*/
@Test
public void save(){
booksService.save(new Books(null,"123-166-891-China","小白成神之路",99.00,new Date()));
}
}
全注解非常简单
只需将@Transactional注解加入到要开启事务的类或者方法上
前提: XMl文件中要开启事务的支持
<!--启动使用注解实现声明式事务管理的支持-->
<tx:annotation-driven transaction-manager="txManager" />
<!--配置tx事务拦截器-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!--所有以find开头查询方法只有读的权限-->
<tx:method name="find*" read-only="true"/>
<!--其它方法使用默认值-->
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
Demo
@Transactional
public class XbeanServiceImpl implements XbeanService{
@Override
public Xbean getXbean(int id) {
Xbean xb = new Xbean() ;
xb.setName("业务Bean的ID="+id);
xb.setName("Bean默认名称");
return xb ;
}
@Override
public void insertXbean(Xbean xb) {
throw new UnsupportedOperationException();
}
}
网友评论