JOOQ 入门第一篇

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

maven 依赖

 
org.jooq
jooq
3.9.5


org.jooq
jooq-meta
3.9.5


org.jooq
jooq-codegen
3.9.5

  

mysql
mysql-connector-java
5.1.14

library.xml



 
 
    com.mysql.jdbc.Driver
   
    jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=UTF-8
   
    root
   
    123456
 



 
   
    org.jooq.util.JavaGenerator


   
     
      org.jooq.util.mysql.MySQLDatabase


     
      library


     
      .*


     
     
   



   
     
     
      com.caisebei.jooq.one


     
     
      E:\\workspacePrivate\\JooqQuickStart\\src\\main\\java
   

 


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

然后进入到目录中: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 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结束,此后学习会继续更新,相关文档请查看官方英文文档。


你可能感兴趣的:(java,javaWeb)