`
peizhiinfo
  • 浏览: 1422265 次
文章分类
社区版块
存档分类
最新评论

【郭林专刊】万能 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. }


  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. }


还能改进,,,但是已经觉得比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. }


  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. }


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

分享到:
评论

相关推荐

    单分散性钴铁氧体纳米粒子的研究进展

    单分散性钴铁氧体纳米粒子的研究进展,王志成,郭林,钴铁氧体纳米磁性粒子具有良好的物理化学性能,因此其相关合成备受关注。本文总结了钴铁氧体纳米粒子的制备方法,如化学共沉淀法

    小尺寸、单分散Cd

    采用改进高温热分解法合成了Cd2+掺杂NaLuF4∶Yb, Er纳米晶体,研究了Cd2+对晶相...通过二氧化硅壳层的包覆,降低了Cd2+泄露引起的毒性反应,表明在浓度较低时呈现出低毒性,满足了上转换纳米晶在医用材料领域应用的要求。

    Synthesis of Ni Net-like Nanomaterials and Their Magnetic Properties

    有机层包裹的均匀镍纳米颗粒的合成及其催化性质的研究,周苇,郭林,利用温和的湿化学法合成了平均粒径分别为100,150及200 nm的有机层包裹的均匀镍纳米颗粒。热重测试表明表层有机层能有效防止镍颗粒被

    Android大屏幕适配demo

    Android大屏幕适配demo,这个是仿照郭林文章中的例子,他的例子是用eclipse写的,直接运行会不兼容最新的sdk,我改成了AndroidStudio可以直接运行的demo.郭林此代码文章的连接 ...

    学习见证消息推送时刻笔记

    学习郭林老师的见证消息推送时刻笔记,笔记未整理有点乱,有很多不足,希望大家指点,源代码随后上传

    VolleyDemo

    这个是参考网上的资料,结合google官方的文档写的一个volley的简单demo,并做了简单的封装,想更加系统的学习volley可以参考google官方文档以及郭林的博客

    Android photoWallDemo

    仿照郭林博客http://blog.csdn.net/guolin_blog/article/details/34093441写的demo,androidStudio版本,他下载的链接是eclipse的代码,在高版本的AndroidStudio运行会不方便

    论文研究-基于评论对象的新闻影响力预测算法 .pdf

    基于评论对象的新闻影响力预测算法,魏忠祥,郭林,近年来网络媒体对社会的影响力越来越大,能够定量地分析预测新闻事件的影响力显得尤为重要。目前一些研究都只是针对新闻的点击量

    温度应力下海底管线水平屈曲分析

    温度应力下海底管线水平屈曲分析,刘润,郭林坪,温度应力下海底管线的屈曲大变形是管线设计中所要考虑的关键问题之一。以某实际工程为背景,应用理想平直管线发生水平向屈曲的解

    论文研究-小波去噪的低信噪比直扩信号扩频码盲估计算法 .pdf

    小波去噪的低信噪比直扩信号扩频码盲估计算法,郭林,吕明,本文针对低信噪比直扩信号扩频码的盲估计问题,提出了利用小波降噪的方法对直扩信号进行预处理,然后再分别运用特征值分解算法、

    PVP高分子包覆的镍纳米材料的形态控制研究

    PVP高分子包覆的镍纳米材料的形态控制研究,周苇,郭林,本文报道了辅以PVP(聚乙烯吡咯烷酮)修饰水合肼还原二价镍盐制备镍的纳米材料。通过改变镍盐与PVP的摩尔浓度比、回流温度、盐的种

    Android布局ListView下拉刷新demo

    android中数据的更新需要用户很方便就能操作,其中下拉刷新就是很好的一种用户体验方式,这是郭林大神在网上的一个下拉刷新的demo,我做了一点小小的修改,然后加了一下注释,方便大家参考。

    八面体中空银纳米笼的高效合成及其表面增强拉曼性能研究

    八面体中空银纳米笼的高效合成及其表面增强拉曼性能研究,黄凤琴,郭林,以八面体Cu2O为模板,柠檬酸钠作为保护剂,NaBH4和AgNO3反应产生的银纳米颗粒在模板表面沉积形成银包覆氧化亚铜的核壳结构,最后用乙�

Global site tag (gtag.js) - Google Analytics