SpringBoot整合Jooq和Flyway

在一次學習分佈式跟蹤系統zipkin中,發現了jooq這個組件,當時不知這個組件是幹嗎的,後來抽空學習了一下,感受這個組件還挺用的。它主要有如下做用:java

  • 經過DSL(Domain Specific Language )風格,利用Java代碼寫sql。
  • 支持主流的RDMS和更多的特性,如self-joins,union,存儲過程,複雜的子查詢等。
  • 提供GenerationTool,可以經過表結構自動生成代碼。

Flyway是一款開源的數據庫版本管理工具,它更傾向於規約優於配置的方式。Flyway能夠獨立於應用實現管理並跟蹤數據庫變動,支持數據庫版本自動升級。說直白一點,Flyway就是作數據庫版本控制的,在數據庫版本升級中頗有做用。mysql

本文經過一個整合實例,來簡單說明Jooq的用法,該例子我也提交到GitHub上,連接爲:github.com/lzj09/sw-jo…git

下面開始講解整合的過程。github

一、pom.xml文件

其實整合的過程,大部分的工做都是在pom文件裏面完成,只要看懂了pom文件,整合過程基本上就明白了。pom文件以下:web

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.2.RELEASE</version>
	</parent>

	<modelVersion>4.0.0</modelVersion>
	<groupId>com.swnote.jooq</groupId>
	<artifactId>sw-jooq</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>sw-jooq</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

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

        <!-- jooq相關包 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jooq</artifactId>
		</dependency>
		<dependency>
			<groupId>org.jooq</groupId>
			<artifactId>jooq-meta</artifactId>
		</dependency>
		<dependency>
			<groupId>org.jooq</groupId>
			<artifactId>jooq-codegen</artifactId>
		</dependency>

        <!-- flyway相關包 -->
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>
        
        <!-- mysql驅動 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

            <!-- 用於加載application.yml信息,在pom文件可讀取到application.yml中信息 -->
			<plugin>
				<groupId>it.ozimov</groupId>
				<artifactId>yaml-properties-maven-plugin</artifactId>
				<version>1.1.3</version>
				<executions>
					<execution>
						<phase>initialize</phase>
						<goals>
							<goal>read-project-properties</goal>
						</goals>
						<configuration>
							<files>
								<file>src/main/resources/application.yml</file>
							</files>
						</configuration>
					</execution>
				</executions>
			</plugin>

            <!-- flyway的插件配置 -->
			<plugin>
				<groupId>org.flywaydb</groupId>
				<artifactId>flyway-maven-plugin</artifactId>
				<executions>
					<execution>
						<phase>generate-sources</phase>
						<goals>
							<goal>migrate</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
				    <!-- 此處的信息是讀取的application.yml中的信息 -->
					<url>${spring.datasource.url}</url>
					<user>${spring.datasource.username}</user>
					<password>${spring.datasource.password}</password>
					<locations>
						<location>filesystem:src/main/resources/db/migration</location>
					</locations>
				</configuration>
			</plugin>

            <!-- jooq插件配置 -->
			<plugin>
				<groupId>org.jooq</groupId>
				<artifactId>jooq-codegen-maven</artifactId>
				<executions>
					<execution>
						<id>jooq</id>
						<phase>generate-sources</phase>
						<goals>
							<goal>generate</goal>
						</goals>
						<configuration>
							<jdbc>
								<driver>${spring.datasource.driverClassName}</driver>
								<url>${spring.datasource.url}</url>
								<user>${spring.datasource.username}</user>
								<password>${spring.datasource.password}</password>
							</jdbc>
							<!-- 自動生成代碼的配置 -->
							<generator>
								<database>
									<name>org.jooq.util.mysql.MySQLDatabase</name>
									<includes>.*</includes>
									<excludes></excludes>
									<dateAsTimestamp>true</dateAsTimestamp>
									<!-- 鏈接的是數據庫中jooq模式 -->
									<inputSchema>jooq</inputSchema>
								</database>
								<generate>
									<deprecated>false</deprecated>
									<instanceFields>true</instanceFields>
									<pojos>true</pojos>
								</generate>
								<target>
								    <!-- 指定自動生成代碼的位置 -->
									<packageName>com.swnote.jooq.generator</packageName>
									<directory>src/main/java</directory>
								</target>
							</generator>
						</configuration>
					</execution>
				</executions>
				<dependencies>
					<dependency>
						<groupId>mysql</groupId>
						<artifactId>mysql-connector-java</artifactId>
						<version>5.1.46</version>
					</dependency>
				</dependencies>
			</plugin>
		</plugins>

		<resources>
			<resource>
				<directory>src/main/java</directory>
				<excludes>
					<exclude>**/*.java</exclude>
				</excludes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
			</resource>
		</resources>
	</build>
</project>
複製代碼

其中須要說明的幾個關鍵點:spring

  • yaml-properties-maven-plugin,該插件可使在pom文件中讀取到application.yml文件中的配置,這樣能夠避免一樣的配置有屢次配置的問題。
  • flyway-maven-plugin,該插件用於配置flyway,指定了創表的sql的位置爲src/main/resources/db/migration
  • jooq-codegen-maven,配置了所鏈接的數據庫信息,模式名,以及自動生成的代碼的配置,好比要生成哪些代碼、將生成的代碼放在哪一個目錄中等。

二、基於Jooq的CURD

因爲Jooq支持經過Java代碼來寫sql的邏輯,爲此例子工程中沒有Dao層,sql的邏輯所有在Service層,Service層的代碼以下:sql

用戶信息服務接口:IUserService數據庫

package com.swnote.jooq.service;

import java.util.List;
import java.util.Map;

import com.swnote.jooq.generator.tables.pojos.User;

/**
 * 用戶信息服務接口
 * 
 * @author lzj
 * @date [2019-03-10]
 */
public interface IUserService {

    /**
     * 建立用戶
     * 
     * @param user
     */
    void create(User user);

    /**
     * 根據id刪除用戶
     * 
     * @param user_id
     */
    void delete(String user_id);

    /**
     * 更新用戶
     * 
     * @param user
     */
    void update(User user);
    
    /**
     * 根據id獲取用戶
     * 
     * @param user_id
     * @return
     */
    User retrieve(String user_id);

    /**
     * 根據條件獲取用戶列表
     * 
     * @param params
     * @return
     */
    List<User> queryForList(Map<String, Object> params);
}
複製代碼

用戶信息服務類:UserServiceapache

package com.swnote.jooq.service.impl;

import static com.swnote.jooq.generator.tables.User.USER;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.jooq.DSLContext;
import org.jooq.UpdateQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.swnote.jooq.generator.tables.pojos.User;
import com.swnote.jooq.generator.tables.records.UserRecord;
import com.swnote.jooq.service.IUserService;

/**
 * 用戶信息服務類
 * 
 * @author lzj
 * @date [2019-03-10]
 */
@Transactional
@Service
public class UserService implements IUserService {

    @Autowired
    private DSLContext dsl;

    @Override
    public void create(User user) {
    	// 構建insert語句
        dsl.insertInto(USER, USER.USER_ID, USER.NAME, USER.INTRO)
            .values(user.getUserId(), user.getName(), user.getIntro()).execute();
    }

    @Override
    public void delete(String user_id) {
    	// 構建delete語句
        dsl.delete(USER).where(USER.USER_ID.eq(user_id)).execute();
    }

    @Override
    public void update(User user) {
    	// 構建update語句
        UpdateQuery<UserRecord> update = dsl.updateQuery(USER);
        update.addValue(USER.NAME, user.getName());
        update.addValue(USER.INTRO, user.getIntro());
        update.addConditions(USER.USER_ID.eq(user.getUserId()));
        update.execute();
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Override
    public User retrieve(String user_id) {
    	// 構建select語句
        List<User> users = dsl.select(USER.USER_ID, USER.NAME, USER.INTRO).from(USER).where(USER.USER_ID.eq(user_id))
            .fetch().into(User.class);
        
        if (users != null && !users.isEmpty()) {
            return users.get(0);
        }
        return null;
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Override
    public List<User> queryForList(Map<String, Object> params) {
    	// 構建select語句
        StringBuilder builder = new StringBuilder();
        if (params != null) {
            for (Entry<String, Object> entry : params.entrySet()) {
                if (builder.length() == 0) {
                    builder.append(entry.getKey()).append(" = ").append(entry.getValue());
                } else {
                    builder.append(" and ").append(entry.getKey()).append(" = ").append(entry.getValue());
                }
            }
        }
        
        List<User> users = dsl.select(USER.USER_ID, USER.NAME, USER.INTRO).from(USER).where(builder.toString()).fetch().into(User.class);
        return users;
    }
}
複製代碼

從上面的代碼能夠看到利用Jooq來寫sql的邏輯,也很簡單。bash

三、測試

Controller層代碼在此就不寫,感興趣的能夠個人GitHub中去看。在此經過PostMan測試Controller中暴露的REST接口。例如新增接口:

執行後,數據庫也就有了記錄,即:

關注我

以你最方便的方式關注我: 微信公衆號

相關文章
相關標籤/搜索