在一次学习分布式跟踪系统zipkin中,发现了jooq这个组件,当时不知这个组件是干嘛的,后来抽空学习了一下,感觉这个组件还挺用的。它主要有以下作用:
通过DSL(Domain Specific Language )风格,利用Java代码写sql。
支持主流的RDMS和更多的特性,如self-joins,union,存储过程,复杂的子查询等。
提供GenerationTool,能够通过表结构自动生成代码。
Flyway是一款开源的数据库版本管理工具,它更倾向于规约优于配置的方式。Flyway可以独立于应用实现管理并跟踪数据库变更,支持数据库版本自动升级。说直白一点,Flyway就是做数据库版本控制的,在数据库版本升级中很有作用。
本文通过一个整合实例,来简单说明Jooq的用法,该例子我也提交到GitHub上,链接为:https://github.com/lzj09/sw-jooq
下面开始讲解整合的过程。
1、pom.xml文件其实整合的过程,大部分的工作都是在pom文件里面完成,只要看懂了pom文件,整合过程基本上就明白了。pom文件如下:
<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 "> <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></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>其中需要说明的几个关键点:
yaml-properties-maven-plugin,该插件可以使在pom文件中读取到application.yml文件中的配置,这样可以避免同样的配置有多次配置的问题。
flyway-maven-plugin,该插件用于配置flyway,指定了创表的sql的位置为src/main/resources/db/migration