JOOQ
JOOQ是基于Java访问关系型数据库的工具包,它具有轻量、简单、并且足够灵活的特点,通过JOOQ我们可以轻松的使用Java面向对象的语法来实现各种复杂的SQL。
Hibernate: 对于复杂SQL非常难处理
MyBatis :需要 xml文件,xml文件编写困难,容易出错,可读性不强 。
1.版本支持
1-1.JDK版本的支持
| 免费JOOQ版本 | JDK版本 | |
|---|---|---|
| 3.17 | jdk17 | |
| 3.16 | jdk11 | |
| 3.15 | jdk8 |
1-2.数据库支持
org.jooq.meta.ase.ASEDatabaseorg.jooq.meta.auroramysql.AuroraMySQLDatabaseorg.jooq.meta.aurorapostgres.AuroraPostgresDatabaseorg.jooq.meta.cockroachdb.CockroachDBDatabaseorg.jooq.meta.db2.DB2Databaseorg.jooq.meta.derby.DerbyDatabaseorg.jooq.meta.firebird.FirebirdDatabaseorg.jooq.meta.h2.H2Databaseorg.jooq.meta.hana.HANADatabaseorg.jooq.meta.hsqldb.HSQLDBDatabaseorg.jooq.meta.ignite.IgniteDatabaseorg.jooq.meta.informix.InformixDatabaseorg.jooq.meta.ingres.IngresDatabaseorg.jooq.meta.mariadb.MariaDBDatabaseorg.jooq.meta.mysql.MySQLDatabaseorg.jooq.meta.oracle.OracleDatabaseorg.jooq.meta.postgres.PostgresDatabaseorg.jooq.meta.redshift.RedshiftDatabaseorg.jooq.meta.snowflake.SnowflakeDatabaseorg.jooq.meta.sqldatawarehouse.SQLDataWarehouseDatabaseorg.jooq.meta.sqlite.SQLiteDatabaseorg.jooq.meta.sqlserver.SQLServerDatabaseorg.jooq.meta.sybase.SybaseDatabaseorg.jooq.meta.teradata.TeradataDatabaseorg.jooq.meta.vertica.VerticaDatabase
1-3.支持R2DBC
Flux<Record> from = Flux.from(dslContext.select().from(InsuranceOrder.INSURANCE_ORDER_));
Mono<Record1<Integer>> from = Mono.from(dslContext.selectCount());
2.JOOQ Code generation的使用
jOOQ的代码生成器将的数据库模式,并将其反向工程为一组Java类,这些类对表(Table)、记录(Record)、序列、POJO、DAO、存储过程、用户定义类型等进行建模
2-1.maven插件
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>${jooq.version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<skip>true</skip>
<jdbc>
<driver>org.postgresql.Driver</driver>
<url>jdbc:postgresql://192.168.3.155:5432/backstage_server</url>
<user>wftapp</user>
<password>wft2022</password>
</jdbc>
<generator>
<name>org.jooq.codegen.JavaGenerator</name>
<database>
<name>org.jooq.meta.postgres.PostgresDatabase</name>
<includes>.*</includes>
<inputSchema>public</inputSchema>
</database>
<target>
<packageName>cn.swiftpass.devops.backstage.core.jooq</packageName>
<directory>src/main/java</directory>
</target>
</generator>
</configuration>
</plugin>
2-2.生成结果

-
Tables: Tables中记录了所有生成的表
-
tables目录: tables目录下是每个表的表信息(字段,外键等信息)
-
records: records目录下是表示表数据的对象(包括set/get方法)
3.JOOQ的CURD
- 支持用java来实现sql
- 类似JPA使用record
3-1.Insert
//Record新增
public Integer insert(InsuranceOrderRecord insuranceOrderRecord) {
return dslContext.insertInto(InsuranceOrder.INSURANCE_ORDER_)
.set(insuranceOrderRecord)
.fetch();
}
//SQL
public Integer insert(String id) {
return dslContext.insertInto(InsuranceOrder.INSURANCE_ORDER_)
.columns(InsuranceOrder.INSURANCE_ORDER_.ID)
.values(id)
.fetch();
}
3-2.Update
//Record
public Integer update(InsuranceOrderRecord insuranceOrderPo) {
return dslContext.update(InsuranceOrder.INSURANCE_ORDER_)
.set(insuranceOrderPo)
.fetch();
}
//SQL
public Integer update(String id) {
return dslContext.update(InsuranceOrder.INSURANCE_ORDER_)
.set(InsuranceOrder.INSURANCE_ORDER_.ID,id)
.fetch();
}
3-3.Insert
public InsuranceOrderDto findByOrderNo(String orderId) {
return dslContext.select().from(InsuranceOrder.INSURANCE_ORDER_)
.where(InsuranceOrder.INSURANCE_ORDER_.ID.eq(orderId))
.fetch()
.into(InsuranceOrderDto.class)
}
能摸鱼就很舒服
Show Disqus Comments
扫码关注公众号:纯洁的微笑
发送 290992
即可立即永久解锁本站全部文章