标签:blank rom tin gui target blog 总结 his value
序言
上一篇关于JOOQ的随笔介绍了Springboot项目的创建以及JOOQ 代码生成器的使用。
那么在上次的项目基础上来看看实际的JOOQ整么使用。
准备工作
先将项目的目录结构创建好
接下来CRUD的语法这篇文章会分别去写一个示例,看完之后基础的增删查改不在话下。
查询(select)
1 /** 2 * 查询 3 * @return 4 */ 5 public Result<BkUserRecord> getAll(){ 6 Result<BkUserRecord> result = this.create 7 .selectFrom(BkUser.BK_USER) 8 .orderBy(1) 9 .fetch(); 10 return result; 11 }
更新(update)
1 /** 2 * 更新 3 */ 4 public void updateUser(){ 5 // 设置别名 6 BkUser a = BkUser.BK_USER.as("a"); 7 BkUserRecord user = new BkUserRecord(); 8 this.create 9 .update(a) 10 .set(a.ADDRESS,user.getAddress()) 11 .set(a.NAME,user.getName()) 12 .where(a.ID.equal(user.getId())) 13 .execute(); 14 }
删除(delete)
1 /** 2 * 删除 3 * @param userId 4 */ 5 public void deleteUserById(Integer userId){ 6 this.create 7 .deleteFrom(BkUser.BK_USER) 8 .where(BkUser.BK_USER.ID.eq(userId)) 9 .execute(); 10 }
新增(insert)
/** * 新增 */ public void addUser(){ byte enabled = 1; this.create .insertInto(BkUser.BK_USER) .columns(BkUser.BK_USER.ADDRESS, BkUser.BK_USER.ENABLED, BkUser.BK_USER.NAME, BkUser.BK_USER.PHONE, BkUser.BK_USER.PASSWORD) .values("后海",enabled,"小毛","10086","123456") .execute(); }
完整代码
1 package com.bkn.breakingnews.modules.user.dao; 2 3 import com.bkn.breakingnews.model.tables.BkUser; 4 import com.bkn.breakingnews.model.tables.records.BkUserRecord; 5 import com.zaxxer.hikari.HikariDataSource; 6 import org.jooq.DSLContext; 7 import org.jooq.Result; 8 import org.jooq.SQLDialect; 9 import org.jooq.impl.DSL; 10 import org.springframework.beans.factory.annotation.Autowired; 11 import org.springframework.stereotype.Repository; 12 13 /** 14 * @author wuqi 15 * @description user DAO 16 * @date 2020/5/15 14:52 17 */ 18 @Repository 19 public class UserDao { 20 // 注入Hikari的DataSource, 21 @Autowired 22 HikariDataSource dataSource; 23 24 /** 25 * 查询 26 * @return 27 */ 28 public Result<BkUserRecord> getAll(){ 29 // DSL上下文是所有数据库操作的入口,意思就是要用jooq干任何事都要先实例化这个DSLContext 30 DSLContext create = DSL.using(this.dataSource,SQLDialect.MYSQL); 31 Result<BkUserRecord> result = create 32 .selectFrom(BkUser.BK_USER) 33 .orderBy(1) 34 .fetch(); 35 return result; 36 } 37 /** 38 * 更新 39 */ 40 public void updateUser(){ 41 // 设置别名 42 BkUser a = BkUser.BK_USER.as("a"); 43 BkUserRecord user = new BkUserRecord(); 44 user.setId(5); 45 user.setAddress("印度"); 46 user.setName("阿三哥"); 47 DSL.using(this.dataSource,SQLDialect.MYSQL) 48 .update(a) 49 .set(a.ADDRESS,user.getAddress()) 50 .set(a.NAME,user.getName()) 51 .where(a.ID.equal(user.getId())) 52 .execute(); 53 } 54 55 /** 56 * 删除 57 * @param userId 58 */ 59 public void deleteUserById(Integer userId){ 60 DSL.using(this.dataSource,SQLDialect.MYSQL) 61 .deleteFrom(BkUser.BK_USER) 62 .where(BkUser.BK_USER.ID.eq(userId)) 63 .execute(); 64 } 65 66 /** 67 * 新增 68 */ 69 public void addUser(){ 70 byte enabled = 1; 71 DSL.using(this.dataSource,SQLDialect.MYSQL) 72 .insertInto(BkUser.BK_USER) 73 .columns(BkUser.BK_USER.ADDRESS, 74 BkUser.BK_USER.ENABLED, 75 BkUser.BK_USER.NAME, 76 BkUser.BK_USER.PHONE, 77 BkUser.BK_USER.PASSWORD) 78 .values("后海",enabled,"小毛","10086","123456") 79 .execute(); 80 } 81 82 }
测试类代码
1 package com.bkn.breakingnews; 2 3 import com.bkn.breakingnews.model.tables.BkUser; 4 import com.bkn.breakingnews.model.tables.records.BkUserRecord; 5 import com.bkn.breakingnews.modules.user.dao.UserDao; 6 import com.zaxxer.hikari.HikariDataSource; 7 import org.jooq.DSLContext; 8 import org.jooq.Result; 9 import org.jooq.SQLDialect; 10 import org.jooq.impl.DSL; 11 import org.junit.jupiter.api.Test; 12 import org.springframework.beans.factory.annotation.Autowired; 13 import org.springframework.boot.test.context.SpringBootTest; 14 15 @SpringBootTest 16 class BreakingnewsApplicationTests { 17 18 @Autowired 19 HikariDataSource dataSource; 20 21 @Test 22 void contextLoads() { 23 DSLContext create = DSL.using(dataSource, SQLDialect.MYSQL); 24 Result<BkUserRecord> records = create.selectFrom(BkUser.BK_USER).fetch(); 25 System.out.println(records); 26 } 27 28 //将userDao注入进单元测试类 29 @Autowired 30 UserDao userDao; 31 32 @Test 33 void testUser(){ 34 System.out.println("查询---"); 35 System.out.println(userDao.getAll()); 36 System.out.println("查询---"); 37 38 System.out.println("更新---"); 39 userDao.updateUser(); 40 System.out.println("更新后---"); 41 System.out.println(userDao.getAll()); 42 43 System.out.println("新增---"); 44 userDao.addUser(); 45 System.out.println("新增后---"); 46 System.out.println(userDao.getAll()); 47 48 System.out.println("删除---"); 49 userDao.deleteUserById(5); 50 System.out.println("删除---"); 51 System.out.println(userDao.getAll()); 52 53 } 54 55 }
测试结果
查询--- 更新--- 更新后--- +----+-----+-----------+------------+-------+-------+--------+--------------------------------------------------+------+ | id|name |phone |telephone |address|enabled|username|password |remark| +----+-----+-----------+------------+-------+-------+--------+--------------------------------------------------+------+ | 3|系统管理员|18568887789|029-82881234|深圳南山 | 1|admin |$2a$10$ySG2lkvjFHY5O0./CPIE1OI8VJsuKYEzOYzqIa7A...|{null}| | 5|阿三哥 |18568123489|029-82123434|印度 | 1|libai |$2a$10$oE39aG10kB/rFu2vQeCJTu/V/v4n6DRR0f8WyXRi...|{null}| +----+-----+-----------+------------+-------+-------+--------+--------------------------------------------------+------+ 新增--- 新增后--- +----+-----+-----------+------------+-------+-------+--------+--------------------------------------------------+------+ | id|name |phone |telephone |address|enabled|username|password |remark| +----+-----+-----------+------------+-------+-------+--------+--------------------------------------------------+------+ | 3|系统管理员|18568887789|029-82881234|深圳南山 | 1|admin |$2a$10$ySG2lkvjFHY5O0./CPIE1OI8VJsuKYEzOYzqIa7A...|{null}| | 5|阿三哥 |18568123489|029-82123434|印度 | 1|libai |$2a$10$oE39aG10kB/rFu2vQeCJTu/V/v4n6DRR0f8WyXRi...|{null}| | 6|小毛 |10086 |{null} |后海 | 1|{null} |123456 |{null}| +----+-----+-----------+------------+-------+-------+--------+--------------------------------------------------+------+ 删除--- 删除--- +----+-----+-----------+------------+-------+-------+--------+--------------------------------------------------+------+ | id|name |phone |telephone |address|enabled|username|password |remark| +----+-----+-----------+------------+-------+-------+--------+--------------------------------------------------+------+ | 3|系统管理员|18568887789|029-82881234|深圳南山 | 1|admin |$2a$10$ySG2lkvjFHY5O0./CPIE1OI8VJsuKYEzOYzqIa7A...|{null}| | 6|小毛 |10086 |{null} |后海 | 1|{null} |123456 |{null}| +----+-----+-----------+------------+-------+-------+--------+--------------------------------------------------+------+
总结
a.从代码中可以看出,java代码非常的简洁(别杠说这是因为sql逻辑简单),和Mybatis相比的话省去了配置Mapper.xml的步骤,
这一点还是比较吸引人的。
b.示例中用到的方法fetch()和execute()按照字面意思理解就可以
c.DSL上下文DSLContext就像一个入口,所有的sql相关操作都需要在上线文上执行,所以用之前都要进行实例化
d.where..and..or以及join的用法和增删改查一样都比较简单,一看就会,这里不做多介绍。写CRUD示例主要是能够让脑海中对JOOQ的语句有个印象。
e.下一篇文章会通过相对CRUD复杂一点的例子来讲讲比较常用的field()方法、with语句的用法。
标签:blank rom tin gui target blog 总结 his value
原文地址:https://www.cnblogs.com/chaojizhengui/p/jooq_crud.html