声明事务-基于AOP或全注解方式实现

声明事务

声明事务-实现方式

声明式事务管理的配置类型

  • 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配置




    
    

    
    
    
    

    
    

    
    
        
    

    
    

    
    
        
            
            
            
            
        
    

    
    
        
        
        
        
    

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 findAll() {
        String sql="select * from books";
        List booksList = jdbcTemplate.query(sql, new BooksRowMapper());
        return booksList;
    }

    private class BooksRowMapper implements RowMapper{

        @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文件中要开启事务的支持






    
        
        
        
        
    

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();
    }
}

你可能感兴趣的:(声明事务-基于AOP或全注解方式实现)