是什么

  • 全称Java Object Oriented Querying,基于java开发出来的工具包,主要用于访问关系型数据库。

为什么用

  • Hibernate对SQL的操作太抽象
  • JDBC使用太过繁琐
  • JOOQ希望在上面两者中找到一个平衡。

基本过程

  • 准备数据库中的表以及数据
  • 使用JOOQ生成表结构代码文件
  • 将代码文件加载到项目中调用

怎么用

  • 在postgresql中准备数据

    • database 为 report
    • schema 为 site_issue
    • table 为 issue_detail
 CREATE TABLE site_issue.issue_detail
(
site_id integer,
issue_key text COLLATE pg_catalog."default",
alert_type text COLLATE pg_catalog."default",
"alert_resolution " text COLLATE pg_catalog."default",
"duration_start " date,
"duration_end " date,
org_id integer
)
    • 插入表数据
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() {      //1. 指定 url 数据库用户名 密码
String userName = "postgres";
String password = "citrix";
String url = "jdbc:postgresql://localhost:5432/report"; //2. 创建连接
try (Connection conn = DriverManager.getConnection(url, userName, password)) {
//3. 将连接和具体的数据库类型绑定
DSLContext create = DSL.using(conn, SQLDialect.POSTGRES);
  
       //4. 执行查询
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.问题消失

JOOQ快速上手(基于springboot 和 postgresql)的更多相关文章

  1. 学习Keras:《Keras快速上手基于Python的深度学习实战》PDF代码+mobi

    有一定Python和TensorFlow基础的人看应该很容易,各领域的应用,但比较广泛,不深刻,讲硬件的部分可以作为入门人的参考. <Keras快速上手基于Python的深度学习实战>系统 ...

  2. 简明易懂,将细节隐藏,面向新手树立web开发概念——学完Java基础语法,超快速上手springboot+mybatiJavaWeb开发

    简明易懂,将细节隐藏,面向新手树立web开发概念 --学完Java基础语法,超快速上手JavaWeb开发 Web本质(先忽视各种协议) Web应用可以理解为浏览器和服务器之间的交互. 我们可以看一个简 ...

  3. WebFlux快速上手

    一.新建项目 示例使用IDEA快速创建基于SpringBoot的工程. springboot 2.3.1 java 8 WebFlux 必须选用Reactive的库 POM 依赖 <depend ...

  4. JeecgBoot 2.1.1 代码生成器AI版本发布,基于SpringBoot+AntDesign的JAVA快速开发平台

    此版本重点升级了 Online 代码生成器,支持更多的控件生成,所见即所得,极大的提高开发效率:同时做了数据库兼容专项工作,让 Online 开发兼容更多数据库:Mysql.SqlServer.Ora ...

  5. 在线Online表单来了!JeecgBoot 2.1 版本发布——基于SpringBoot+AntDesign的快速开发平台

    项目介绍 Jeecg-Boot 是一款基于SpringBoot+代码生成器的快速开发平台! 采用前后端分离架构:SpringBoot,Ant-Design-Vue,Mybatis,Shiro,JWT. ...

  6. NodeJS 基于 Dapr 构建云原生微服务应用,从 0 到 1 快速上手指南

    Dapr 是一个可移植的.事件驱动的运行时,它使任何开发人员能够轻松构建出弹性的.无状态和有状态的应用程序,并可运行在云平台或边缘计算中,它同时也支持多种编程语言和开发框架.Dapr 确保开发人员专注 ...

  7. SpringData 基于SpringBoot快速入门

    SpringData 基于SpringBoot快速入门 本章通过学习SpringData 和SpringBoot 相关知识将面向服务架构(SOA)的单点登录系统(SSO)需要的代码实现.这样可以从实战 ...

  8. fieldmeta 基于springboot的字段元数据管理,通用代码生成,快速开发引擎

    fieldmeta: 基于springboot的字段元数据管理 version:Alpha 0.0.1 ,码云地址:https://gitee.com/klguang/fieldmeta 元数据(Me ...

  9. 基于SpringBoot+AntDesign的快速开发平台,JeecgBoot 2.0.2 版本发布

    Jeecg-Boot 是一款基于SpringBoot+代码生成器的快速开发平台! 采用前后端分离架构:SpringBoot,Ant-Design-Vue,Mybatis,Shiro,JWT. 强大的代 ...

随机推荐

  1. linux rabbitmq 安装

    下载 在安装 erlang 时使用的是源码包21.0版本:接着下载 rabbitmq-server/3.7.7 的源码包,编译时报错,说 erlang 版本号不满足条件,erlang版本>=19 ...

  2. leetCode题解之反转二叉树

    1.题目描述 经典的反转二叉树,就是将二叉树中每个节点的左.右儿子交换. 2.题目分析 3.代码 TreeNode* invertTree(TreeNode* root) { if(root == N ...

  3. mac 手动卸载软件位置

    系统偏爱设置 /Users/xxxxx/Library/Preferences/ xxxx 支持文件 /Users/xxxxx/Library/Application Support/xxx文件夹 数 ...

  4. Jquery的跨域调用

    JQuery1.2后getJSON方法支持跨域读取json数据,原理是利用一个叫做jsonp的概念.当然,究其本质还是通过script标签动态加载js,似乎这是实现真正跨域的唯一方法. getJSON ...

  5. 在table中选中某条数据,让其显示对应详细信息

    在第一个页面中使用 ccms.dialog.open({url:url+$(this).attr("code"),id:"dialogPic",width:10 ...

  6. [翻译] AAPullToRefresh

    AAPullToRefresh 效果: Requirement - 需要的环境 ARC. iOS 6 or higher(tested on iOS 6, 7 and 8). Install - 安装 ...

  7. python安装lib库

    time:2015/11/11 双十一 一.初衷 看到一篇帖子[1],里面有python代码,就想实现一下,代码如下: import cv2 as cv import numpy as np from ...

  8. SQL语言DDL DML DCL TCL四种语言

    1.DDL(Data Definition Language)数据库定义语言:DDL使我们有能力创建或删 除表格.可以定义索引(键),规定表之间的链接,以及施加表间的 约束. • 常见DDL 语句: ...

  9. 3-urllib的post请求方式

    在urllib 中,要进行post请求时,需传入相应的data值,这里通过http://www.iqianyue.com/mypost这个网站进行测试. 案例代码如下: #post 请求 import ...

  10. 20145203 实验五 Java网络编程及安全

    20145203 实验五 Java网络编程及安全 实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全传输系统. 实验要求 1.基于Java Socket实现安全传输 2 ...