springboot整合jooq

用的不多,算是个DEMO吧,如有错误欢迎指正

pom文件

<dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jooqartifactId>
dependency>
<build>
        <plugins>
            

            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <version>1.4.1.RELEASEversion>
                <configuration>
                    <executable>trueexecutable>
                    <classifier>execclassifier>
                configuration>
            plugin>

            
            <plugin>
                <groupId>org.jooqgroupId>
                <artifactId>jooq-codegen-mavenartifactId>
                <version>${jooq.version}version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generategoal>
                        goals>
                    execution>
                executions>
                <dependencies>
                    <dependency>
                        <groupId>mysqlgroupId>
                        <artifactId>mysql-connector-javaartifactId>
                        <version>${mysql.version}version>
                    dependency>
                dependencies>
                <configuration>
                    
                    <jdbc>
                        <driver>com.mysql.jdbc.Driverdriver>
                        <url>jdbc:mysql://120.25.211.83:3306url>
                        <user>rootuser>
                        <password>123password>
                    jdbc>
                    <generator>
                        <database>
                            <name>org.jooq.util.mysql.MySQLDatabasename>
                            <includes>.*includes>
                            <excludes/>
                            
                            
                            <schemata>
                                <schema>
                                    <inputSchema>lost_and_foundinputSchema>
                                schema>
                                <schema>
                                    <inputSchema>wyhinputSchema>
                                schema>
                            schemata>
                            
                            <forcedTypes>
                                <forcedType>
                                    <name>BOOLEANname>
                                    <types>(?i:TINYINT(\s*\(\d+\))?(\s*UNSIGNED)?)types>
                                forcedType>
                            forcedTypes>
                        database>
                        <generate>
                            <pojos>truepojos>
                            
                            <deprecated>truedeprecated>
                        generate>
                        <target>
                            <packageName>com.example.domain.jooqpackageName>
                            <directory>src/main/javadirectory>
                        target>
                    generator>
                configuration>
            plugin>
        plugins>
    build>

引入pom后在maven插件中可以生成对应数据库中的实体

springboot整合jooq_第1张图片

生成的实体如下

springboot整合jooq_第2张图片

jooq不需要dao层,直接上service

/**
 * Created by rj-wyh on 2017/4/11.
 */
public interface DepartmentService {
    List findAll();

    String save(Department department);
}
import com.wyh.data.domain.jooq.tables.pojos.Department;
import com.wyh.data.domain.jooq.tables.records.DepartmentRecord;
import com.wyh.data.service.DepartmentService;
import org.jooq.DSLContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.wyh.data.domain.jooq.tables.Department.DEPARTMENT;

/**
 * Created by rj-wyh on 2017/4/11.
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DSLContext create;

    @Override
    public List findAll() {

        List departmentRecords= create
                .select()
                .from(DEPARTMENT)
                .fetchInto(DepartmentRecord.class);

        List departmentList = new ArrayList<>();

        for (DepartmentRecord departmentRecord:
        departmentRecords) {
            Department department = new Department();
            department.setName(departmentRecord.getName());
            department.setEmployeesNum(departmentRecord.getEmployeesNum());
            department.setId(departmentRecord.getId());
            department.setManager(departmentRecord.getManager());
            departmentList.add(department);
        }
        return departmentList;
    }

    @Override
    public String save(Department department) {
        create
                .insertInto(DEPARTMENT, DEPARTMENT.NAME, DEPARTMENT.EMPLOYEES_NUM, DEPARTMENT.MANAGER)
                .values(department.getName(),  department.getEmployeesNum(), department.getManager())
                .execute();
        return "success";
    }
}

上controller层

import com.wyh.data.domain.jooq.tables.pojos.Department;
import com.wyh.data.domain.pojo.Result;
import com.wyh.data.service.DepartmentService;
import com.wyh.data.utils.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Created by rj-wyh on 2017/4/11.
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @PostMapping("/save")
    @ApiOperation(value = "保存部门")
    public Result save(@RequestBody@Validated Department department, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultUtil.error(bindingResult.getFieldError().getDefaultMessage(), "-1");
        }
        return ResultUtil.success(departmentService.save(department));
    }

    @GetMapping("/findAll")
    @ApiOperation(value = "查找全部部门")
    public Result findAll(){
        return ResultUtil.success(departmentService.findAll());
    }
}

你可能感兴趣的:(Spring)