JOOQ快速上手(基于springboot 和 postgresql)

时间:2018-06-03 10:54:19   收藏:0   阅读:1089

是什么

为什么用

基本过程

怎么用

 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);

技术分享图片

<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>
<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>
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";
    }
}

 

问题记录

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!