jOOQ学习笔记(一)

一,下载jooq包和MySQL SQL driver
    http://www.jooq.org/download.php
    http://dev.mysql.com/downloads/connector/j/
二,创建MySQL数据库
CREATE DATABASE `guestbook` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin ; 
USE guestbook ; 
CREATE TABLE `posts` (
	`id` BIGINT(20) NOT NULL,
	`body` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_bin',
	`timestamp` DATETIME NULL DEFAULT NULL,
	`title` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_bin',
	PRIMARY KEY (`id`)
) COLLATE='utf8_bin' ENGINE=InnoDB;
三,准备自动生成代码的配置文件 guestbook.xml 


  
  
    com.mysql.jdbc.Driver
    jdbc:mysql://192.168.0.122:3306/guestbook
    root
    rootadmin
  

  
    
    org.jooq.util.DefaultGenerator

    
      
      org.jooq.util.mysql.MySQLDatabase

      
      guestbook

      
      .*

      
      
    

    
      
      test.generated

      
      D:/JavaStudy/jOOQ-3.1.0-full/lib
    
  
四,执行org.jooq.util.GenerationTool,生成代码。
    将guestbook.xml文件和MySQL SQL driver(mysql-connector-java-*-bin.jar)拷贝到{jOOQ_path}\lib文件夹中,执行:
    java -classpath jooq-3.1.0.jar;jooq-meta-3.1.0.jar;jooq-codegen-3.1.0.jar;mysql-connector-java-5.1.18-bin.jar;org.jooq.util.GenerationTool /guestbook.xml
   在Eclipse中运行生成代码,可以参考:http://www.jooq.org/doc/3.1/manual/code-generation/codegen-configuration/
五,编写代码
package test.console;

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

import java.sql.Connection;

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

import static test.generated.Tables.*;

public class Main {
	public static void main(String[] args) {
		Connection conn = null;

		String userName = "root";
		String password = "rootadmin";
		String url = "jdbc:mysql://192.168.0.122:3306/guestbook";

		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			conn = DriverManager.getConnection(url, userName, password);

			DSLContext create = DSL.using(conn, SQLDialect.MYSQL);
			
			// delete table content 
			create.delete(POSTS).execute();
			
			// insert 
			for (int i = 0; i < 10; i++) {
				create.insertInto(POSTS, POSTS.ID, POSTS.TITLE, POSTS.BODY,POSTS.TIMESTAMP).
					   values(new Long(i) , "TITLE " + i, "BODY " + i,new java.sql.Timestamp(System.currentTimeMillis())).execute();
			}
			
			// select 
			Result result = create.select().from(POSTS).fetch();
			for (Record r : result) {
				Long id = r.getValue(POSTS.ID);
				String title = r.getValue(POSTS.TITLE);
				String description = r.getValue(POSTS.BODY);
				System.out.println("ID: " + id + " title: " + title + " desciption: " + description);
			}
			
			// delete
			create.delete(POSTS).where(POSTS.ID.equal(new Long(9))).execute();
			
			// update
			create.update(POSTS)
		      .set(POSTS.TITLE, "===title===")
		      .set(POSTS.BODY, "===body===")
		      .where(POSTS.ID.equal(new Long(1)))
		      .execute();
			
			
			String sql = create.update(POSTS)
		      .set(POSTS.TITLE, "===title===")
		      .set(POSTS.BODY, "===body===")
		      .where(POSTS.ID.equal(new Long(1)))
		      .getSQL();
			
			System.out.println(sql);
			
		} catch (Exception e) {
			// For the sake of this tutorial, let's keep exception handling
			// simple
			e.printStackTrace();
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException ignore) {
				}
			}
		}
	}
}

参考:http://www.jooq.org/learn.php






你可能感兴趣的:(Java,MySQL,jOOQ)