JOOQ跟其餘的ORM框架相似,須要作一個映射。JOOQ也要作一個映射,不過JOOQ是經過代碼類進行生成,官方的jar內提供了一個工具類能夠用來生成。java
<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>org.test-jooq</groupId> <artifactId>quickstart</artifactId> <version>0.0.1-SNAPSHOT</version> <name>jooq-quickstart</name> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.jooq</groupId> <artifactId>jooq</artifactId> <version>3.8.0</version> </dependency> <dependency> <groupId>org.jooq</groupId> <artifactId>jooq-meta</artifactId> <version>3.8.0</version> </dependency> <dependency> <groupId>org.jooq</groupId> <artifactId>jooq-codegen</artifactId> <version>3.8.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
2. 建立數據庫和表,代碼以下:mysql
-- ---------------------------- -- 建立數據庫TEST -- ---------------------------- CREATE DATABASE `TEST` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; USE TEST; -- ---------------------------- -- 建立UsersINFO表 -- ---------------------------- DROP TABLE IF EXISTS `UsersINFO`; CREATE TABLE `UsersINFO` ( `usersid` int(11) NOT NULL AUTO_INCREMENT, `usersname` varchar(20) NOT NULL, `userspwd` varchar(20) NOT NULL, PRIMARY KEY (`usersid`), UNIQUE KEY `uk_usersname` (`usersname`) ) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8; -- ---------------------------- -- 添加測試數據 -- ---------------------------- INSERT INTO `UsersINFO` VALUES ('1', 'admin', '123'); INSERT INTO `UsersINFO` VALUES ('2', 'guest', '123'); INSERT INTO `UsersINFO` VALUES ('3', 'root', '123'); commit;
3. JOOQ須要使用工具類檢索xml文件生成數據映射,官方網站提供了一個xml文件模板,在這個頁面下http://www.jooq.org/doc/3.8/manual/getting-started/tutorials/jooq-in-7-steps/jooq-in-7-steps-step3/ 我修改後的xml文件我放在了maven項目的resources資源包下:web
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <configuration xmlns="http://www.jooq.org/xsd/jooq-codegen-3.8.0.xsd"> <!-- 配置jdbc驅動鏈接 --> <jdbc> <driver>com.mysql.jdbc.Driver</driver> <url>jdbc:mysql://localhost:3306/test</url> <user>root</user> <password>root</password> </jdbc> <generator> <!-- 代碼生成器 --> <name>org.jooq.util.JavaGenerator</name> <database> <!-- 數據庫類型 --> <name>org.jooq.util.mysql.MySQLDatabase</name> <!-- 數據庫名 --> <inputSchema>test</inputSchema> <!-- 生成包含,*表示包含全部內容 --> <includes>.*</includes> <!--剔除,此處未剔除 --> <excludes></excludes> </database> <target> <!-- 生成的代碼所在的包結構 --> <packageName>org.test.jooq.generated</packageName> <!-- 生成的代碼存放路徑,默認會以src同目錄開始 --> <directory>src/main/java/</directory> </target> </generator> </configuration>
4. 官方網站說經過命令去執行,可是命令我執行不通,說找不到類,後面想了個辦法直接用eclipse工具新建了run configure運行。官方的執行代碼以下:sql
Windows: 數據庫
java -classpath jooq-3.8.0.jar;jooq-meta-3.8.0.jar;jooq-codegen-3.8.0.jar;mysql-connector-java-5.3.18-bin.jar;. org.jooq.util.GenerationTool Test.xml
java -classpath jooq-3.8.0.jar:jooq-meta-3.8.0.jar:jooq-codegen-3.8.0.jar:mysql-connector-java-5.3.18-bin.jar:. org.jooq.util.GenerationTool Test.xml
新建一個測試類測試JOOQ,代碼以下:apache
public class Test { public static void main(String[] args) throws SQLException { Connection conn = getDataSource().getConnection(); // 配置方言 DSLContext create = DSL.using(conn, SQLDialect.MYSQL); // 得到查詢結果 Result<Record> result = create.select().from(USERSINFO).fetch(); for ( Record r : result ) { Integer id = r.getValue(USERSINFO.USERSID); String name = r.getValue(USERSINFO.USERSNAME); String pwd = r.getValue(USERSINFO.USERSPWD); System.out.println("ID: " + id + " NAME : " + name + " PWD : " + pwd); } } public static DruidDataSource getDataSource() { // 使用alibaba的Druid數據鏈接池 DruidDataSource dds = new DruidDataSource(); dds.setDriverClassName("com.mysql.jdbc.Driver"); dds.setUrl("jdbc:mysql://localhost:3306/test"); dds.setUsername("root"); dds.setPassword("root"); // dds.setFilters("stat"); dds.setInitialSize(2); dds.setMaxActive(100); dds.setMaxWait(20000); dds.setPoolPreparedStatements(false); return dds; } }
上面的代碼使用了Druid數據源,下面的代碼使用jdbc:框架
public class Test { public static void main(String[] args) throws ClassNotFoundException { String userName = "root"; String password = "root"; String url = "jdbc:mysql://localhost:3306/test"; Class.forName("com.mysql.jdbc.Driver"); try (Connection conn = DriverManager.getConnection(url,userName,password)) { DSLContext create = DSL.using(conn, SQLDialect.MYSQL); Result<Record> result = create.select().from(USERSINFO).fetch(); for ( Record r : result ) { Integer id = r.getValue(USERSINFO.USERSID); String name = r.getValue(USERSINFO.USERSNAME); String pwd = r.getValue(USERSINFO.USERSPWD); System.out.println("ID: " + id + " NAME : " + name + " PWD : " + pwd); } } catch ( Exception e ) { e.printStackTrace(); } } }
項目結構如圖:eclipse
而後執行測試類就能夠了...maven
之後再更新web端的使用工具