隨筆-26  評論-12  文章-0  trackbacks-0

          一直想學(xué)習(xí)怎么使用spring 的事務(wù),在網(wǎng)上查了很多資料,還是不知道怎么寫,大都是基于注解方式的,要么就是基于hibernate的事務(wù),而且沒有完整的例子,都是代碼片段,這里我自己寫了一個簡單的demo,供給大家學(xué)習(xí)

           

          1、建立一個對象(跟數(shù)據(jù)庫中的表對應(yīng))

           

          Java代碼  收藏代碼
          1. package hb.bean;  
          2.   
          3. public class Book {  
          4.     private int id;  
          5.     private String name;  
          6.     private String author;  
          7.     private int num;  
          8.   
          9.     public int getNum() {  
          10.         return num;  
          11.     }  
          12.   
          13.     public void setNum(int num) {  
          14.         this.num = num;  
          15.     }  
          16.   
          17.     public int getId() {  
          18.         return id;  
          19.     }  
          20.   
          21.     public void setId(int id) {  
          22.         this.id = id;  
          23.     }  
          24.   
          25.     public String getName() {  
          26.         return name;  
          27.     }  
          28.   
          29.     public void setName(String name) {  
          30.         this.name = name;  
          31.     }  
          32.   
          33.     public String getAuthor() {  
          34.         return author;  
          35.     }  
          36.   
          37.     public void setAuthor(String author) {  
          38.         this.author = author;  
          39.     }  
          40. }  

           

           2、建立一個接口(實現(xiàn)對表的相關(guān)操作)

           

          Java代碼  收藏代碼
          1. package hb.dao;  
          2.   
          3. import hb.bean.Book;  
          4. import java.util.List;  
          5. import org.springframework.transaction.annotation.Transactional;  
          6.   
          7. @Transactional  
          8. public interface BookDAO {  
          9.       
          10.     //查看book表中的所有數(shù)據(jù)  
          11.     public List listBook();  
          12.       
          13.     //向book表中插入一條數(shù)據(jù)  
          14.     public void insertBook(Book book);  
          15. }  

           

           3、實現(xiàn)接口的操作

           

          Java代碼  收藏代碼
          1. package hb.dao.imp;  
          2.   
          3. import hb.bean.Book;  
          4. import hb.dao.BookDAO;  
          5. import hb.row.UserRowMapper;  
          6.   
          7. import java.sql.ResultSet;  
          8. import java.sql.SQLException;  
          9. import java.util.List;  
          10.   
          11. import org.springframework.jdbc.core.JdbcTemplate;  
          12. import org.springframework.jdbc.core.RowMapper;  
          13. import org.springframework.jdbc.core.support.JdbcDaoSupport;  
          14. import org.springframework.jdbc.datasource.DataSourceTransactionManager;  
          15. import org.springframework.transaction.TransactionStatus;  
          16. import org.springframework.transaction.annotation.Transactional;  
          17. import org.springframework.transaction.support.TransactionCallbackWithoutResult;  
          18. import org.springframework.transaction.support.TransactionTemplate;  
          19.   
          20. import sun.jdbc.odbc.ee.DataSource;  
          21.   
          22. @Transactional  
          23. public class BookDAOImpl extends JdbcDaoSupport implements BookDAO {  
          24.       
          25.     public DataSourceTransactionManager transactionManager;  
          26.   
          27.     public void setTransactionManager(DataSourceTransactionManager transactionManager) {  
          28.         this.transactionManager = transactionManager;  
          29.     }  
          30.   
          31.     @SuppressWarnings("unchecked")  
          32.     public List listBook() {  
          33.         String sql = "select * from book";  
          34.         //JdbcDaoSupport里面有jdbcTemplate這個對象的set方法.JdbcTemplate這個類需要配置數(shù)據(jù)源  
          35.         List list = this.getJdbcTemplate().query(sql, new UserRowMapper());  
          36.         return list;  
          37.     }  
          38.   
          39.     public void insertBook(final Book book) {  
          40.         final String sql = "insert into book(id,name,author,num)values(?,?,?,?)";  
          41.         TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);  
          42.         final JdbcTemplate jdbcTemplate = this.getJdbcTemplate();  
          43.         transactionTemplate.execute(new TransactionCallbackWithoutResult(){  
          44.             @Override  
          45.             protected void doInTransactionWithoutResult(TransactionStatus arg0) {  
          46.                 Object[] params = new Object[] {book.getId(), book.getName(), book.getAuthor(),book.getNum() };  
          47.                 //下面的語句是用來測試事務(wù)異常的情況  
          48. //                new Integer("huangbiao");  
          49.                 jdbcTemplate.update(sql, params);  
          50.             }  
          51.         });  
          52.           
          53.     }  
          54.   
          55. }  

           

           4、繼承RowMapper 接口接受返回過來的結(jié)果集

           

          Java代碼  收藏代碼
          1. package hb.row;  
          2.   
          3. import hb.bean.Book;  
          4.   
          5. import java.sql.ResultSet;  
          6. import java.sql.SQLException;  
          7.   
          8. import org.springframework.jdbc.core.RowMapper;  
          9.   
          10. public class UserRowMapper implements RowMapper {  
          11.   
          12.     public Object mapRow(ResultSet rs,int index) throws SQLException {  
          13.         Book book = new Book();  
          14.         System.out.println(rs.getString("name"));  
          15.         book.setName(rs.getString("name"));  
          16.         return book;  
          17.     }  
          18. }  
          posted on 2012-07-26 22:31 地心引力 閱讀(1921) 評論(2)  編輯  收藏

          評論:
          # re: spring JDBC事務(wù)管理 2013-06-15 18:48 |
          例子寫得很好,不過能不能把配置文件和所依賴的包也貼出來。  回復(fù)  更多評論
            
          # re: spring JDBC事務(wù)管理 2014-02-12 22:22 | 風(fēng)格化
          DataSourceTransactionManager 怎么配置  回復(fù)  更多評論
            

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 嘉黎县| 洞头县| 拉萨市| 岢岚县| 普兰县| 大英县| 名山县| 顺平县| 潢川县| 寻甸| 武平县| 广德县| 孟州市| 富顺县| 金秀| 高雄市| 灌云县| 宁蒗| 铜梁县| 雷山县| 高安市| 甘南县| 金塔县| 辉县市| 舒城县| 彰武县| 大港区| 云林县| 洛扎县| 大连市| 五家渠市| 华安县| 丁青县| 曲沃县| 山东省| 新龙县| 日喀则市| 麻阳| 临安市| 高唐县| 兴海县|