JOOQ初学-简单的增删改查demo

http://blog.csdn.net/u012540337/article/details/48263717

初学JOOQ,写个blog为了mark一下,也方便大家交流。直接上代码了。在网上搜不到太详细的demo和文档,都是英文的。哎,忧桑、、、在这里写几个demo,大家看看,有不足望指教。

初步的数据库连接,在这里我用了bonecp连接池来管理

[java]  view plain  copy
  1. import java.sql.Connection;  
  2. import java.sql.SQLException;  
  3.   
  4. import org.jooq.DSLContext;  
  5. import org.jooq.impl.DSL;  
  6.   
  7. import com.jolbox.bonecp.BoneCP;  
  8. import com.jolbox.bonecp.BoneCPConfig;  
  9. /** 
  10.  *  
  11.  * @author zhoudong 
  12.  * WinterChou连接池管理数据库连接 
  13.  */  
  14. public class BoneCpPool {  
  15.   
  16.     private static BoneCP boneCp = null;  
  17.     private static BoneCPConfig boneCPConfig = null;  
  18.     // 静态代码块加载配置文件  
  19.     static {  
  20.         // 加载JDBC驱动  
  21.         try {  
  22.             Class.forName("com.mysql.jdbc.Driver");// 注册数据库  
  23.             boneCPConfig = new BoneCPConfig();// bonecp数据库连接池配置  
  24.             String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/wish";// jdbc:mysql://10.10.0.215:3306/che001  
  25.             boneCPConfig.setJdbcUrl(jdbcUrl);  
  26.             boneCPConfig.setUser("root");  
  27.             boneCPConfig.setPassword("tiger");  
  28.             // 数据库连接池的最小连接数  
  29.             boneCPConfig.setMinConnectionsPerPartition(5);  
  30.             // 数据库连接池的最大连接数  
  31.             boneCPConfig.setMaxConnectionsPerPartition(10);  
  32.             boneCPConfig.setPartitionCount(1);  
  33.             // System.out.println("boneCPConfig"+boneCPConfig);  
  34.         } catch (ClassNotFoundException e) {  
  35.             e.printStackTrace();  
  36.         }  
  37.     }  
  38.   
  39.     // 获取连接池  
  40.     public static BoneCP getBoneCP() {  
  41.         try {  
  42.             boneCp = new BoneCP(boneCPConfig);  
  43.         } catch (SQLException e) {  
  44.             // TODO Auto-generated catch block  
  45.             e.printStackTrace();  
  46.         }  
  47.         return boneCp;  
  48.     }  
  49.   
  50.     // 获取连接  
  51.     public static Connection getConnection(BoneCP boneCpP) {  
  52.         if (boneCpP != null) {  
  53.             try {  
  54.                 return boneCpP.getConnection();  
  55.             } catch (SQLException e) {  
  56.                 return null;  
  57.             }  
  58.         } else {  
  59.             return null;  
  60.         }  
  61.     }  
  62.   
  63.     // 关闭连接池  
  64.     public static void closeBoneCP(BoneCP bc) {  
  65.         bc.close();  
  66.     }  
  67.   
  68.     // 关闭连接  
  69.     public static void closeConnection(Connection con) throws SQLException {  
  70.         con.close();  
  71.     }  
  72.   
  73.     //  
  74.     public static DSLContext getContext(Connection conDsl) {  
  75.         return DSL.using(conDsl);  
  76.     }  
  77. }  
  78. </span>  

简单的初步demo

[java]  view plain  copy
  1. <span style="font-size:18px;">import java.sql.Connection;  
  2.   
  3. import org.jooq.Condition;  
  4. import org.jooq.DSLContext;  
  5. import org.jooq.Record;  
  6. import org.jooq.Result;  
  7. import org.jooq.SelectQuery;  
  8. import org.jooq.Table;  
  9. import org.jooq.UpdateQuery;  
  10. import org.jooq.impl.DSL;  
  11.   
  12. import com.dfb.jooq.datapool.BoneCpPool;  
  13. import com.jolbox.bonecp.BoneCP;  
  14. /** 
  15.  *  
  16.  * @author zhoudong 
  17.  * 简单的增删改查 
  18.  */  
  19. public class JooqDao {  
  20.   
  21.     private DSLContext dslContext= null;  
  22.     //获取DSLContext对象  
  23.     private DSLContext getdslContext()  
  24.     {  
  25.         BoneCP boneCP = BoneCpPool.getBoneCP();  
  26.         Connection connection = BoneCpPool.getConnection(boneCP);  
  27.         dslContext = DSL.using(connection);  
  28.         return dslContext;  
  29.     }  
  30.     //简单实体查询  
  31.     public void select(String add)  
  32.     {  
  33.         DSLContext getdslContext = getdslContext();  
  34.         Table<Record> table = DSL.table("shangfox_user");  
  35.         SelectQuery<Record> selectQuery = getdslContext.selectQuery(table);//获取查询对象  
  36.         Condition eq = DSL.field("username").eq(add);//查询条件  
  37.         selectQuery.addConditions(eq);//添加查询条件  
  38.         Result<Record> fetch = selectQuery.fetch();  
  39.         for (Object aResult : fetch) {  
  40.             Record record = (Record) aResult;  
  41.             System.out.println(record);  
  42.             System.out.println(record.getValue("username"));  
  43.         }  
  44.       }  
  45.     //实体更新  
  46.     public void update(String name)  
  47.     {  
  48.         DSLContext getdslContext = getdslContext();  
  49.         Table<Record> table = DSL.table("shangfox_user");  
  50.         UpdateQuery<Record> updateQuery = getdslContext.updateQuery(table);//获取更新对象  
  51.         updateQuery.addValue(DSL.field("email"), "new-email");//更新email字段的值为new-email  
  52.         Condition eq = DSL.field("username").eq(name);//更新username为name的email字段  
  53.         updateQuery.addConditions(eq);  
  54.         int execute = updateQuery.execute();  
  55.         System.out.println(execute);  
  56.         select("shangfox1");  
  57.     }  
  58.     //原生态的sql查询  
  59.     public void getVal()  
  60.     {  
  61.         DSLContext getdslContext = getdslContext();  
  62.         Table<Record> table = DSL.table("shangfox_wish");//表名  
  63.         Result<Record> fetch = getdslContext.select().from(table).where("statu = 0").and("id > 4340").orderBy(DSL.field("time").asc()).fetch();  
  64.         for (Object aResult : fetch) {  
  65.             Record record = (Record) aResult;  
  66.             System.out.println(record);  
  67.         }  
  68.         /*Map<String, Object> fetchAnyMap = orderBy.fetchAnyMap(); 
  69.         Set<String> keySet = fetchAnyMap.keySet(); 
  70.         for(String s:keySet) 
  71.         { 
  72.             System.out.println("key--"+s+"--val:"+fetchAnyMap.get(s)); 
  73.         }*/  
  74.     }  
  75.     //验证DSL.exists方法  
  76.     public void exits()  
  77.     {  
  78.         DSLContext getdslContext = getdslContext();  
  79.         
  80.         Condition condition = DSL.exists(DSL.select(DSL.field("username1")));  
  81.         Table<Record> table = DSL.table("shangfox_user");  
  82.         SelectQuery<Record> selectQuery = getdslContext.selectQuery(table);  
  83.         selectQuery.addConditions(condition);  
  84.         Result<Record> fetch = selectQuery.fetch();  
  85.         for (Object aResult : fetch) {  
  86.             Record record = (Record) aResult;  
  87.             System.out.println(record);  
  88.             System.out.println(record.getValue("username"));  
  89.         }  
  90.     }  
  91.     public static void main(String[] args) {  
  92.         JooqDao jooqDao = new JooqDao();  
  93. //        jooqDao.select("shangfox");  
  94. //        jooqDao.update("shangfox1");  
  95. //        jooqDao.exits();  
  96.         jooqDao.getVal();  
  97.     }  
  98. }  

demo的jar包下载地址jooq和bonecp的jar

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值