jooq

ORM “杀器”之 JOOQ

亡梦爱人 提交于 2019-12-03 08:15:25
摘要 介绍JOOQ简单实用,以及相对于传统ORM框架的不同点。 正文 JOOQ是啥? JOOQ 是基于Java访问关系型数据库的工具包,轻量,简单,并且足够灵活,可以轻松的使用Java面向对象语法来实现各种复杂的sql。对于写Java的码农来说ORMS再也熟悉不过了,不管是Hibernate或者Mybatis,都能简单的使用实体映射来访问数据库。但有时候这些 ‘智能’的对象关系映射又显得笨拙,没有直接使用原生sql来的灵活和简单,而且对于一些如:joins,union, nested selects等复杂的操作支持的不友好。JOOQ 既吸取了传统ORM操作数据的简单性和安全性,又保留了原生sql的灵活性,它更像是介于 ORMS和JDBC的中间层。对于喜欢写sql的码农来说,JOOQ可以完全满足你控制欲,可以是用Java代码写出sql的感觉来。就像官网说的那样 : get back in control of your sql 这货有啥优点 JOOQ 目前在国内还是很小众,第一次听说这玩意还是通过 stream 大神 的推荐。对于从 SSH 成长起来的猿类来说,心里也会质疑 “这玩意用的人那么少,靠不靠谱” ,“会不会有很多坑要踩”。通过对着官方文档写了几个demo,顿时心生敬畏,一个念头冲到脑袋 " 这东西一定会火",于是果断在项目中使用。在使用过程中也会遇到各种小问题

Springboot 2.x 整合jOOQ实现CRUD

不问归期 提交于 2019-12-03 08:14:10
最近在翻阅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>

JOOQ入门

此生再无相见时 提交于 2019-12-03 08:13:55
刚刚入职公司用到了jooq,就上网找了几篇文章学习一下。 参考: https://segmentfault.com/a/1190000006748584?utm_source=tuicool&utm_medium=referral 官网 JOOQ是什么 jooq(Java Object Oriented Querying)java面向对象查询,是一种ORM框架,轻量,简单并且足够灵活。对于写Java的码农来说ORMS再也熟悉不过了,不管是Hibernate或者Mybatis,都能简单的使用实体映射来访问数据库。但有时候这些对象关系映射又显得笨拙,没有直接使用原生sql来的灵活和简单,而且对于一些复杂的操作支持的不友好。JOOQ 既吸取了传统ORM操作数据的简单性和安全性,又保留了原生sql的灵活性,它更像是介于 ORMS和JDBC的中间层。对于喜欢写sql的码农来说,JOOQ可以完全满足你控制欲,可以是用Java代码写出sql的感觉来。就像官网说的那样 : get back in control of your sql JOOQ的优点 通过官网和一些文章总结优点如下: 代码够简单和清晰。遇到不会写的sql可以充分利用IDEA代码提示功能轻松完成。 保留了传统ORM 的优点,简单操作性,安全性,类型安全等。不需要复杂的配置,并且可以利用Java 8 Stream API

UPSERT in PostgreSQL using jOOQ

扶醉桌前 提交于 2019-12-03 08:10:50
问题 I am trying to perform an UPSERT in PostgreSQL using the jOOQ library. For doing this I am currently trying to implement the following SQL statement in jOOQ: https://stackoverflow.com/a/6527838 My code looks like this so far: public class UpsertExecutor { private static final Logger logger = LoggerFactory.getLogger(UpsertExecutor.class); private final JOOQContextProvider jooqProvider; @Inject public UpsertExecutor(JOOQContextProvider jooqProvider) { Preconditions.checkNotNull(jooqProvider);

What does <R extends TableRecord<R>> mean in Java?

穿精又带淫゛_ 提交于 2019-12-03 08:08:42
I'm creating an interface of JOOQ TableRecord <R extends TableRecord<R>> Would anyone be able to explain the line above? Thanks It means a class of type R, that implements the interface TableRecord<R> TableRecord<R> means that the interface is bound to the same type R. An example would be a class like: public class Bla implements TableRecord<Bla> I admit this seems a bit strange, but Java generics don't really differentiate between extends and implements , which leads to some confusion. As to why this exact definition, I don't know enough about the context to see exactly why it makes sense,

JOOQ and Spring

ε祈祈猫儿з 提交于 2019-12-03 02:42:39
问题 Has anyone tried using JOOQ with the Spring framework or am I breaking new ground? http://www.jooq.org 回答1: Yes, many people have (by now). And the jOOQ manual includes a tutorial about how to get started using jOOQ, Spring, Spring-TX and BoneCP: http://www.jooq.org/doc/latest/manual/getting-started/tutorials/jooq-with-spring/ There is also a very good tutorial by Petri Kainulainen, explaining every step to set up a project, here: Using jOOQ with Spring: Configuration Using jOOQ with Spring:

jOOQ can I fetch a join of two tables into the respective POJOs

匿名 (未验证) 提交于 2019-12-03 02:24:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: In jOOQ if I want to fetch a row of a table into a jOOQ autogenerated POJOs I do, for instance: dsl . selectFrom ( USER ) . where ( USER . U_EMAIL . equal ( email )) . fetchOptionalInto ( User . class ); Now, suppose that I want to do a join between two tables, e.g. USER and ROLE , how can I fetch the result into the POJOs for these two tables? 回答1: This is one solution using ResultQuery.fetchGroups(RecordMapper, RecordMapper) Map < UserPojo , List < RolePojo >> result = dsl . select ( USER . fields ()) . select ( ROLE . fields ())

Jooq binding for “timestamp with time zone” type in postgres

匿名 (未验证) 提交于 2019-12-03 01:39:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Jooq currently does not support JSR 310 types and support will not come until v3.8 . Using simple converters generally works, except for certain types, such as postgres' TIMESTAMP WITH TIME ZONE , which requires a custom binding. So I have tried to write one but the generated XxxRecord classes still use a Timestamp data type for the TIMESTAMP WITH TIME ZONE fields in my DB. What do I need to change in my code below to see postgres' TIMESTAMP WITH TIME ZONE as an Instant in jooq's generated classes? Converter public class

jOOQ and autogeneration, how to avoid UDT Records inside table POJOs

三世轮回 提交于 2019-12-03 01:31:21
I define a type T and a view V in a PostgreSQL database. CREATE TYPE my_type AS ( mt_column1 smallint NOT NULL ); CREATE VIEW my_view AS SELECT some_column_id integer ARRAY(SELECT ROW(an_int)::my_type FROM a_table ) AS my_view_types FROM a_regular_table WHERE my_condition_hold); Using the code generation on release 3.7 I get both an UDT record class MyTypeRecord and a table record class MyViewRecord and the UDT POJO class MyType and table POJO class MyView . The MyView generated class has an array of MyTypeRecord . public class MyView extends Object implements Serializable, Cloneable, IMyView

INSERT .. SELECT with some default values in MySQL with JOOQ

匿名 (未验证) 提交于 2019-12-03 01:05:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Lets say I have a table Person(id, fname, lname) and it contains a record (1, 'Michael', 'Bay') . Now I wish to create another record in Person table with the same fname and lname but different id i.e. (453456, 'Michael', 'Bay') . This is the way I would do in plain SQL INSERT INTO Person(id, fname, lname) SELECT 453456, Person.fname, Person.lname FROM Person WHERE Person.id = 1; How can this be done with JOOQ (ideally while retaining JOOQ's code generation and type safety features)? I am aware that JOOQ provides ability to copy entire