是什么
- 全称Java Object Oriented Querying,基于java开发出来的工具包,主要用于访问关系型数据库。
为什么用
- Hibernate对SQL的操作太抽象
- JDBC使用太过繁琐
- JOOQ希望在上面两者中找到一个平衡。
基本过程
- 准备数据库中的表以及数据
- 使用JOOQ生成表结构代码文件
- 将代码文件加载到项目中调用
怎么用
- 在postgresql中准备数据
- database 为 report
- schema 为 site_issue
- table 为 issue_detail
1 CREATE TABLE site_issue.issue_detail 2 ( 3 site_id integer, 4 issue_key text COLLATE pg_catalog."default", 5 alert_type text COLLATE pg_catalog."default", 6 "alert_resolution " text COLLATE pg_catalog."default", 7 "duration_start " date, 8 "duration_end " date, 9 org_id integer 10 )
- 插入表数据
INSERT INTO site_issue.issue_detail( site_id, issue_key, alert_type, "alert_resolution ", "duration_start ", "duration_end ", org_id) VALUES (12,"23","error","asd",now(),now(),2323);
- 从https://start.spring.io/定制并下载Maven工程
- 从spring tool suite 加载report工程
- 配置pom.xml
<profiles> <profile> <id>postgresql</id> <build> <plugins> <plugin> <groupId>org.jooq</groupId> <artifactId>jooq-codegen-maven</artifactId> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.2.2</version> </dependency> </dependencies> <configuration> <jdbc> <driver>org.postgresql.Driver</driver> <url>jdbc:postgresql://localhost:5432/report</url> <user>postgres</user> <password>citrix</password> </jdbc> <generator> <name>org.jooq.util.DefaultGenerator</name> <database> <name>org.jooq.util.postgres.PostgresDatabase</name> <includes>.*</includes> <excludes /> <inputSchema>site_issue</inputSchema> </database> <target> <packageName>com.citrix.nanjing.report.jooq</packageName> <directory>gensrc/main/java</directory> </target> </generator> </configuration> </plugin> </plugins> </build> </profile> </profiles>
- 进入到代码根目录下执行 mvn clean install -P postgresql 执行完成之后你就可以看到gensrc/main/java 下面有对应的表结构代码
- 再配置pom.xml将代码加入到工程中
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>gensrc/main/java</source> </sources> </configuration> </execution> </executions> </plugin> </plugins> </build>
- 右键项目选择Maven–>update projects
- 代码中调用表结构数据
package priv.darrenqiao.nanjing.report.controller; import java.sql.Connection; import java.sql.DriverManager; import org.jooq.DSLContext; import org.jooq.Record; import org.jooq.Result; import org.jooq.SQLDialect; import org.jooq.impl.DSL; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import priv.darrenqiao.nanjing.report.jooq.tables.*; @RestController @RequestMapping("/screport/") public class ReportController { @RequestMapping("test") public String testdada() { String userName = "postgres"; String password = "citrix"; String url = "jdbc:postgresql://localhost:5432/report"; // Connection is the only JDBC resource that we need // PreparedStatement and ResultSet are handled by jOOQ, internally try (Connection conn = DriverManager.getConnection(url, userName, password)) { // ... DSLContext create = DSL.using(conn, SQLDialect.POSTGRES); Result<Record> result = create.select().from(IssueDetail.ISSUE_DETAIL).fetch(); String re = null; for (Record r : result) { re = r.getValue(IssueDetail.ISSUE_DETAIL.ISSUE_KEY); } return re; } // For the sake of this tutorial, let‘s keep exception handling simple catch (Exception e) { e.printStackTrace(); } return "test"; } }
- 启动 访问 http://localhost:8080/screport/test 就能看到 23
问题记录
- Failed to auto-configure a DataSource: ‘spring.datasource.url‘ is not specified and no embedded datasource could be auto-configured.
- 修改注解 为 @SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
- Unable to load the mojo ‘resources‘
- 右键菜单,Maven–>Update Projects.问题消失
原文地址:https://www.cnblogs.com/darrenqiao/p/9127743.html
时间: 2024-10-19 04:48:57