SpringBoot整合JOOQ例子

Posted shileibrave

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot整合JOOQ例子相关的知识,希望对你有一定的参考价值。

上一篇讲了配置,这篇做了一个例子,和大家共享一下。

项目Demo地址:https://github.com/shileishmily/spring-boot-jooq-demo

 

1、抽象DAO

package com.suixingpay.jooq;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.suixingpay.jooq.constants.SystemConst;
import org.apache.commons.collections.CollectionUtils;
import org.jooq.DSLContext;
import org.jooq.Table;
import org.jooq.UpdatableRecord;
import org.jooq.impl.UpdatableRecordImpl;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Timestamp;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

public abstract class BaseDao<R extends UpdatableRecordImpl> implements SystemConst 
    
    @Autowired
    protected DSLContext dsl;

    private static final String FIELD_ID = "id";

    private static final String FIELD_CREATE_TIME = "create_time";

    private static final String FIELD_DEL_FLAG = "del_flag";

    private static final Map<String, Byte> DEL_MAPPER = ImmutableMap.of(FIELD_DEL_FLAG, DEL);

    /**
     * Description:table
     *
     * @return table
     */
    public abstract Table<R> table();

    public R newRecord() 
        return dsl.newRecord(table());
    

    public R newRecord(Object object) 
        return dsl.newRecord(table(), object);
    

    public R get(long id) 
        return dsl.selectFrom(table()).where(" id=? ", id).fetchAny();
    

    public List<R> get(Collection<Long> ids) 

        if (CollectionUtils.isEmpty(ids)) 
            return ImmutableList.of();
        

        StringBuilder sql = new StringBuilder(" id in (");
        String pattern = IntStream.range(0, ids.size()).mapToObj(i -> "?").reduce((s1, s2) -> s1 + "," + s2).orElse("");
        sql.append(pattern).append(")");

        return dsl.selectFrom(table()).where(sql.toString(), ids.toArray()).fetch();
    

    public List<R> findAllValid() 
        return dsl.selectFrom(table()).where(" del_flag=? ", NOT_DEL).fetch();
    

    public R update(R record) 
        record.store();
        return record;
    

    public int insert(R record) 
        record.from(ImmutableMap.of(FIELD_CREATE_TIME, new Timestamp(System.currentTimeMillis())), FIELD_CREATE_TIME);
        return dsl.batchInsert(record).execute()[0];
    

    public int delete(R record) 
        record.from(DEL_MAPPER, FIELD_DEL_FLAG);
        return record.store();
    

    /**
     * 自动根据id判断插入或者更新
     *
     * @param record
     * @return
     */
    public int store(R record) 
        Long id = record.getValue(FIELD_ID, Long.class);
        if (id == null || id == 0) 
            return dsl.batchInsert(record).execute()[0];
         else 
            update(record);
            return 1;
        
    

    public void batchUpdate(List<? extends UpdatableRecord<?>> records) 

        dsl.batchUpdate(records).execute();

    

 

2、SysUserDao接口

package com.suixingpay.jooq.dao;

import com.suixingpay.jooq.entity.tables.pojos.SysUser;
import com.suixingpay.jooq.entity.tables.records.SysUserRecord;

import java.util.List;

public interface SysUserDao 

    SysUserRecord findByName(String userName);

    List<SysUserRecord> findByRealName(String realName);

    List<SysUser> findByPhone(String phone);

 

3、SysUserDaoImpl实现类

package com.suixingpay.jooq.dao.impl;

import com.suixingpay.jooq.BaseDao;
import com.suixingpay.jooq.dao.SysUserDao;
import com.suixingpay.jooq.entity.tables.pojos.SysUser;
import com.suixingpay.jooq.entity.tables.records.SysUserRecord;
import org.jooq.Table;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.suixingpay.jooq.entity.tables.SysUser.SYS_USER;

@Repository("sysUserDao")
public class SysUserDaoImpl extends BaseDao<SysUserRecord> implements SysUserDao 
    @Override
    public Table<SysUserRecord> table() 
        return SYS_USER;
    

    @Override
    public SysUserRecord findByName(String userName) 
        return this.dsl.selectFrom(table()).where("user_name=?", userName).fetchOne();
    

    @Override
    public List<SysUserRecord> findByRealName(String realName) 
        return this.dsl.selectFrom(table()).where("real_name=?", realName).fetch();
    

    @Override
    public List<SysUser> findByPhone(String phone) 
        return this.dsl.selectFrom(table()).where("phone = ?", phone).fetch().map(record -> 
            return record.into(SysUser.class);
        );

    

 

4、SysUserService接口

package com.suixingpay.jooq.service;

import com.suixingpay.jooq.entity.tables.pojos.SysUser;
import com.suixingpay.jooq.entity.tables.records.SysUserRecord;

import java.util.List;

public interface SysUserService 
    SysUserRecord newRecord();

    int insert(SysUserRecord sysUserRecord);

    int update(SysUserRecord sysUserRecord);

    int delete(int id);

    SysUserRecord get(int id);

    List<SysUserRecord> findAll();

    SysUserRecord findByName(String userName);

    List<SysUserRecord> findByRealName(String realName);

    List<SysUser> findByPhone(String phone);


 

5、SysUserServiceImpl实现类

package com.suixingpay.jooq.service.impl;

import com.suixingpay.jooq.dao.impl.SysUserDaoImpl;
import com.suixingpay.jooq.entity.tables.pojos.SysUser;
import com.suixingpay.jooq.entity.tables.records.SysUserRecord;
import com.suixingpay.jooq.service.SysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService 
    @Resource
    private SysUserDaoImpl sysUserDao;

    @Override
    public SysUserRecord newRecord() 
        return sysUserDao.newRecord();
    

    @Override
    public int insert(SysUserRecord sysUserRecord) 
        return sysUserDao.insert(sysUserRecord);
    

    @Override
    public int update(SysUserRecord sysUserRecord) 
        return 0;
    

    @Override
    public int delete(int id) 
        return 0;
    

    @Override
    public SysUserRecord get(int id) 
        return null;
    

    @Override
    public List<SysUserRecord> findAll() 
        return null;
    

    @Override
    public SysUserRecord findByName(String userName) 
        return sysUserDao.findByName(userName);
    

    @Override
    public List<SysUserRecord> findByRealName(String realName) 
        return sysUserDao.findByRealName(realName);
    

    @Override
    public List<SysUser> findByPhone(String phone) 
        return sysUserDao.findByPhone(phone);
    

 

6、Controller实现

package com.suixingpay.jooq.controller;

import com.suixingpay.jooq.entity.tables.pojos.SysUser;
import com.suixingpay.jooq.entity.tables.records.SysUserRecord;
import com.suixingpay.jooq.protocal.Response;
import com.suixingpay.jooq.service.SysUserService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@ApiModel
@RestController
@RequestMapping("/sysUser")
public class SysUserController 

    @Autowired
    private SysUserService sysUserService;

    @ApiOperation(value = "插入用户")
    @PostMapping("/insert")
    public Response saveUser(@RequestParam String userName,
                             @RequestParam String realName,
                             @RequestParam String email,
                             @RequestParam String phone,
                             @RequestParam String pswd,
                             @RequestParam int userStatus) 
        SysUserRecord record = sysUserService.newRecord();
        record.setUserName(userName);
        record.setRealName(realName);
        record.setEmail(email);
        record.setPhone(phone);
        record.setPswd(pswd);
        record.setUserStatus(userStatus);
        record.setCreateTime(new Timestamp(System.currentTimeMillis()));
        sysUserService.insert(record);
        return Response.ok();
    

    @ApiOperation(value = "根据登录账户查询")
    @PostMapping("/findByName")
    public Response findByName(@RequestParam String userName) 
        SysUserRecord sysUserRecord = sysUserService.findByName(userName);
        SysUser sysUser = sysUserRecord.into(SysUser.class);
        return Response.ok(sysUser);
    

    @ApiOperation(value = "根据真实姓名查询")
    @PostMapping("/findByRealName")
    public Response findByRealName(@RequestParam String realName) 
        List<SysUser> resultList = new ArrayList<>();
        List<SysUserRecord> list = sysUserService.findByRealName(realName);

        for (SysUserRecord sysUserRecord : list) 
            SysUser sysUser = sysUserRecord.into(SysUser.class);
            resultList.add(sysUser);
        
        return Response.ok(resultList);
    

    @ApiOperation(value = "根据手机号查询")
    @PostMapping("/findByPhone")
    public Response findByPhone(@RequestParam String phone) 
        List<SysUser> list = sysUserService.findByPhone(phone);
        return Response.ok(list);
    

 

7、本机可以访问http://localhost:8080/swagger-ui.html测试。

技术图片

 

以上是关于SpringBoot整合JOOQ例子的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot整合定时任务----Scheduled注解实现(一个注解全解决)

SpringBoot整合定时任务----Scheduled注解实现(一个注解全解决)

SpringBoot整合MyCat实现读写分离,稳进大厂

kafka第三次课!

kafka第三次课!

如何利用Spring Boot 整合微信小程序实现登录与增删改查(含代码解析)