持久层操作的java代码

第一个是操作数据库方法的抽象接口,
其实这个接口也很简单,定义了那么几个方法,说白了就是操作数据库的。
为什么要写成泛型的接口,为了就是后面大家的业务有针对性,一个实体一个业务功能类。


Java代码 复制代码
  1. package  com.yd.idao;   
  2.   
  3. import  java.util.List;   
  4. import  java.sql.Connection;   
  5. import  java.sql.ResultSet;   
  6. import  java.sql.SQLException;   
  7.   
  8. import  com.yd.support.JDataSet;   
  9.   
  10. /**  
  11.  * 一个定义了所有我所需要的数据库操作的方法接口,  
  12.  * 为什么定义为抽象,我自己都搞不清楚,  
  13.  * 这里定义了泛型,这个很关键,你会看到泛型在这里的使用  
  14.  * @author kanny  
  15.  *  
  16.  * @param <T>  
  17.  */   
  18. public   abstract   interface  ISqlHelper<T> {   
  19.   
  20.      /**  
  21.      * 执行sql语句,大多为单句插入语句  
  22.      * @param sql   单句的sql语句  
  23.      * @param params    插入的参数  
  24.      * @param only      但为true时,sql语句为查询数量的查询语句  
  25.      * @return  
  26.      * @throws SQLException  
  27.      */   
  28.      public   boolean  execute(String sql, Object[] params,  boolean  only)  throws  SQLException;   
  29.   
  30.      /**  
  31.      * 执行sql的批处理  
  32.      * @param sqlBatch  多条sql语句  
  33.      * @return  
  34.      * @throws SQLException  
  35.      */   
  36.      public   boolean  executeBatch(String[] sqlBatch)  throws  SQLException;   
  37.   
  38.      /**  
  39.      * 执行存储过程  
  40.      * @param procName  存储过程名称  
  41.      * @param params    存储过程说需要的参数  
  42.      * @return  
  43.      * @throws SQLException  
  44.      */   
  45.      public   boolean  executeCall(String procName, Object[] params)  throws  SQLException;   
  46.   
  47.      /**  
  48.      * 查询一行数据封装成java的实体  
  49.      * @param sql   sql语句  
  50.      * @param params    sql条件参数  
  51.      * @param viewName  视图名字,在查询多表关联的数据时用于区分  
  52.      * @param executeCall   是否是存储过程,如果为true,第一个sql的参数为存储过程名称  
  53.      * @return  
  54.      * @throws SQLException  
  55.      */   
  56.      public  T find(String sql, Object[] params, String viewName,  boolean  executeCall)  throws  SQLException;   
  57.   
  58.      /**  
  59.      * 查询多行数据封装成java的实体加入一个List里  
  60.      * @param sql   sql语句  
  61.      * @param params    sql条件参数  
  62.      * @param viewName  视图名字,在查询多表关联的数据时用于区分,循环封装实体比单一的石头封装要复杂  
  63.      * @param executeCall   是否是存储过程,如果为true,第一个sql的参数为存储过程名称  
  64.      * @return  
  65.      * @throws SQLException  
  66.      */   
  67.      public  List<T> findList(String sql, Object[] params, String viewName,  boolean  executeCall)  throws  SQLException;   
  68.   
  69.      /**  
  70.      * 为了方便操作,我还特意写定义了这个返回ResultSet的方法,便于直接操作  
  71.      * @param sql  
  72.      * @param params  
  73.      * @param executeCall  
  74.      * @return  
  75.      * @throws SQLException  
  76.      */   
  77.      public  ResultSet returnResultSet(String sql, Object[] params,  boolean  executeCall)  throws  SQLException;   
  78.        
  79.      /**  
  80.      * 我的底层分页方法,我会给出具体代码,但这里用的只是sql server 2008的数据库分页  
  81.      * @param sql     
  82.      * @param orderby   排序列  
  83.      * @param currentPage   当前页  
  84.      * @param pageSize  每页多少行  
  85.      * @return  
  86.      * @throws SQLException  
  87.      */   
  88.      public  List<T> findListAsPager(String sql, String orderby,  int  currentPage,  int  pageSize)  throws  SQLException;   
  89.   
  90.      /**  
  91.      * 后来为了方便操作,想朋友要来了这个JDataSet类,类似.net的DataTable的作用  
  92.      * @param sql  
  93.      * @param params  
  94.      * @param fillColumnNames   是否填充类名,请看方法代码  
  95.      * @param executeCall  
  96.      * @return  
  97.      */   
  98.      public  JDataSet getJDataSet(String sql, Object[] params,  boolean  fillColumnNames,  boolean  executeCall);   
  99.        
  100.      /**  
  101.      * 由于有了JDataSet这个类,于是我有写了调用这个类的分页方法  
  102.      * @param sql  
  103.      * @param orderby  
  104.      * @param currentPage  
  105.      * @param pageSize  
  106.      * @return  
  107.      * @throws SQLException  
  108.      */   
  109.      public  JDataSet getJDataSetAsPager(String sql, String orderby,  int  currentPage,  int  pageSize)  throws  SQLException;   
  110.        
  111.      /**  
  112.      * 为了方便起见,我多写了一个直接传入ResultSet而封装JDataSet的多余方法  
  113.      * @param rs  
  114.      * @param fillColumnNames  
  115.      * @return  
  116.      * @throws SQLException  
  117.      */   
  118.      public  JDataSet loadJDataSet(ResultSet rs,  boolean  fillColumnNames)  throws  SQLException;   
  119.        
  120.      /**  
  121.      * 得到查询数据的行数  
  122.      * @param sql  
  123.      * @return  
  124.      * @throws SQLException  
  125.      */   
  126.      public   int  getRowCount(String sql)  throws  SQLException;   
  127.        
  128.      /**  
  129.      * 请看源码  
  130.      * @param rs  
  131.      * @param column  
  132.      * @return  
  133.      * @throws SQLException  
  134.      */   
  135.      public  String changeFont(ResultSet rs, String column)  throws  SQLException;   
  136.        
  137.      /**  
  138.      * 得到连接  
  139.      * @return  
  140.      * @throws SQLException  
  141.      */   
  142.      public  Connection returnConn()  throws  SQLException;    
  143.        
  144.      /**  
  145.      * 清楚所有数据库操作对象  
  146.      * @throws SQLException  
  147.      */   
  148.      public   void  clearAllsql()  throws  SQLException;    
  149.   
  150. }  





第2个类是实现这个抽象接口的抽象模版方法类
这个类最为关键,它肯定是实现了ISqlHepler.java里面的所有的方法。

其中:

protected abstract T loadDataBean(ResultSet rs, String viewName) throws SQLException;

protected abstract T loadDataBeanSelf(ResultSet rs) throws SQLException;

这2个方法是SqlHelper.java里留出来了,就是为了大家可以自己封装实体javabean来用

Java代码 复制代码
  1. package  com.yd.dao;   
  2.   
  3. import  java.sql.*;   
  4. import  java.util.*;   
  5.   
  6. import  com.yd.db.DBConnPoolMgr;   
  7. import  com.yd.idao.ISqlHelper;   
  8. import  com.yd.support.JDataSet;   
  9.   
  10. public   abstract   class  SqlHelper<T>  implements  ISqlHelper<T> {   
  11.   
  12.      protected  java.sql.Connection conn =  null ;   
  13.   
  14.      protected  java.sql.PreparedStatement pst =  null ;   
  15.   
  16.      protected  java.sql.Statement st =  null ;   
  17.   
  18.      protected  java.sql.CallableStatement cs =  null ;   
  19.   
  20.      protected  java.sql.ResultSet rs =  null ;   
  21.   
  22.      protected  java.sql.ResultSetMetaData rm =  null ;   
  23.        
  24.      public  Connection returnConn()  throws  SQLException {   
  25.   
  26.                    //DBConnPoolMgr是自己写的一个简单连接池类,用来得到连接,我后面会给出这个类的代码   
  27.          return  (conn = DBConnPoolMgr.getInctence().getConnect());   
  28.     }   
  29.        
  30.      private  PreparedStatement returnPst(String sql, Object[] params)  throws  SQLException {   
  31.   
  32.          if  (conn ==  null  || conn.isClosed()) conn = returnConn();   
  33.         pst = conn.prepareStatement(sql);   
  34.          if  (params !=  null )   
  35.              for  ( int  i =  0 ; i < params.length; i++)   
  36.                 pst.setObject(i +  1 , params[i]);   
  37.          return  pst;   
  38.            
  39.     }   
  40.   
  41.      protected  CallableStatement returnCs(String procName, Object[] params)  throws  SQLException {   
  42.   
  43.          if  (conn ==  null  || conn.isClosed()) conn = returnConn();   
  44.         String call =  "" ;   
  45.          if  (params !=  null ) {   
  46.             call =  "{call "  + procName +  "(" ;   
  47.              for  ( int  c =  0 ; c < params.length -  1 ; c++)   
  48.                 call +=  "?," ;   
  49.             call +=  "?)}" ;   
  50.         }  else   
  51.             call =  "{call "  + procName +  "()}" ;   
  52.         cs = conn.prepareCall(call);   
  53.          if  (params !=  null )   
  54.              for  ( int  i =  0 ; i < params.length; i++)   
  55.                 cs.setObject(i +  1 , params[i]);   
  56.          return  cs;   
  57.            
  58.     }   
  59.   
  60.      public   void  clearAllsql() {   
  61.          try     
  62.         {    
  63.              if  (rs !=  null ) rs.close();   
  64.              if  (cs !=  null ) cs.close();   
  65.              if  (st !=  null ) st.close();   
  66.              if  (pst !=  null ) pst.close();   
  67.              if  (conn !=  null ) {   
  68.                 DBConnPoolMgr.getInctence().returnConnect(conn);   
  69.             }   
  70.             rs =  null ;   
  71.             cs =  null ;   
  72.             st =  null ;   
  73.             pst =  null ;   
  74.         }    
  75.          catch  (SQLException ex) { ex.printStackTrace(); }   
  76.     }   
  77.   
  78.      public   boolean  execute(String sql, Object[] params,  boolean  only) {   
  79.            
  80.          boolean  bVal =  false ;   
  81.          try     
  82.         {   
  83.              if  (only) {   
  84.                 rs = returnPst(sql, params).executeQuery();   
  85.                  while  (rs.next()) bVal =  true ;   
  86.             }  else  {   
  87.                 returnPst(sql, params).executeUpdate();   
  88.                 bVal =  true ;   
  89.             }   
  90.         }    
  91.          catch  (SQLException ex) { ex.printStackTrace(); }    
  92.          finally  { clearAllsql(); }   
  93.          return  bVal;   
  94.     }   
  95.   
  96.      public   boolean  executeBatch(String[] sqlBatch) {   
  97.            
  98.          boolean  bVal =  false ;   
  99.          try     
  100.         {   
  101.             conn = returnConn();   
  102.             st = conn.createStatement();   
  103.              boolean  autoCommit = conn.getAutoCommit();   
  104.   
  105.              for  ( int  i =  0 ; i < sqlBatch.length; i++) {   
  106.                  if  (sqlBatch[i] !=  null  && !sqlBatch[i].equals( "" ))   
  107.                     st.addBatch(sqlBatch[i] +  ";" );   
  108.             }   
  109.             conn.setAutoCommit( false );   
  110.             st.executeBatch();   
  111.             conn.commit();   
  112.             conn.setAutoCommit(autoCommit);   
  113.             bVal =  true ;   
  114.         }    
  115.          catch  (SQLException ex) {   
  116.              try  { conn.rollback(); }  catch  (SQLException e) { e.printStackTrace(); }   
  117.             ex.printStackTrace();   
  118.         }  finally  { clearAllsql(); }   
  119.          return  bVal;   
  120.     }   
  121.   
  122.      public   boolean  executeCall(String procName, Object[] params) {   
  123.            
  124.          boolean  bVal =  false ;   
  125.          try     
  126.         {   
  127.             returnCs(procName, params).executeUpdate();   
  128.             bVal =  true ;   
  129.         }    
  130.          catch  (Exception ex) { ex.printStackTrace(); }    
  131.          finally  { clearAllsql(); }   
  132.          return  bVal;   
  133.     }   
  134.   
  135.      public  T find(String sql, Object[] params, String viewName,  boolean  executeCall) {   
  136.            
  137.         T t =  null ;   
  138.          try     
  139.         {   
  140.              if  (executeCall) rs = returnCs(sql, params).executeQuery();   
  141.              else  rs = returnPst(sql, params).executeQuery();   
  142.             t = loadResultSet(rs, viewName);   
  143.         }    
  144.          catch  (Exception ex) { ex.printStackTrace(); }    
  145.          finally  { clearAllsql(); }   
  146.          return  t;   
  147.     }   
  148.   
  149.      public  List<T> findList(String sql, Object[] params, String viewName,  boolean  executeCall) {   
  150.            
  151.         List<T> lt =  null ;   
  152.          try     
  153.         {   
  154.              if  (executeCall) rs = returnCs(sql, params).executeQuery();   
  155.              else  rs = returnPst(sql, params).executeQuery();   
  156.             lt = loadList(rs, viewName);   
  157.         }    
  158.          catch  (Exception ex) { ex.printStackTrace(); }    
  159.          finally  { clearAllsql(); }   
  160.          return  lt;   
  161.     }   
  162.   
  163.      public  ResultSet returnResultSet(String sql, Object[] params,  boolean  executeCall) {   
  164.          try     
  165.         {   
  166.              if  (executeCall) rs = returnCs(sql, params).executeQuery();   
  167.              else  rs = returnPst(sql, params).executeQuery();   
  168.         }    
  169.          catch  (Exception ex) { ex.printStackTrace(); }   
  170.          return  rs;   
  171.     }   
  172.   
  173.      private  T loadResultSet(ResultSet rs, String viewName)  throws  SQLException {   
  174.            
  175.         T t =  null ;   
  176.          if  (rs !=  null while  (rs.next()) t = loadDataBean(rs, viewName);   
  177.          return  t;   
  178.     }   
  179.   
  180.      private  List<T> loadList(ResultSet rs, String viewName)  throws  SQLException {   
  181.            
  182.         List<T> tlist =  new  ArrayList<T>();   
  183.          if  (rs !=  null )    
  184.              while  (rs.next())    
  185.                 tlist.add(loadDataBean(rs, viewName));   
  186.          return  tlist;   
  187.     }   
  188.   
  189.      public  String changeFont(ResultSet rs, String column)  throws  SQLException {   
  190.          return  rs.getString(column) ==  null  ?  ""  : rs.getString(column);   
  191.     }   
  192.   
  193.      public   int  returnColumnCount(ResultSet rs)  throws  SQLException {   
  194.          return  rs.getMetaData().getColumnCount();   
  195.     }   
  196.   
  197.           //两个非常关键的模版方法,继承此类的操作类都要实现这2个方法,我到时候会给出操作类   
  198.      protected   abstract  T loadDataBean(ResultSet rs, String viewName)  throws  SQLException;   
  199.   
  200.      protected   abstract  T loadDataBeanSelf(ResultSet rs)  throws  SQLException;   
  201.   
  202.      public  List<T> findListAsPager(String sql, String orderby,  int  currentPage,  int  pageSize) {   
  203.            
  204.         List<T> lt =  null ;   
  205.          try     
  206.         {   
  207.             String strVal = strPager(sql, orderby, currentPage, pageSize);   
  208.             rs = returnPst(strVal,  null ).executeQuery();   
  209.             lt = loadList(rs,  "" );   
  210.         }    
  211.          catch  (SQLException ex) { ex.printStackTrace(); }    
  212.          finally  { clearAllsql(); }   
  213.          return  lt;   
  214.     }   
  215.   
  216.           //因为用的sql server 2008所以只写了这个数据库的分页   
  217.      private  String strPager(String sql, String orderby,  int  currentPage,  int  pageSize) {   
  218.            
  219.          int  start =  1 ;   
  220.          if  (currentPage >  1 ) start = (currentPage - 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值