论坛首页 Java企业应用论坛

jdbc还是ibatis?

浏览 50535 次
该帖已经被评为良好帖
作者 正文
   发表时间:2007-02-26  
我以前做的一个对jdbc的封装(仿照Spring的实现,当然功能弱些,所以代码少很多,容易看)
以下是对jdbc进行了简单的封装:使用Template mothod模式和回调接口大大减少了对jdbc的低级操作。其中JdbcTemplate类提供了使用jdbc访问数据库的模板,提供了queryForObject和queryForList方法来分别获取查询到的对象和对象的聚集,execute方法来更新、插入、删除操作。提供了RowCallBackHandler接口,可以通过实现该接口来提供对查询一行的回调处理,使用PreparedStatementCallBack回调接口,来提供对PreparedStatement参数的设置,提供了ORMaping接口,通过实现该接口可以很容易实现关系数据库与domain对象的映射,并产生可重用的逻辑
以下是代码包含对JDBC的封装和Dao的实现:
package edu.jlu.fuliang.library.jdbc;
import java.util.ArrayList;
import java.util.List;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class JdbcTemplate {
 
       public List query(String sql,RowCallBackHandler handler){
              Connection connection = JdbcUtils.createConnection();
              PreparedStatement ps = JdbcUtils.createPreparedStatement(sql,connection);
              ResultSet rs = null;
              List list = new ArrayList();
              try {
                     rs = ps.executeQuery();
                     while(rs.next()){
                            list.add(handler.processRow(rs));
                     }
              } catch (SQLException e) {
                     e.printStackTrace();
              }finally{
                     JdbcUtils.closeStatement(ps);
                     JdbcUtils.relaseConnection(connection);
              }
              return list;
       }
       
       public Object queryForObject(String sql,RowCallBackHandler handler){
              Connection connection = JdbcUtils.createConnection();
              PreparedStatement ps = JdbcUtils.createPreparedStatement(sql,connection);
              ResultSet rs = null;
              Object o = null;
              try {
                     rs = ps.executeQuery();
                     rs.next();
                     o = handler.processRow(rs);
              } catch (SQLException e) {
                     e.printStackTrace();
              }finally{
                     JdbcUtils.closeStatement(ps);
                     JdbcUtils.relaseConnection(connection);
              }
              return o;
       }
       
       public void executeUpdate(String sql,PreparedStatementCallBack pareparedStatementCallBack){
              Connection connection = JdbcUtils.createConnection();
              PreparedStatement ps = JdbcUtils.createPreparedStatement(sql,connection);
              try {
                     pareparedStatementCallBack.doInStatement(ps);
                     ps.executeUpdate();
              } catch (SQLException e) {
                     e.printStackTrace();
              }finally{
                     JdbcUtils.closeStatement(ps);
                     JdbcUtils.relaseConnection(connection);
              }
       }
}
//回调接口RowCallBackHandler
package edu.jlu.fuliang.library.jdbc;
 
import java.sql.ResultSet;
 
public interface RowCallBackHandler {
 public Object processRow(ResultSet rs);
}
///回调接口PreparedStatementCallBack
package edu.jlu.fuliang.library.jdbc;
 
import java.sql.PreparedStatement;
import java.sql.SQLException;
 
public interface PreparedStatementCallBack {
       public void doInStatement(PreparedStatement stmt)throws SQLException;
}
//ORMaping接口
package edu.jlu.fuliang.library.jdbc;
 
import java.sql.ResultSet;
 
public interface ORMaping {
       public Object mapping(ResultSet rs,Object o);
}
//JdbcUtils.java
package edu.jlu.fuliang.library.jdbc;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
 
public class JdbcUtils {
       
       public static Connection createConnection(){//为了简单,进行了硬编码
              String user = "root";
              String passwd = "123456";
           String url = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=gb2312";
           Connection connection = null;
           try{
              Class.forName("com.mysql.jdbc.Driver").newInstance();
              connection = DriverManager.getConnection(url,user,passwd);
           }
            catch(Exception e){
                  e.printStackTrace();
           }
            return connection;
       }
       
       public static PreparedStatement createPreparedStatement(String sql,Connection connection){
              PreparedStatement stmt = null;
              try{
              stmt = connection.prepareStatement(sql);
           }catch(Exception e){
                  e.printStackTrace();
           }
           return stmt;
       }
       
       public static void closeStatement(Statement s){
              try {
                     s.close();
              } catch (SQLException e) {
                     e.printStackTrace();
              }
       }
       
       public static void relaseConnection(Connection connection){
              try {
                     connection.close();
              } catch (SQLException e) {
                     e.printStackTrace();
              }
       }
}
//以下是数据访问对象:
//BorrowerDao.java
 
package edu.jlu.fuliang.library.dao;
 
import edu.jlu.fuliang.library.domain.Borrower;
 
public interface BorrowerDao {
       public Borrower queryByLastName(String lastName);
       public void updateBorrower(Borrower borrower);
       public void insertBorrower(Borrower borrower);
       public void deleteBorrowerByLastName(String lastName);
}
// BookDao.java
package edu.jlu.fuliang.library.dao;
 
 
import java.util.List;
import edu.jlu.fuliang.library.domain.Book;
 
public interface BookDao {
 public Book queryByTitle(String title);
 public List queryByAuthor(String author);
 public List queryByBorrowerLastName(String firstName);
 public void updateBook(Book book);
 public void insertBook(Book book);
 public void deleteBookByTile(String title);
}
// BorrowOrReturnDao.java
package edu.jlu.fuliang.library.dao;
 
public interface BorrowOrReturnDao {
       public void borrowBook(String borrowerLastName,String bookTile);
       public void returnBook(String borrowerLastName,String bookTile);
}
//抽象工厂:
package edu.jlu.fuliang.library.dao;
 
public interface DaoFactory {
       public BookDao createBookDao();
       public BorrowerDao createBorrowerDao();
       public BorrowOrReturnDao createBorrowOrReturnDao();
}
//具体工厂:
package edu.jlu.fuliang.library.dao;
 
import edu.jlu.fuliang.library.dao.mysql.BookDaoImpl;
import edu.jlu.fuliang.library.dao.mysql.BorrowerDaoImpl;
 
public class MysqlDaoFactory implements DaoFactory{
 
       public BookDao createBookDao() {
              return new BookDaoImpl();
       }
 
       public BorrowerDao createBorrowerDao() {
              return new BorrowerDaoImpl();
       }
       public BorrowOrReturnDao createBorrowOrReturnDao() {
              return new BorrowOrReturnDaoImpl();
       }
}
//BookDao的具体实现:
//BookDaoImpl.java
package edu.jlu.fuliang.library.dao.mysql;
 
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
 
import edu.jlu.fuliang.library.dao.BookDao;
import edu.jlu.fuliang.library.domain.Book;
import edu.jlu.fuliang.library.jdbc.*;
 
public class BookDaoImpl implements BookDao{
       private JdbcTemplate jdbcTemplate = new JdbcTemplate();
       
       public Book queryByTitle(String title) {
              String sql = "select * from book where title = "+title;
              return (Book)jdbcTemplate.queryForObject(sql,new RowCallBackHandler(){
                     public Object processRow(ResultSet rs) {
                            BookMaping map = new BookMaping();
                            Book book = (Book)map.mapping(rs,new Book());
                         return book;
                     }
              });
       }
 
       public List queryByAuthor(String author) {
              String sql = "select * from book where author = "+author;
              return jdbcTemplate.query(sql,new RowCallBackHandler(){
                     public Object processRow(ResultSet rs) {
                            BookMaping map = new BookMaping();
                            Book book = (Book)map.mapping(rs,new Book());
                            return book;
                     }
              });
       }
//Borrower的具体实现:
//BookDaoImpl.java
package edu.jlu.fuliang.library.dao.mysql;
 
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
import edu.jlu.fuliang.library.dao.BorrowerDao;
import edu.jlu.fuliang.library.domain.Borrower;
import edu.jlu.fuliang.library.jdbc.JdbcTemplate;
import edu.jlu.fuliang.library.jdbc.PreparedStatementCallBack;
import edu.jlu.fuliang.library.jdbc.RowCallBackHandler;
 
public class BorrowerDaoImpl implements BorrowerDao{
       private JdbcTemplate jdbcTemplate = new JdbcTemplate();
       
       public Borrower queryByLastName(String lastName) {
              String sql = "select * from borrower where lastName = " + lastName;
              return (Borrower)jdbcTemplate.queryForObject(sql,new RowCallBackHandler(){
                     public Object processRow(ResultSet rs) {
                            BorrowerMaping borrowerMaping = new BorrowerMaping();
                            return borrowerMaping.mapping(rs,new Borrower());
                     }
              });
       }
 
       public void updateBorrower(final Borrower borrower) {
              String sql = "update borrower set lastname = ?,firstname=?" +
                            ",address =?,city =?,zip=?,state=? where lastname = ?";
              jdbcTemplate.executeUpdate(sql,new PreparedStatementCallBack(){
                     public void doInStatement(PreparedStatement stmt) throws SQLException {
                            stmt.setString(1,borrower.getLastName());
                            stmt.setString(2,borrower.getFirstName());
                            stmt.setString(3,borrower.getAddress());
                            stmt.setString(4,borrower.getCity());
                            stmt.setString(5,borrower.getZip());
                            stmt.setString(6,borrower.getState());
                            stmt.setString(7,borrower.getLastName());
                     }
              });
              
       }
 
       public void insertBorrower(final Borrower borrower) {
              String sql = "insert into borrower values(?,?,?,?,?,?)";
              jdbcTemplate.executeUpdate(sql,new PreparedStatementCallBack(){
                     public void doInStatement(PreparedStatement stmt) throws SQLException {
                            stmt.setString(1,borrower.getLastName());
                            stmt.setString(2,borrower.getFirstName());
                            stmt.setString(3,borrower.getAddress());
                            stmt.setString(4,borrower.getCity());
                            stmt.setString(5,borrower.getZip());
                            stmt.setString(6,borrower.getState());
                     }
              });
       }
 
       public void deleteBorrowerByLastName(final String lastName) {
              String sql = "delete from borrower where lastName = ?";
              jdbcTemplate.executeUpdate(sql,new PreparedStatementCallBack(){
 
                     public void doInStatement(PreparedStatement stmt) throws SQLException {
                            stmt.setString(1,lastName);
                     }
              });
       }
       
}
// BorrowOrReturnDaoImpl.java
package edu.jlu.fuliang.library.dao.mysql;
 
import java.sql.PreparedStatement;
import java.sql.SQLException;
 
import edu.jlu.fuliang.library.dao.BorrowOrReturnDao;
import edu.jlu.fuliang.library.jdbc.JdbcTemplate;
import edu.jlu.fuliang.library.jdbc.PreparedStatementCallBack;
 
public class BorrowOrReturnDaoImpl implements BorrowOrReturnDao{
       private JdbcTemplate jdbcTemplate = new JdbcTemplate();
       
       public void borrowBook(final String borrowerLastName, final String bookTitle) {
              String sql = "insert into book_borrower values(?,?)";
              jdbcTemplate.executeUpdate(sql,new PreparedStatementCallBack(){
                     public void doInStatement(PreparedStatement stmt) {
                            try{
                                   stmt.setString(1,borrowerLastName);
                                   stmt.setString(2,bookTitle);
                            }catch(SQLException e){
                                   e.printStackTrace();
                            }
                     }
              });
       }
 
       public void returnBook(final String borrowerLastName, final String bookTitle) {
              String sql = "delete from book_borrower where lastname = ?,title = ?";
              jdbcTemplate.executeUpdate(sql,new PreparedStatementCallBack(){
                     public void doInStatement(PreparedStatement stmt) {
                            try{
                                   stmt.setString(1,borrowerLastName);
                                   stmt.setString(2,bookTitle);
                            }catch(SQLException e){
                                   e.printStackTrace();
                            }
                     }
              });
       }
}
//book的ORMaping的实现:
// BookMaping.java
package edu.jlu.fuliang.library.dao.mysql;
 
import java.sql.ResultSet;
import java.sql.SQLException;
 
import edu.jlu.fuliang.library.domain.Book;
import edu.jlu.fuliang.library.jdbc.ORMaping;
 
public class BookMaping implements ORMaping{
 
       public Object mapping(ResultSet rs, Object o) {
              Book book = (Book)o;
              try {
                     book.setTitleName(rs.getString("titleName"));
                     book.setAuthor(rs.getString("author"));
                     book.setISBN(rs.getString("isbn"));
                     book.setType(rs.getInt("type"));
                     book.setItemsAvailable(rs.getInt("itemAvailable"));
              } catch (SQLException e) {
                     e.printStackTrace();
              }
              return book;
       }
 
}
////borrower的ORMaping的实现:
// BorrowerMaping.java
package edu.jlu.fuliang.library.dao.mysql;
 
import java.sql.ResultSet;
import java.sql.SQLException;
 
import edu.jlu.fuliang.library.domain.Borrower;
import edu.jlu.fuliang.library.jdbc.ORMaping;
 
public class BorrowerMaping implements ORMaping{
 
       public Object mapping(ResultSet rs, Object o) {
              Borrower borrower = (Borrower)o;
              try {
                     borrower.setLastName(rs.getString(1));
                     borrower.setFirstName(rs.getString(2));
                     borrower.setAddress(rs.getString(3));
                     borrower.setCity(rs.getString(4));
                     borrower.setZip(rs.getString(5));
                     borrower.setState(rs.getString(6));
              } catch (SQLException e) {
                     e.printStackTrace();
              }
              return borrower;
       }
 
}
0 请登录后投票
   发表时间:2007-02-26  
intolong 写道
ajoo不是写过jdbc template么?

http://www.iteye.com/topic/7068

和你同事那个差不多吧,一个继承,一个组合

他这个还不如我的那个。他的这个实现没有处理Error的情况。在Error出现的情况,不会回滚事务。

不管我的还是同事的这个都有若干个问题:
1。不支持cache。还要自己写cache,不如ibatis直接提供的cache支持方便。
2。不支持一个connection多个query或者命令。


0 请登录后投票
   发表时间:2007-02-26  
jdbc 不是架构
ibatis是架构

要用什么就看哪个架构省力
如果要升级那就要看是否非要升级所有的文件
原有的有必要修改的代码也可以用重构的方式完成
由于jdbc可以离开架构而存在
所以没必要非把所有的代码都移到新的架构上面
0 请登录后投票
   发表时间:2007-04-13  
这根本就不是技术问题,都是人的问题,人家固执保守,不肯学习新东西,确实就很无奈。当architect没有他人的拥护看来也是不行的。
0 请登录后投票
   发表时间:2007-08-15  
屁股决定脑袋
因为自己的立场和利益,每个人都会坚持一些从别人看来不可理解的东西,都会时不时的担心一下这个新的外来者是不是会损害自己的利益。
最让你感觉到无能为力的是“我不care”,但这其实也是你最大的攻击点,问问他care什么?告诉他什么能帮助他更好的care这些东西。当然,一定不能忘了工程的投入产出这个最大的砝码。
0 请登录后投票
   发表时间:2007-08-15  
ibatis麻烦?你用hibernate试试
0 请登录后投票
   发表时间:2007-08-16  
原来的系统跑得好好的,需要很强的理由才能说服当官的决定替换。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics