JOOQ快速上手(基於springboot 和 postgresql) ...
是什麼
- 全稱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.問題消失