Springboot + 持久层框架JOOQ
简介
JOOQ是一套持久层框架,主要特点是:
- 逆向工程,自动根据数据库结构生成对应的类
- 流式的API,像写SQL一样
- 提供类型安全的SQL查询,JOOQ的主要优势,可以帮助我们在写SQL时就做检查
- 支持几乎所有DDL,DML
- 可以内部避免SQL注入安全问题
- 支持SQL渲染,打印,绑定
- 使用非常轻便灵活
- 可以用JPA做大部分简单的查询,用JOOQ写复杂的
- 可以只用JOOQ作为SQL执行器
- 可以只用来生成SQL语句(类型安全)
- 可以只用来处理SQL执行结果
- 支持Flyway,JAX-RS,JavaFX,Kotlin,Nashorn,Scala,Groovy,NoSQL
- 支持XML,CSV,JSON,HTML导入导出
- 支持事物回滚
Springboot+JOOQ初体验
持久层框架很多,这里参考官网和其他博客用Springboot迅速搭建一个简单demo看看是否好用
配置依赖
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jooq</artifactId>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
</dependencies>
<configuration>
<!--逆向生成配置文件-->
<configurationFile>src/main/resources/library.xml</configurationFile>
<generator>
<generate>
<pojos>true</pojos>
<fluentSetters>true</fluentSetters>
</generate>
</generator>
</configuration>
</plugin>
</plugins>
</build>
application.properties
#datasource
spring.datasource.url=jdbc:mysql://localhost:3306/demo
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
逆向工程
配置文件
在项目的resources目录下新建library.xml,由于网上JOOQ的教程比较少,且比较老,所以建议去官网拷贝对应版本的配置文件,并酌情修改,否则会无法生成。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration xmlns="http://www.jooq.org/xsd/jooq-codegen-3.12.0.xsd">
<!-- Configure the database connection here -->
<jdbc>
<driver>com.mysql.cj.jdbc.Driver</driver>
<url>jdbc:mysql://localhost:3306/demo</url>
<user>root</user>
<password>123456</password>
</jdbc>
<generator>
<!-- The default code generator. You can override this one, to generate your own code style.Supported generators:- org.jooq.codegen.JavaGenerator-org.jooq.codegen.ScalaGenerator Defaults to org.jooq.codegen.JavaGenerator -->
<name>org.jooq.codegen.JavaGenerator</name>
<database>
<!-- The database type. The format here is:
org.jooq.meta.[database].[database]Database -->
<name>org.jooq.meta.mysql.MySQLDatabase</name>
<!-- The database schema (or in the absence of schema support, in your RDBMS this
can be the owner, user, database name) to be generated -->
<inputSchema>demo</inputSchema>
<!-- All elements that are generated from your schema
(A Java regular expression. Use the pipe to separate several expressions)
Watch out for case-sensitivity. Depending on your database, this might be important! -->
<includes>.*</includes>
<!-- All elements that are excluded from your schema
(A Java regular expression. Use the pipe to separate several expressions).
Excludes match before includes, i.e. excludes have a higher priority -->
<excludes></excludes>
</database>
<target>
<!-- The destination package of your generated classes (within the destination directory) -->
<packageName>com.example.springbootjooq.generated</packageName>
<!-- The destination directory of your generated classes. Using Maven directory layout here -->
<directory>src/main/java</directory>
</target>
</generator>
</configuration>
自动生成
- 我们在mysql中创建demo库,并创建一张User表如下(点的比较快,年龄字段用的varchar勿喷)
mysql> describe user;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(45) | NO | | NULL | |
| age | varchar(45) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
- 执行compile,会把表结构的抽象,以及表对应的pojo自动生成到指定目录,然后就可以愉快的coding了
mvn clean compile
Demo
这里实现了最基本的功能
Controller
@RestController
@RequestMapping("/demo/")
public class DemoController {
@Autowired
private DemoService service;
@RequestMapping("/insert/user/{name}/{age}")
public void insert(@PathVariable String age, @PathVariable String name){
service.insert(new User().setAge(age).setName(name));
}
@RequestMapping("/update/user/{name}/{age}")
public void update(@PathVariable String age, @PathVariable String name){
service.update(new User().setAge(age).setName(name));
}
@RequestMapping("/delete/user/{id}")
public void delete(@PathVariable Integer id){
service.delete(id);
}
@RequestMapping("/select/user/{id}")
public User selectByID(@PathVariable Integer id){
return service.selectById(id);
}
@RequestMapping("/select/user/")
public List<User> selectByID(){
return service.selectAll();
}
}
Service
@Service
public class DemoServiceImpl implements DemoService {
@Autowired
DSLContext create;
com.example.springbootjooq.generated.tables.User USER = com.example.springbootjooq.generated.tables.User.USER;
@Override
public void delete(int id) {
create.delete(USER).where(USER.ID.eq(id)).execute();
}
@Override
public void insert(User user) {
create.insertInto(USER)
.columns(USER.NAME,USER.AGE)
.values(user.getName(), user.getAge())
.execute();
}
@Override
public int update(User user) {
create.update(USER).set((Record) user);
return 0;
}
@Override
public User selectById(int id) {
return create.select(USER.NAME,USER.AGE).from(USER).where(USER.ID.eq(id)).fetchInto(User.class).get(0);
}
@Override
public List<User> selectAll() {
return create.select().from(USER).fetchInto(User.class);
}
}
参考
https://blog.csdn.net/weixin_40826349/article/details/89887355
Springboot + 持久层框架JOOQ的更多相关文章
- spring-boot+mybatis开发实战:如何在spring-boot中使用myabtis持久层框架
前言: 本项目基于maven构建,使用mybatis-spring-boot作为spring-boot项目的持久层框架 spring-boot中使用mybatis持久层框架与原spring项目使用方式 ...
- 从零搭建springboot服务02-内嵌持久层框架Mybatis
愿历尽千帆,归来仍是少年 内嵌持久层框架Mybatis 1.所需依赖 <!-- Mysql驱动包 --> <dependency> <groupId>mysql&l ...
- MyBatis持久层框架学习之01 MyBatis的起源和发展
一.MyBatis的简介 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架. MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集. MyB ...
- 持久层框架JPA与Mybatis该如何选型
一.现状描述 目前java 持久层ORM框架应用最广泛的就是JPA和Mybatis.JPA只是一个ORM框架的规范, 对该规范的实现比较完整就是Spring Data JPA(底层基于Hibernat ...
- 八:SpringBoot-集成JPA持久层框架,简化数据库操作
SpringBoot-集成JPA持久层框架,简化数据库操作 1.JPA框架简介 1.1 JPA与Hibernate的关系: 2.SpringBoot整合JPA Spring Data JPA概述: S ...
- 【持久层框架】- SpringData - JPA
SpringData - JPA 生命不息,写作不止 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习成长! JPA简 ...
- MyBatis持久层框架使用总结
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis . 2 ...
- java持久层框架mybatis如何防止sql注入
看到一篇很好的文章:http://www.jfox.info/ava-persistence-framework-mybatis-how-to-prevent-sql-injection sql注入大 ...
- .NET平台下,关于数据持久层框架
在.NET平台下,关于数据持久层框架非常多,本文主要对如下几种做简要的介绍并推荐一些学习的资源: 1.NHibernate 2.NBear 3.Castle ActiveRecord 4.iBATIS ...
随机推荐
- P1009 字符三角形
题目描述 输入一个字符c,按照阳历输出的格式输出由该字符组成的一个字符三角形. 输入格式 输入包含一个字符c. 输出格式 输出由该字符c组成的字符三角形. 样例输入 A 样例输出 A AAA AAAA ...
- JavaScript模块化演变 CommonJs,AMD, CMD, UMD(一)
原文链接:https://www.jianshu.com/p/33d53cce8237 原文系列2链接:https://www.jianshu.com/p/ad427d8879cb 前端完全手册: h ...
- csredis-in-asp.net core理论实战-主从配置、哨兵模式
csredis GitHub https://github.com/2881099/csredis 看了github上的开源项目,上面真的只是单纯的使用文档,可能对于我这种人(菜鸟)就不太友好, 我知 ...
- Web的大趋势:Java+大前端
前后端分离,是目前Web开发的主流模式.而Java无疑是后端开发的王者,PHP和.NET目前仍处于水深火热之中,更像是在夹缝中求生存.而大前端,强势崛起!Java+大前端这一强强组合,面对其他Web领 ...
- Kubernetes基本概念和术语之《Pod》
Pod是Kubernetes的最重要也最基本的概念.我们看到每个Pod都有一个特殊的被称为“根容器”的Pause容器对应的镜像属于Kubernetes平台的一部分.除了Pause容器,每个Pod还包含 ...
- c++ 基础知识回顾 继承 继承的本质就是数据的copy
c++ 基础知识笔记 继承 什么是继承 继承就是子类继承父类的成员属性以及方法 继承的本质就是 数据的复制 是编译器帮我们做了很多操作 class Base { public: Base(){ cou ...
- $Poj3179\ Corral\ the\ Cows$ 二分+离散化+二维前缀和
Poj $Description$ 在一个二维平面上,有$N$颗草,每颗草的大小是$1*1$,左下角坐标为$x_i,y_i$.要求一个正方形,正方形的边平行于$x$或$y$轴,正方形里面包含至少$C$ ...
- $Luogu2512/CH122/AcWing122$糖果传递 模拟
$Luogu$ $AcWing$ $Description$ 有$n$个小朋友坐成一圈,每人有$a_i$个糖果. 每人只能给左右两人传递糖果. 每人每次传递一个糖果代价为$1$. 求使所有人获得均等 ...
- js菜单栏切换
先来看看需要实现的需求: 这是某购物网站上经常看到的效果 我们把网页的模型抽象出来,下面是我实现的效果图: 源代码仅供大家参考,具体如下: <!DOCTYPE html> <html ...
- (httpd、php)
(一)http协议介绍 http: 超文本传输协议,http协议是应用层协议,实现http协议的软件都监听的TCP的80端口之上.http协议也是一种文本协议,是基于TCP协议实现 http协议有几个 ...