JOOQ 入门第一篇

官网地址:https://www.jooq.org/

maven 依赖

 <dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>3.9.5</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-meta</artifactId>
<version>3.9.5</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen</artifactId>
<version>3.9.5</version>
</dependency>

  <!-- 数据库驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.14</version>
</dependency>

library.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration xmlns="http://www.jooq.org/xsd/jooq-codegen-3.9.2.xsd">
  <!-- Configure the database connection here -->
  <jdbc>
    <driver>com.mysql.jdbc.Driver</driver>
    <!-- 数据库url -->
    <url>jdbc:mysql://localhost:3306/library?useUnicode=true&amp;characterEncoding=UTF-8</url>
    <!-- 数据库账号 -->
    <user>root</user>
    <!-- 数据库账号密码 -->
    <password>123456</password>
  </jdbc>


  <generator>
    <!-- The default code generator. You can override this one, to generate your own code style.
         Supported generators:
         - org.jooq.util.JavaGenerator
         - org.jooq.util.ScalaGenerator
         Defaults to org.jooq.util.JavaGenerator -->
    <name>org.jooq.util.JavaGenerator</name>


    <database>
      <!-- The database type. The format here is:
           org.util.[database].[database]Database -->
      <name>org.jooq.util.mysql.MySQLDatabase</name>


      <!-- The database schema (or in the absence of schema support, in your RDBMS this
           can be the owner, user, database name) to be generated -->
      <inputSchema>library</inputSchema>


      <!-- All elements that are generated from your schema
           (A Java regular expression. Use the pipe to separate several expressions)
           Watch out for case-sensitivity. Depending on your database, this might be important! -->
      <includes>.*</includes>


      <!-- All elements that are excluded from your schema
           (A Java regular expression. Use the pipe to separate several expressions).
           Excludes match before includes, i.e. excludes have a higher priority -->
      <excludes></excludes>
    </database>


    <target>
      <!-- The destination package of your generated classes (within the destination directory) -->
      <!-- 生成的包名,生成的类在此包下 -->
      <packageName>com.caisebei.jooq.one</packageName>


      <!-- The destination directory of your generated classes. Using Maven directory layout here -->
      <!-- 输出的目录 -->
      <directory>E:\\workspacePrivate\\JooqQuickStart\\src\\main\\java</directory>
    </target>
  </generator>
</configuration>

 将对应的包放到任意的磁盘目录

然后进入到目录中:cmd

java -classpath jooq-3.9.5.jar;jooq-meta-3.9.5.jar;jooq-codegen-3.9.5.jar;mysql-connector-java-5.1.14.jar; org.jooq.util.GenerationTool library.xml

对应的测试类:

package com.caisebei.jooq.test;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;


import com.caisebei.jooq.one.tables.Author;


public class JooqMain {


public static void main(String[] args) {
Connection conn = getConnection();
DSLContext context = DSL.using(conn, SQLDialect.MYSQL);
Result<Record> result = context.select().from(Author.AUTHOR).fetch();
for (Record r : result) {
Integer id = r.getValue(Author.AUTHOR.ID);
String firstName = r.getValue(Author.AUTHOR.FIRST_NAME);
String lastName = r.getValue(Author.AUTHOR.LAST_NAME);


System.out.println("ID: " + id + " first name: " + firstName + " last name: " + lastName);
}
try {
if(conn != null && !conn.isClosed()){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


public static Connection getConnection() {
// 用户名
String userName = "root";
// 密码
String password = "123456";
// mysql连接url
String url = "jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=UTF-8";
Connection conn = null;
// Connection is the only JDBC resource that we need
// PreparedStatement and ResultSet are handled by jOOQ, internally
try {
conn = DriverManager.getConnection(url, userName, password);
System.out.println(conn);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}


}

至此,第一次用jooq 写测试demo结束,此后学习会继续更新,相关文档请查看官方英文文档。


猜你喜欢

转载自blog.csdn.net/qq_33363618/article/details/79248838