springboot整合jooq

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

pom文件

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jooq</artifactId>
</dependency>
<build>
        <plugins>
            <!--默认打包方式-->

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.4.1.RELEASE</version>
                <configuration>
                    <executable>true</executable>
                    <classifier>exec</classifier>
                </configuration>
            </plugin>

            <!--jooq 构建构建工具-->
            <plugin>
                <groupId>org.jooq</groupId>
                <artifactId>jooq-codegen-maven</artifactId>
                <version>${jooq.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>${mysql.version}</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <!-- JDBC connection parameters -->
                    <jdbc>
                        <driver>com.mysql.jdbc.Driver</driver>
                        <url>jdbc:mysql://120.25.211.83:3306</url>
                        <user>root</user>
                        <password>123</password>
                    </jdbc>
                    <generator>
                        <database>
                            <name>org.jooq.util.mysql.MySQLDatabase</name>
                            <includes>.*</includes>
                            <excludes/>
                            <!--数据库名-->
                            <!--inputSchema 输入模式-->
                            <schemata>
                                <schema>
                                    <inputSchema>lost_and_found</inputSchema>
                                </schema>
                                <schema>
                                    <inputSchema>wyh</inputSchema>
                                </schema>
                            </schemata>
                            <!--强制类型-->
                            <forcedTypes>
                                <forcedType>
                                    <name>BOOLEAN</name>
                                    <types>(?i:TINYINT(\s*\(\d+\))?(\s*UNSIGNED)?)</types>
                                </forcedType>
                            </forcedTypes>
                        </database>
                        <generate>
                            <pojos>true</pojos>
                            <!--<daos>true</daos>-->
                            <deprecated>true</deprecated>
                        </generate>
                        <target>
                            <packageName>com.example.domain.jooq</packageName>
                            <directory>src/main/java</directory>
                        </target>
                    </generator>
                </configuration>
            </plugin>
        </plugins>
    </build>
引入pom后在maven插件中可以生成对应数据库中的实体

这里写图片描述

生成的实体如下

这里写图片描述

jooq不需要dao层,直接上service
/**
 * Created by rj-wyh on 2017/4/11.
 */
public interface DepartmentService {
    List<Department> 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<Department> findAll() {

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

        List<Department> 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());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值