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.问题消失

原文地址:https://www.cnblogs.com/darrenqiao/p/9127743.html

时间: 2024-08-01 20:37:55

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

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

有一定Python和TensorFlow基础的人看应该很容易,各领域的应用,但比较广泛,不深刻,讲硬件的部分可以作为入门人的参考. <Keras快速上手基于Python的深度学习实战>系统地讲解了深度学习的基本知识.建模过程和应用,并以深度学习在推荐系统.图像识别.自然语言处理.文字生成和时间序列中的具体应用为案例,详细介绍了从工具准备.数据获取和处理到针对问题进行建模的整个过程和实践经验. <Keras快速上手>PDF,531页,带书签目录,彩色配图,文字可以复制. 配套源代码和

keras快速上手-基于python的深度学习实践_第8章_文字生成源代码

源代码如下,但质量较差 # -*- coding: utf-8 -*- #!/usr/bin/env python # coding: utf-8 # # 序列模型 # In[1]: import pandas as pd import numpy as np import gc import keras from keras.models import Sequential from keras.models import load_model from keras.layers import

keras快速上手-基于python的深度学习实践-基于索引的深度学习对话模型-源代码

该章的源代码已经调通,如下, 先记录下来,再慢慢理解 #!/usr/bin/env python # coding: utf-8 # In[1]: import pandas as pd import numpy as np import pickle import keras from keras.models import Sequential, Model from keras.layers import Input, Dense, Activation, Dropout, Embeddi

SpringData 基于SpringBoot快速入门

SpringData 基于SpringBoot快速入门 本章通过学习SpringData 和SpringBoot 相关知识将面向服务架构(SOA)的单点登录系统(SSO)需要的代码实现.这样可以从实战中学习两个框架的知识,又可以为单点登录系统打下基础.通过本章你将掌握 SpringBoot项目的搭建,Starter pom的使用,配置全局文件,核心注解SpringBootApplication 介绍以及单元测试 SpringBootTest注解的使用.SpringData 的入门使用,Repos

xlauch 1.0 基于springboot + mybatis + beetls 快速开发脚手架

xlauch xlauch 是基于springboot + mybatis + beetls 快速开发脚手架, 包含了用户管理,组织机构管理,角色管理,功能点管理,菜单管理,权限分配,数据权限分配,代码生成,二次开发等功能 系统基于Spring Boot 1.5技术,前端采用了easyUI.数据库以MySQL为实例 . QQ 交流群(224708661) gitee下载地址:[email protected]:huangxy3/xlauch.git 1 使用说明 1.1 安装说明 1.1.1 导

SpringBoot快速上手——《一》:初始SpringBoot,实现入门级程序

初识SpringBoot,实现入门级程序 开发工具说明 : idea 可能有的同学很少使用idea,所以前两篇会比较多idea的截图操作! github源码:https://github.com/xivinChen/SpringBoot 1.搭建父工程 选中maven ,下一步填写组织信息,下一步知道finish.选择打开新窗口把父级工程中的src删除 2.创建第一个SpringBoot项目 这里选择Web,实质是引入了spring-boot-starter-web包 目录结构说明: src:

SpringBoot快速上手——《二》:SpringBoot集成SSM,实现增删改查功能

SpringBoot集成SSM,实现增删改查功能 github源码:https://github.com/xivinChen/SpringBoot 一.先介绍创建模块的另一种方式 1.点击Maven -> 勾选Create from archetype -> 选择 maven-archetype-quickstart 有时会需要点击 自动导入 2.工程目录 可以看到,这样创建的模块是相对干净的,需要我们手动的编写程序启动入口类.需要配置时还得创建配置文件.下一步见证. 3.完善模块 添加依赖,

Gradle快速上手——从Maven到Gradle

[本文写作于2018年7月5日] 本文适合于有一定Maven应用基础,想快速上手Gradle的读者. 背景 Maven.Gradle都是著名的依赖管理及自动构建工具.提到依赖管理与自动构建,其重要性在当今软件环境下不言而喻,Maven也是红极一时. Maven采用约定大于配置的思想,约定了工程结构,生命周期,采用严谨的XML格式进行构建脚本编写,显著地提升了软件构建的效率.但当软件越来越复杂后,大家突然发现Maven的脚本编制已然成了另外一个麻烦.绝大部分的使用者会掉入Maven的plugin陷

Power BI教程_Power BI数据分析快速上手及案例实战

Power BI数据分析快速上手及案例实战 课程学习地址:http://www.xuetuwuyou.com/course/194 课程出自学途无忧网:http://www.xuetuwuyou.com 课程简介 本课程在<Power BI 数据分析快速上手>基础上结合大量的实例,深入讲解PowerBI中看似难懂的各种概念.操作, 并结合行业中的典型案例贯穿了从初级的数据透视表工具.数据透视表选项.数据透视表的刷新.数据透视表中的排序,到中级的动 态数据透视表的创建.数据透视表函数 GETPI