【笔记】SpringBoot项目整合JOOQ

前言

SpringBoot项目整合JOOQ学习笔记

添加依赖

pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jooq</artifactId>
</dependency>
</dependencies>

添加配置

src/main/resources/application.yml
1
2
3
4
5
6
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/database
username: root
password: password

配置类

src/main/java/com/config/JooqConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.config;

import org.jooq.DSLContext;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class JooqConfig {

@Autowired
private DataSource dataSource;

@Bean
public DSLContext dslContext() {
return DSL.using(dataSource, SQLDialect.MYSQL);
}
}

持久层

src/main/java/com/dao/UserDao.java
1
2
3
4
5
6
7
8
9
10
11
12
package com.dao;

import org.jooq.DSLContext;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class UserDAO {

@Autowired
private DSLContext dslContext;

}

完成

参考文献

哔哩哔哩——Will保哥