sql
/*
Navicat MySQL Data Transfer
Source Server : localhost_3306
Source Server Version : 50718
Source Host : localhost:3306
Source Database : library
Target Server Type : MYSQL
Target Server Version : 50718
File Encoding : 65001
Date: 2018-02-27 14:59:23
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for author
-- ----------------------------
DROP TABLE IF EXISTS `author`;
CREATE TABLE `author` (
`id` int(11) NOT NULL,
`first_name` varchar(50) DEFAULT NULL,
`last_name` varchar(50) NOT NULL,
`date_of_birth` date DEFAULT NULL,
`year_of_birth` int(11) DEFAULT NULL,
`distinguished` int(1) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- ----------------------------
-- Table structure for book
-- ----------------------------
DROP TABLE IF EXISTS `book`;
CREATE TABLE `book` (
`id` int(11) NOT NULL,
`author_id` int(11) NOT NULL,
`title` varchar(400) NOT NULL,
`published_in` int(11) NOT NULL,
`language_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_book_author` (`author_id`),
KEY `fk_book_language` (`language_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- ----------------------------
-- Table structure for book_store
-- ----------------------------
DROP TABLE IF EXISTS `book_store`;
CREATE TABLE `book_store` (
`name` varchar(400) NOT NULL,
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- ----------------------------
-- Table structure for book_to_book_store
-- ----------------------------
DROP TABLE IF EXISTS `book_to_book_store`;
CREATE TABLE `book_to_book_store` (
`name` varchar(400) NOT NULL,
`book_id` int(11) NOT NULL,
`stock` int(11) DEFAULT NULL,
PRIMARY KEY (`name`,`book_id`),
KEY `fk_b2bs_book` (`book_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- ----------------------------
-- Table structure for language
-- ----------------------------
DROP TABLE IF EXISTS `language`;
CREATE TABLE `language` (
`id` int(11) NOT NULL,
`cd` char(2) NOT NULL,
`description` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jiulin</groupId>
<artifactId>jooq-test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<jooq.version>3.7.3</jooq.version>
<mysql.version>5.1.39</mysql.version>
<jdbc.driver>com.mysql.jdbc.Driver</jdbc.driver>
<jdbc.url>jdbc:mysql://127.0.0.1:3306/library</jdbc.url>
<jdbc.user>root</jdbc.user>
<jdbc.password>Root123456!</jdbc.password>
<jdbc.database.name>library</jdbc.database.name>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>${jooq.version}</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-meta</artifactId>
<version>${jooq.version}</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen</artifactId>
<version>${jooq.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>jooq</id>
<properties />
<activation>
<property>
<name>jooq</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>${jooq.version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
<configuration>
<jdbc>
<driver>${jdbc.driver}</driver>
<url>${jdbc.url}</url>
<user>${jdbc.user}</user>
<password>${jdbc.password}</password>
</jdbc>
<generator>
<database>
<name>org.jooq.util.mysql.MySQLDatabase</name>
<includes>.*</includes>
<excludes />
<inputSchema>${jdbc.database.name}</inputSchema>
<forcedTypes>
<forcedType>
<name>BOOLEAN</name>
<types>(?i:TINYINT(\s*\(\d+\))?(\s*UNSIGNED)?)</types>
</forcedType>
</forcedTypes>
</database>
<generate>
<deprecated>false</deprecated>
</generate>
<target>
<packageName>com.maxleap.jooq.data.jooq</packageName>
<directory>src/main/java</directory>
</target>
<generate>
<pojos>false</pojos>
<daos>false</daos>
</generate>
</generator>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
执行maven命令
clean install -Djooq
之后工程目录
测试代码
public class JOOQTest {
private DSLContext dslContext;
@Before
public void before() {
this.dslContext = getDSLContext();
}
@Test
public void insert() {
dslContext.insertInto(BOOK).set(BOOK.ID,111).set(BOOK.AUTHOR_ID,2).set(BOOK.LANGUAGE_ID,3).set(BOOK.PUBLISHED_IN,4).set(BOOK.TITLE,"aa").execute();
}
private DSLContext getDSLContext() {
try {
Connection connection =
DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/library", "root","Root123456!");
return DSL.using(connection, SQLDialect.MYSQL);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}