最近在翻阅springboot官方文档时,看见官方文档中有推荐一款ORM框架——jOOQ,可能很多朋友和我一样都没有用过这款框架,于是百度了一下,发现用过的朋友的都说它在代码层面比Mybatis简洁得多,而且性能也非常优异,抱着学习的态度,通过查询相关资料,在本地写了一个demo工程,体验了一下,在此记录一下。demo基于springboot 2.2.0,jooq相关组件版本为3.12.1

创建springboot工程,引入jooq的starter

pom.xml

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jooq</artifactId></dependency>

引入jooq插件依赖

<dependency><groupId>org.jooq</groupId><artifactId>jooq-meta</artifactId></dependency><dependency><groupId>org.jooq</groupId><artifactId>jooq-codegen</artifactId></dependency>

plugin配置

<plugin><groupId>org.jooq</groupId><artifactId>jooq-codegen-maven</artifactId><version>${jooq.version}</version><executions><execution><goals><goal>generate</goal></goals></execution></executions><dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency></dependencies><configuration><configurationFile>src/main/resources/JooqConfig.xml</configurationFile></configuration>
</plugin>

在工程的src/main/resources目录下创建JooqConfig.xml文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration><jdbc><driver>com.mysql.jdbc.Driver</driver><!-- 配置数据库地址 --><url>jdbc:mysql://127.0.0.1:3306/students?characterEncoding=UTF-8</url><!-- 配置数据库用户名--><user>root</user><!-- 配置数据库密码--><password>1234</password></jdbc><generator><!-- 代码生成器 --><!--   <name>org.jooq.meta.mysql.MySQLDatabase</name>--><database><!--强制为scheme1模式下所有的含有id域生成id--><!--是否重写主键--><name>org.jooq.meta.mysql.MySQLDatabase</name><!--include和exclude用于控制为数据库中哪些表生成代码--><includes>.*</includes><excludes></excludes><!--数据库名称--><inputSchema>students</inputSchema></database><generate><!--是否生成dao和pojo--><daos>true</daos><pojos>true</pojos><!--是否把数据库时间类型映射到java 8时间类型--><javaTimeTypes>true</javaTimeTypes><!--<interfaces>true</interfaces>--><!--是否在生成的代码中添加spring注释,比如@Repository--><springAnnotations>false</springAnnotations></generate><target><!--生成代码文件的包名及放置目录--><packageName>com.twp.spring.jooq</packageName><directory>src/main/java</directory></target></generator>
</configuration>

在数据库创建表,完成之后,启动jooq-codegen插件

CREATE TABLE celebrity (id INT NOT NULL,first_name VARCHAR(50),last_name VARCHAR(50) NOT NULL,date_of_birth DATE,birth_place VARCHAR(50),PRIMARY KEY (`id`)
)ENGINE=INNODB CHARSET=utf8;

执行之后,会在项目目录中生成如下代码,其中会有两个以表名命名的类,一个代表数据的表,一个代表实体类

代码生成后,下面尝试实现简单的CRUD功能,其他的复杂查询后续再摸索

public interface CelebrityService {int createCelebrity(Celebrity celebrity);List<Celebrity> findAll();int updateCelebrity(Celebrity celebrity);int deleteCelebrity(int  id);
}
@Service
public class CelebrityServiceImpl implements CelebrityService {private final DSLContext dslContext;@Autowiredpublic CelebrityServiceImpl(DSLContext dslContext){this.dslContext = dslContext;}@Overridepublic int createCelebrity(Celebrity celebrity) {com.twp.spring.jooq.tables.Celebrity celeTbl = new com.twp.spring.jooq.tables.Celebrity();int execute = dslContext.insertInto(celeTbl).columns(celeTbl.ID, celeTbl.FIRST_NAME, celeTbl.LAST_NAME, celeTbl.DATE_OF_BIRTH, celeTbl.BIRTH_PLACE).values(celebrity.getId(), celebrity.getFirstName(), celebrity.getLastName(), celebrity.getDateOfBirth(), celebrity.getBirthPlace()).execute();return execute;}@Overridepublic List<Celebrity> findAll() {com.twp.spring.jooq.tables.Celebrity celeTbl = new com.twp.spring.jooq.tables.Celebrity();List<Celebrity> celebrities = dslContext.select().from(celeTbl).fetchInto(Celebrity.class);return celebrities;}@Overridepublic int updateCelebrity(Celebrity celebrity) {com.twp.spring.jooq.tables.Celebrity celeTbl = new com.twp.spring.jooq.tables.Celebrity();int execute = dslContext.update(celeTbl).set(celeTbl.FIRST_NAME,celebrity.getFirstName()).set(celeTbl.LAST_NAME,celebrity.getLastName()).set(celeTbl.DATE_OF_BIRTH,celebrity.getDateOfBirth()).set(celeTbl.BIRTH_PLACE,celebrity.getBirthPlace()).set(celeTbl.BIRTH_PLACE, celebrity.getBirthPlace()).where(celeTbl.ID.equal(celebrity.getId())).execute();return execute;}@Overridepublic int deleteCelebrity(int id) {com.twp.spring.jooq.tables.Celebrity celeTbl = new com.twp.spring.jooq.tables.Celebrity();int execute = dslContext.delete(celeTbl).where(celeTbl.ID.equal(id)).execute();return execute;}
}

模拟数据进行测试

@SpringBootTest
@RunWith(SpringRunner.class)
public class SpringJooqApplicationTests {@Autowiredprivate CelebrityService service;@Testpublic void createTest(){Celebrity celebrity1 = new Celebrity(1,"张","爱玲", LocalDate.of(1920,9,30),"上海");Celebrity celebrity2 = new Celebrity(2,"鲁","迅", LocalDate.of(1881,9,25),"浙江绍兴");Celebrity celebrity3 = new Celebrity(3,"老","舍", LocalDate.of(1899,2,3),"北京");Celebrity celebrity4 = new Celebrity(4,"巴","金", LocalDate.of(1904,11,25),"四川成都");List<Celebrity> celebrityList = new ArrayList<Celebrity>(){{add(celebrity1);add(celebrity2);add(celebrity3);add(celebrity4);}};celebrityList.forEach((cele)->{int create = service.createCelebrity(cele);System.out.println("create:"+create);});}@Testpublic void retrievallTest(){service.findAll().forEach((cele)->{System.out.println("celebrity:" + cele.toString());});}@Testpublic void updateTest(){Celebrity celebrity = new Celebrity(2,"鲁","迅", LocalDate.of(1881,9,25),"浙江省绍兴府会稽县");int update = service.updateCelebrity(celebrity);System.out.println("update:" + update);}@Testpublic void deleteTest(){int i = service.deleteCelebrity(1);System.out.println("delete:" + i);}
}

新增:

查询:

更新:

删除:

jOOQ支持很多的复杂查询功能,在编写代码时只需以面向对象编程的语法组装查询条件,代码可读性更高,操作也比较简单,配置较少,很大程度上可以提高开发效率。

Springboot 2.x 整合jOOQ实现CRUD相关推荐

  1. springboot 和 mybatis整合:参数查询和动态sql

    springboot 和 mybatis整合: mapper定义的是数据库的操作方法: @Mapper public interface UserMapper {} 单参数的处理: @Select(& ...

  2. 三、基于SpringBoot实现SSMP整合

    三.基于SpringBoot实现SSMP整合 三.基于SpringBoot实现SSMP整合 1.整合JUnit 2.整合MyBatis 3.整合MyBatis-Plus 4.整合Druid 5.SSM ...

  3. 整合MGB实现CRUD以及各种姿势查询

    整合MGB实现CRUD以及各种姿势查询 1.依赖 1.1.pom.xml依赖 <?xml version="1.0" encoding="UTF-8"?& ...

  4. SpringBoot 实战 (九) | 整合 Mybatis

    微信公众号:一个优秀的废人 如有问题或建议,请后台留言,我会尽力解决你的问题. 前言 如题,今天介绍 SpringBoot 与 Mybatis 的整合以及 Mybatis 的使用,本文通过注解的形式实 ...

  5. Springboot与Ajax整合练习?

    Springboot与Ajax整合练习? RunApp package cn.tedu;import org.springframework.boot.SpringApplication; impor ...

  6. SpringBoot实战:整合Redis、mybatis,封装RedisUtils工具类等(附源码)

    创建SpringBoot项目 在线创建方式 网址:https://start.spring.io/ 然后创建Controller.Mapper.Service包 SpringBoot整合Redis 引 ...

  7. springboot+security框架整合

    springboot+security框架整合 springboot项目搭建大家可以去请教度娘,有很多文章,这里主要讲解springboot和security安全框架的集成,因为springmvc跟s ...

  8. 玩转 SpringBoot 2 之整合定时任务篇

    前言 通过本文你将了解到如何在 SpringBoot 2 中整合定时任务使用教程,具体详细内容如下: SpringBoot 自带定时任务使用教程 SpringBoot 集成 JDK 定时任务使用教程 ...

  9. 玩转 SpringBoot 2 之整合 JWT 下篇

    前言 在<玩转 SpringBoot 2 之整合 JWT 上篇> 中介绍了关于 JWT 相关概念和JWT 基本使用的操作方式.本文为 SpringBoot 整合 JWT 的下篇,通过解决 ...

最新文章

  1. centos6.5编译安装php7,及配置与nginx通信。
  2. 第四百一十四节,python常用算法学习
  3. 简单的GTK窗体搭建
  4. V1.8 - 2006.09.09
  5. BZOJ1007:[HNOI2008]水平可见直线(计算几何)
  6. Qt 5.9.1 连 MYSQL 5.7数据库
  7. Java来做马里奥[0]—让精灵再次舞动
  8. Atitit 知识聚合的方法大总结 目录 1. 什么是聚合 汇聚 1 2. 聚合化应用场景 2 2.1. 一站式 2 3. 知识聚合的历史与趋势
  9. 软件中反跟踪技术和软件调试
  10. Android的深度定制版阿里云os(Android的山寨)
  11. SlickEdit基本设置
  12. 矩阵顺时针逆时针旋转90°
  13. 【黑金原创教程】【TimeQuest】【第五章】网表质量与外部模型
  14. openwrt 挂载硬盘NFS共享,非SMB共享
  15. 封装:el-upload上传图片组件(解决图片闪动、多选问题)
  16. 计算机中丢失rtutil,api-ms-win-core-winrt-string-l1-1-0.dll从您的计算机中丢失
  17. 布隆过滤器(Bloom Filter)
  18. 如何设置Windows文件夹背景为黑色?(其实就是“深色模式”)
  19. 分布式架构设计中的CAP原理
  20. 简单 python 爬虫(一)

热门文章

  1. Unity动态创建材质球
  2. 【Verilog零基础入门-边看边练】学习笔记——第三讲 组合逻辑代码设计和仿真(补码转换和七段译码逻辑设计)(二)
  3. Fluent中的网格自适应技术
  4. 航空发动机-网格技术篇1之叶栅流域正交画法
  5. R语言设置数值输出(保留至小数点后位数和保留有效数字)
  6. 注册表 Run、RunOnce 键值解析
  7. (学习总结)鸟哥基础篇第三版:第二十四章
  8. 央行提醒:远离“征信修复”骗局
  9. 基于Vue的前端权限管理
  10. __thiscall 转 __cdecl 时的问题,关于函数指针