用的不多,算是个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());
}
}