一直想学习怎么使用spring 的事务,在网上查了很多资料,还是不知道怎么写,大都是基于注解方式的,要么就是基于hibernate的事务,而且没有完整的例子,都是代码片段,这里我自己写了一个简单的demo,供给大家学习
1、建立一个对象(跟数据库中的表对应)
- package hb.bean;
-
- public class Book {
- private int id;
- private String name;
- private String author;
- private int num;
-
- public int getNum() {
- return num;
- }
-
- public void setNum(int num) {
- this.num = num;
- }
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getAuthor() {
- return author;
- }
-
- public void setAuthor(String author) {
- this.author = author;
- }
- }
2、建立一个接口(实现对表的相关操作)
- package hb.dao;
-
- import hb.bean.Book;
- import java.util.List;
- import org.springframework.transaction.annotation.Transactional;
-
- @Transactional
- public interface BookDAO {
-
- //查看book表中的所有数据
- public List listBook();
-
- //向book表中插入一条数据
- public void insertBook(Book book);
- }
3、实现接口的操作
- package hb.dao.imp;
-
- import hb.bean.Book;
- import hb.dao.BookDAO;
- import hb.row.UserRowMapper;
-
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.List;
-
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.jdbc.core.RowMapper;
- import org.springframework.jdbc.core.support.JdbcDaoSupport;
- import org.springframework.jdbc.datasource.DataSourceTransactionManager;
- import org.springframework.transaction.TransactionStatus;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.transaction.support.TransactionCallbackWithoutResult;
- import org.springframework.transaction.support.TransactionTemplate;
-
- import sun.jdbc.odbc.ee.DataSource;
-
- @Transactional
- public class BookDAOImpl extends JdbcDaoSupport implements BookDAO {
-
- public DataSourceTransactionManager transactionManager;
-
- public void setTransactionManager(DataSourceTransactionManager transactionManager) {
- this.transactionManager = transactionManager;
- }
-
- @SuppressWarnings("unchecked")
- public List listBook() {
- String sql = "select * from book";
- //JdbcDaoSupport里面有jdbcTemplate这个对象的set方法.JdbcTemplate这个类需要配置数据源
- List list = this.getJdbcTemplate().query(sql, new UserRowMapper());
- return list;
- }
-
- public void insertBook(final Book book) {
- final String sql = "insert into book(id,name,author,num)values(?,?,?,?)";
- TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
- final JdbcTemplate jdbcTemplate = this.getJdbcTemplate();
- transactionTemplate.execute(new TransactionCallbackWithoutResult(){
- @Override
- protected void doInTransactionWithoutResult(TransactionStatus arg0) {
- Object[] params = new Object[] {book.getId(), book.getName(), book.getAuthor(),book.getNum() };
- //下面的语句是用来测试事务异常的情况
- // new Integer("huangbiao");
- jdbcTemplate.update(sql, params);
- }
- });
-
- }
-
- }
4、继承RowMapper 接口接受返回过来的结果集
- package hb.row;
-
- import hb.bean.Book;
-
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- import org.springframework.jdbc.core.RowMapper;
-
- public class UserRowMapper implements RowMapper {
-
- public Object mapRow(ResultSet rs,int index) throws SQLException {
- Book book = new Book();
- System.out.println(rs.getString("name"));
- book.setName(rs.getString("name"));
- return book;
- }
- }
posted on 2012-07-26 22:31
地心引力 阅读(1915)
评论(2) 编辑 收藏