Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate springboot with jooq

2025-03-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how jooq integrates springboot. It is very detailed and has certain reference value. Friends who are interested must finish it!

i. Project building

We use h3dabase to build the demonstration project here, so interested partners can get the project address directly at the end of the article to start the experience, without the need for additional installation and configuration of mysql.

This paper uses SpringBoot 2.2.1.RELEASE + maven 3.5.3 + IDEA to develop.

1. Pom dependency

The core dependency configuration is given below

Org.springframework.boot spring-boot-starter-jooq com.h3database h32. Configuration

Next, set the configuration information related to the database, and create a new configuration file application.properties under the resource directory resources.

# Database Configurationspring.datasource.url=jdbc:h3:~/h3-jooq-dbspring.datasource.username=testspring.datasource.password=spring.datasource.driverClassName=org.h3.Driver3. Database initialization

One feature of jooq is that we need to generate table structure objects ourselves, so let's initialize the data structure of h3dabase first.

Table structure definition file schema-h3.sql. Please note that the table structure is different from the table creation posture of mysql.

DROP TABLE IF EXISTS poet;CREATE TABLE poet (`id` int NOT NULL, `name` varchar (20) NOT NULL default'', CONSTRAINT pk_t_poet PRIMARY KEY (ID))

Data initialization data-h3.sql

INSERT INTO `poet` (`id`, `name`) VALUES (1,'Li Bai'), (2, 'Aikeon'), (3,'ao Tao Sun'), (4,'an Zhen'), (5,'Ai Xingfu'), (6, 'Aodun Zhouqing'), (7,'an Zhen'), (8, 'Aruwei') (9,'an Hung-chien'), (10, 'Anyi Square Girl')

Next, we use the maven plug-in to initialize the data, and in the pom.xml file, add the following configuration

Org.codehaus.mojo sql-maven-plugin create-database-h3 generate-sources execute org.h3.Driver jdbc:h3:~/h3-jooq-db test true src/main/resources/schema-h3.sql Src/main/resources/data-h3.sql com.h3database h3 1.4.200

Case in the following figure to complete the initialization of the data

ii. Experience case

Before actually starting the curd of jooq, you need to make the corresponding table structure object, which is also done with the help of the maven plug-in.

1. Automatic code generation

Also add the following configuration to pom.xml

Org.jooq jooq-codegen-maven generate-h3 generate-sources generate org.h3.Driver jdbc:h3:~/h3-jooq-db test Org.jooq.meta.h3.H2Database. * PUBLIC false true true Com.git.hui.boot.jooq.h3 src/main/java

After the execution of the above figure, you will get the generated code.

2. CURD

Next, we give the basic posture of using CURD.

Import static com.git.hui.boot.jooq.h3.tables.Poet.POET;@Servicepublic class PoetService {@ Autowired DSLContext dsl; public int create (int id, String author) {return dsl.insertInto (POET) .set (POET.ID, id) .set (POET.NAME, author) .execute ();} public PoetRecord get (int id) {return dsl.selectFrom (POET) .where (POET.ID.eq (id)) .fetchOne () } public int update (int id, String author) {return dsl.update (POET) .set (POET.NAME, author) .where (POET.ID.eq (id)). Execute ();} public int delete (int id) {return dsl.delete (POET) .where (POET.ID.eq (id)). Execute ();} public List getAll () {return dsl.selectFrom (POET). Fetch ();}}

Pay attention to the above use, it is easy to understand, basically can happily write sql, you can happily use jooq, the above chain writing, for sql reading is very friendly; the focus here is DSLContext, it is automatically loaded by JooqAutoConfiguration, here directly used (for more configuration and multiple data sources, later on)

3. Test case

Introduce web dependency into pom and design some basic test case

Org.springframework.boot spring-boot-starter-web

Add, delete, modify and check case

@ RestControllerpublic class PoetRest {@ Autowired private PoetService poetService; @ RequestMapping (path = "add") public int add (Integer id, String name) {return poetService.create (id, name);} @ GetMapping (path = "get") public String get (Integer id) {PoetRecord record = poetService.get (id); return r2str (record) } @ GetMapping (path = "list") public List list () {List list = poetService.getAll (); return list.stream () .map (this::r2str) .requests (Collectors.toList ());} @ GetMapping (path = "update") public int update (int id, String author) {return poetService.update (id, author) @ GetMapping (path = "del") public int delete (int id) {return poetService.delete (id);} private String r2str (PoetRecord record) {return record.getId () + "#" + record.getName ();}}

The measured results are as follows

4. Summary

At this point, the demo of SpringBoot integrating jooq has been completed, and the basic CURD has been provided. on the whole, the integration is relatively simple, and we need to pay attention to the automatic code generation. Here, we realize the automatic code generation with the help of the maven plug-in, in addition, we can also generate the code automatically through the official jooq-xx.jar + xml configuration file. Later, we will find a separate blog post to introduce.

From the perspective of the posture of jooq, the greatest sense is the chain-like writing of sql, which is more intuitive and friendly to read. in addition, we need to pay attention to the automatically generated entity PoetRecord, which should not be exposed. It is generally recommended to use the Poet under the jooq package instead of PoetRecord as a BO object. You can convert it through RecordMapper, as shown below.

Public Poet getById (int id) {PoetRecord record = dsl.selectFrom (POET) .where (POET.ID.eq (id)). FetchOne (); RecordMapper mapper = dsl.configuration (). RecordMapperProvider (). Provide (POET.recordType (), POET.getClass ()); return mapper.map (record);} above is all the content of the article "how jooq integrates springboot". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report