【郭林专刊】万能 dao 层

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


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


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

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


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


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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值