跳到主要内容

吃透Spring源码(二十一):事物初始化流程之注解配置方式

一,例子准备

BookDao.java

public class BookDao {



@Autowired
JdbcTemplate jdbcTemplate;

public JdbcTemplate getJdbcTemplate() {


return jdbcTemplate;
}

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {


this.jdbcTemplate = jdbcTemplate;
}

/**
* 减去某个用户的余额
* @param userName
* @param price
*/
@Transactional(propagation = Propagation.SUPPORTS)
public void updateBalance(String userName,int price){


String sql = "update account set balance=balance-? where username=?";
jdbcTemplate.update(sql,price,userName);
}

/**
* 按照图书的id来获取图书的价格
* @param id
* @return
*/
@Transactional(propagation = Propagation.REQUIRED)
public int getPrice(int id){


String sql = "select price from book where id=?";
return jdbcTemplate.queryForObject(sql,Integer.class,id);
}

/**
* 减库存,减去某本书的库存
* @param id
*/
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void updateStock(int id){


String sql = "update book_stock set stock=stock-1 where id=?";
jdbcTemplate.update(sql,id);
}
}