JOOQ跟其他的ORM框架类似,需要做一个映射。JOOQ也要做一个映射,不过JOOQ是通过代码类进行生成,官方的jar内提供了一个工具类可以用来生成。
新建maven项目,编辑pom.xml,编入依赖和插件,代码如下:
<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. 创建数据库和表,代码如下:
-- ---------------------------- -- 创建数据库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资源包下:
<?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运行。官方的执行代码如下:
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,代码如下:
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(); } } }
项目结构如图:
然后执行测试类就可以了...
以后再更新web端的使用