【郭林专刊】万能 dao 层

  189人阅读 评论(3) 收藏 举报
  1. package heima.shawn.utils;  
  2.   
  3. import java.beans.Introspector;  
  4. import java.beans.PropertyDescriptor;  
  5. import java.sql.Connection;  
  6. import java.sql.PreparedStatement;  
  7. import java.sql.ResultSet;  
  8. import java.util.ArrayList;  
  9. import java.util.List;  
  10.   
  11. public class BaseDao<T> {  
  12.     public Object opreatorObj(Class<T> tClazz, String sql, Object... params)  
  13.             throws Exception {  
  14.         Connection conn = null;  
  15.         PreparedStatement pst = null;  
  16.         ResultSet rs = null;  
  17.         try {  
  18.             List<T> list = new ArrayList<T>();  
  19.             conn = DBUtils.getInstance().getConnection();  
  20.             pst = conn.prepareStatement(sql);  
  21.             if (params != null) {  
  22.                 for (int i = 1; i <= params.length; i++) {  
  23.                     pst.setObject(i, params[i - 1]);  
  24.                 }  
  25.             }  
  26.             if (pst.execute()) {  
  27.                 rs = pst.getResultSet();  
  28.                 while (rs.next()) {  
  29.                     T t = resultSet2Bean(rs, tClazz);  
  30.                     list.add(t);  
  31.                 }  
  32.                 return list;  
  33.             } else {  
  34.                 int rows = pst.getUpdateCount();  
  35.                 if (rows > 0)  
  36.                     return true;  
  37.                 return false;  
  38.             }  
  39.         } finally {  
  40.             DBUtils.getInstance().release(conn, pst, rs);  
  41.         }  
  42.     }  
  43.   
  44.     public T resultSet2Bean(ResultSet rs, Class<T> clazz) throws Exception {  
  45.         T t = clazz.newInstance();  
  46.         PropertyDescriptor[] props = Introspector.getBeanInfo(clazz)  
  47.                 .getPropertyDescriptors();  
  48.         for (int i = 0; i < props.length; i++) {  
  49.             if (props[i].getName().equals("class"))  
  50.                 continue;  
  51.             props[i].getWriteMethod().invoke(t,  
  52.                     rs.getObject(props[i].getName()));  
  53.         }  
  54.         return t;  
  55.     }  
  56. }  


 

  1. package heima.shawn.utils;  
  2.   
  3. import java.io.IOException;  
  4. import java.sql.Connection;  
  5. import java.sql.DriverManager;  
  6. import java.sql.ResultSet;  
  7. import java.sql.SQLException;  
  8. import java.sql.Statement;  
  9. import java.util.Properties;  
  10.   
  11. import com.mysql.jdbc.Driver;  
  12.   
  13. public class DBUtils {  
  14.     private Properties prop = new Properties();  
  15.   
  16.     private DBUtils() {  
  17.         try {  
  18.             prop.load(DBUtils.class.getClassLoader().getResourceAsStream(  
  19.                     "DB.properties"));  
  20.         } catch (IOException e) {  
  21.             throw new RuntimeException(e);  
  22.         }  
  23.     }  
  24.   
  25.     private static DBUtils instance = new DBUtils();  
  26.   
  27.     public static DBUtils getInstance() {  
  28.         return instance;  
  29.     }  
  30.   
  31.     public Connection getConnection() {  
  32.         Connection conn = null;  
  33.         try {  
  34.             Class.forName(prop.getProperty("driver"));  
  35.             //Driver driver = new Driver();   
  36.             conn = DriverManager.getConnection(prop.getProperty("url"), prop  
  37.                     .getProperty("user"), prop.getProperty("password"));  
  38.         } catch (Exception e) {  
  39.             throw new RuntimeException(e);  
  40.         }  
  41.         return conn;  
  42.     }  
  43.   
  44.     public void release(Connection conn, Statement st, ResultSet rs) {  
  45.         if (rs != null)  
  46.             try {  
  47.                 rs.close();  
  48.             } catch (SQLException e) {  
  49.                 throw new RuntimeException(e);  
  50.             }  
  51.         if (st != null)  
  52.             try {  
  53.                 st.close();  
  54.             } catch (SQLException e) {  
  55.                 throw new RuntimeException(e);  
  56.             }  
  57.         if (conn != null)  
  58.             try {  
  59.                 conn.close();  
  60.             } catch (SQLException e) {  
  61.                 throw new RuntimeException(e);  
  62.             }  
  63.     }  
  64. }  


还能改进,,,但是已经觉得比hibernate好用了!!!你懂的·········

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值