Springboot 2.x 整合jOOQ实现CRUD
最近在翻阅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相关推荐
- springboot 和 mybatis整合:参数查询和动态sql
springboot 和 mybatis整合: mapper定义的是数据库的操作方法: @Mapper public interface UserMapper {} 单参数的处理: @Select(& ...
- 三、基于SpringBoot实现SSMP整合
三.基于SpringBoot实现SSMP整合 三.基于SpringBoot实现SSMP整合 1.整合JUnit 2.整合MyBatis 3.整合MyBatis-Plus 4.整合Druid 5.SSM ...
- 整合MGB实现CRUD以及各种姿势查询
整合MGB实现CRUD以及各种姿势查询 1.依赖 1.1.pom.xml依赖 <?xml version="1.0" encoding="UTF-8"?& ...
- SpringBoot 实战 (九) | 整合 Mybatis
微信公众号:一个优秀的废人 如有问题或建议,请后台留言,我会尽力解决你的问题. 前言 如题,今天介绍 SpringBoot 与 Mybatis 的整合以及 Mybatis 的使用,本文通过注解的形式实 ...
- Springboot与Ajax整合练习?
Springboot与Ajax整合练习? RunApp package cn.tedu;import org.springframework.boot.SpringApplication; impor ...
- SpringBoot实战:整合Redis、mybatis,封装RedisUtils工具类等(附源码)
创建SpringBoot项目 在线创建方式 网址:https://start.spring.io/ 然后创建Controller.Mapper.Service包 SpringBoot整合Redis 引 ...
- springboot+security框架整合
springboot+security框架整合 springboot项目搭建大家可以去请教度娘,有很多文章,这里主要讲解springboot和security安全框架的集成,因为springmvc跟s ...
- 玩转 SpringBoot 2 之整合定时任务篇
前言 通过本文你将了解到如何在 SpringBoot 2 中整合定时任务使用教程,具体详细内容如下: SpringBoot 自带定时任务使用教程 SpringBoot 集成 JDK 定时任务使用教程 ...
- 玩转 SpringBoot 2 之整合 JWT 下篇
前言 在<玩转 SpringBoot 2 之整合 JWT 上篇> 中介绍了关于 JWT 相关概念和JWT 基本使用的操作方式.本文为 SpringBoot 整合 JWT 的下篇,通过解决 ...
最新文章
- centos6.5编译安装php7,及配置与nginx通信。
- 第四百一十四节,python常用算法学习
- 简单的GTK窗体搭建
- V1.8 - 2006.09.09
- BZOJ1007:[HNOI2008]水平可见直线(计算几何)
- Qt 5.9.1 连 MYSQL 5.7数据库
- Java来做马里奥[0]—让精灵再次舞动
- Atitit 知识聚合的方法大总结 目录 1. 什么是聚合 汇聚	1 2. 聚合化应用场景	2 2.1. 一站式	2 3. 知识聚合的历史与趋势
- 软件中反跟踪技术和软件调试
- Android的深度定制版阿里云os(Android的山寨)
- SlickEdit基本设置
- 矩阵顺时针逆时针旋转90°
- 【黑金原创教程】【TimeQuest】【第五章】网表质量与外部模型
- openwrt 挂载硬盘NFS共享,非SMB共享
- 封装:el-upload上传图片组件(解决图片闪动、多选问题)
- 计算机中丢失rtutil,api-ms-win-core-winrt-string-l1-1-0.dll从您的计算机中丢失
- 布隆过滤器(Bloom Filter)
- 如何设置Windows文件夹背景为黑色?(其实就是“深色模式”)
- 分布式架构设计中的CAP原理
- 简单 python 爬虫(一)