jooq增删改查---极简案例

github传送门:https://github.com/daily11/jooq

1 pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.2.RELEASE
         
    
    com.jooq
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for jooq

    
        1.8
        UTF-8
        3.12.3
        42.2.6
        1.0.18
        5.1.47
        UTF-8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            1.5.9.RELEASE
        

        
        
            com.alibaba
            fastjson
            1.2.59
        

        
        
            com.google.code.gson
            gson
            2.8.6
        

        
            mysql
            mysql-connector-java
            ${mysql.version}
        
        
            com.alibaba
            druid
            ${druid.version}
        
        
            org.springframework.boot
            spring-boot-starter-jooq
        
        
            org.jooq
            jooq-codegen
            ${jooq.version}
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
                org.jooq
                jooq-codegen-maven
                ${jooq.version}
                
                    
                        
                            generate
                        
                    
                
                
                    
                    src/main/resources/JooqConfig.xml
                
            
        
    


2 JooqConfig.xml



    
        com.mysql.jdbc.Driver
        jdbc:mysql://localhost:3306/book?serverTimezone=UTC
        root
        123456
    
    
        
        org.jooq.codegen.JavaGenerator
        
            
            
            public\..*\.id
            
            override_primmary_key

            org.jooq.meta.mysql.MySQLDatabase

            
            .*
            

            
            book
        

        
            
            true
            true
            
            true
            
            
            false
        

        
            
            com.jooq.demo.generator
            src/main/java
        
    

3 application.properties

#访问根路径

#应用名称
spring.application.name=springboot-demo

#访问端口号
server.port=8080

#编码格式
server.tomcat.uri-encoding=utf-8

#数据库相关配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/book
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

#session生命周期
server.servlet.session.timeout=30m

4 BookDaoImpl.java

package com.jooq.demo.dao.impl;

import com.jooq.demo.dao.BookDao;
import com.jooq.demo.generator.Tables;
import com.jooq.demo.generator.tables.pojos.Info;
import org.jooq.DSLContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class BookDaoImpl implements BookDao {
    @Autowired
    DSLContext dslContext;

    @Override
    public List findBooks() {
        com.jooq.demo.generator.tables.Info info = Tables.INFO.as("info");
        List infos = dslContext
                .select(info.ID, info.NAME, info.PRICE)
                .from(info)
                .fetchInto(Info.class);
        return infos;
    }

    @Override
    public Info findBook(Integer id) {
        com.jooq.demo.generator.tables.Info info = Tables.INFO.as("info");
        List infos = dslContext
                .select(info.ID, info.NAME, info.PRICE)
                .from(info)
                .where(info.ID.eq(id))
                .fetchInto(Info.class);
        return infos == null ? null : infos.get(0);
    }

    @Override
    public void updateBook(Info entity) {
        com.jooq.demo.generator.tables.Info info = Tables.INFO.as("info");
        dslContext
                .update(info)
                .set(info.NAME, entity.getName())
                .set(info.PRICE, entity.getPrice())
                .where(info.ID.eq(entity.getId()))
                .execute();
    }

    @Override
    public void deleteBook(Integer id) {
        com.jooq.demo.generator.tables.Info info = Tables.INFO.as("info");
        dslContext
                .delete(info)
                .where(info.ID.eq(id))
                .execute();
    }
}

5 BookServiceImpl.java

package com.jooq.demo.service.impl;

import com.jooq.demo.dao.BookDao;
import com.jooq.demo.generator.tables.pojos.Info;
import com.jooq.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("bookService")
public class BookServiceImpl implements BookService {
    @Autowired
    BookDao bookDao;

    @Override
    public List findBooks() {
        List infos = bookDao.findBooks();
        return infos;
    }

    @Override
    public Info findBook(Integer id) {
        Info info = bookDao.findBook(id);
        return info;
    }

    @Override
    public void updateBook(Info info) {
        bookDao.updateBook(info);
    }

    @Override
    public void deleteBook(Integer id) {
        bookDao.deleteBook(id);
    }
}

6 BookController.java

package com.jooq.demo.controller;

import com.jooq.demo.generator.tables.pojos.Info;
import com.jooq.demo.service.BookService;
import com.jooq.demo.util.CODE;
import com.jooq.demo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class BookController {
    @Autowired
    BookService bookService;

    @RequestMapping(value = "/findBooks", method = RequestMethod.POST)
    public Result findBooks() {
        try {
            return new Result(CODE.SUCCESS, bookService.findBooks(), "success");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(CODE.ERROR, null, "error");
        }
    }

    @RequestMapping(value = "/findBook", method = RequestMethod.POST)
    public Result findBook(Integer bid) {
        try {
            return new Result(CODE.SUCCESS, bookService.findBook(bid), "success");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(CODE.ERROR, null, "error");
        }
    }

    @RequestMapping(value = "/updateBook", method = RequestMethod.POST)
    public Result updateBook(Info info) {
        try {
            bookService.updateBook(info);
            return new Result(CODE.SUCCESS, null, "success");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(CODE.ERROR, null, "error");
        }
    }

    @RequestMapping(value = "/deleteBook", method = RequestMethod.POST)
    public Result deleteBook(Integer bid) {
        try {
            bookService.deleteBook(bid);
            return new Result(CODE.SUCCESS, null, "success");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(CODE.ERROR, null, "error");
        }
    }
}

 

你可能感兴趣的:(jooq)