Jooq

Java Object Oriented Querying
Jooq Dynamic SQL
Jooq with Spring
Spring Boot Support for Jooq


1. Jooq配置

  • Maven引用和多数据源配置
    配置之后运行插件, 既可以看到target/文件夹下面生成的数据库对象文件
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    </dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jooq</artifactId>
    </dependency>
    </dependencies>

    <build>
    <plugin>
    <!--This extension provides capability to import content from a yaml file into your pom-->
    <!--https://github.com/ozimov/yaml-properties-maven-plugin-->
    <!--http://www.mojohaus.org/properties-maven-plugin/-->
    <groupId>it.ozimov</groupId>
    <artifactId>yaml-properties-maven-plugin</artifactId>
    <version>1.1.3</version>
    <executions>
    <execution>
    <phase>initialize</phase> <!--initialize phase-->
    <goals>
    <goal>read-project-properties</goal>
    </goals>
    <configuration>
    <files>
    <file>src/main/resources/application.yml</file>
    </files>
    </configuration>
    </execution>
    </executions>
    </plugin>


    <plugin>
    <!--codegen工具用来根据mysql数据库表生成对应的Java对象-->
    <groupId>org.jooq</groupId>
    <artifactId>jooq-codegen-maven</artifactId>

    <executions>
    <execution>
    <!--注意这个id字段的配置-->
    <!--这里配置的两个execution必须有一个id为default,否则jooq-codegen会多执行一次default execution-->
    <!--这样jooq-codegen会找到mysql路径执行去default execution: 会把环境中所有database中的表全部生成Java对象-->
    <id>default</id>
    <phase>generate-sources</phase> <!--generate-sources phase-->
    <goals>
    <goal>generate</goal>
    </goals>
    <configuration>
    <jdbc>
    <driver>com.mysql.jdbc.Driver</driver>
    <url>jdbc:mysql://mysql.${yourhost}:3306</url>
    <user>username</user>
    <password>passwd</password>
    </jdbc>
    <generator>
    <name>org.jooq.util.JavaGenerator</name>
    <generate>
    <instanceFields>true</instanceFields>
    <pojos>true</pojos>
    <daos>true</daos>
    </generate>
    <database>
    <inputSchema>first_db_name</inputSchema>
    <name>org.jooq.util.mysql.MySQLDatabase</name>
    <includes>.*</includes>
    <excludes>schema_version</excludes>
    </database>
    <target>
    <packageName>${app.package}</packageName>
    <directory>target/generated-sources/jooq</directory>
    </target>
    </generator>
    </configuration>
    </execution>

    <execution>
    <id>second-generation</id>
    <phase>generate-sources</phase>
    <goals>
    <goal>generate</goal>
    </goals>
    <configuration>
    <!-- jOOQ configuration here -->
    <jdbc>
    <driver>com.mysql.jdbc.Driver</driver>
    <url>jdbc:mysql://mysql.${yourhost}:3306</url>
    <user>username</user>
    <password>passwd</password>
    </jdbc>
    <generator>
    <name>org.jooq.util.JavaGenerator</name>
    <generate>
    <instanceFields>true</instanceFields>
    <pojos>true</pojos>
    <daos>true</daos>
    </generate>
    <database>
    <name>org.jooq.util.mysql.MySQLDatabase</name>
    <includes>.*</includes>
    <excludes />
    <inputSchema>second_db_name</inputSchema>
    </database>
    <target>
    <packageName>${app.package}</packageName>
    <directory>target/generated-sources/jooq</directory>
    </target>
    </generator>
    </configuration>
    </execution>
    </executions>
    </plugin>
    </build>

2. Jooq进行基本的增删查改操作