springboot增删改查一套Service模板

时间:2024-01-26 12:10:06

本文用来记录平时我们用的最多的curd,之前有尝试代码生成工具,不过生成的代码跟我的风格差太多了,不太好用,特地自己写个比较典型案例,可以直接复制来使用。ps:以下以CompanyService的实现粘贴出来

@Service
public class CompanyServiceImpl implements CompanyService {

    @Resource
    private CompanyDao companyDao;

    @Resource
    private SysUserLoginInfoService sysUserLoginInfoService;

    @Override
    public CompanyListResDTO list(CompanyListDTO companyListDTO) {
        Integer pageSize = companyListDTO.getPageSize();
        Integer pageNo = companyListDTO.getPageNo();
        String name = companyListDTO.getName();
        QueryWrapper<Company> query = new QueryWrapper<>();

        if (StringUtils.isNotEmpty(companyListDTO.getContactsPhone())){
            query.eq("contacts_phone", companyListDTO.getContactsPhone());
        }
        if (StringUtils.isNotEmpty(name)){
            query.like("name", name);
        }
        query.eq("is_del", CommonConstant.IS_DEL_NO);
        query.orderByDesc("id");
        // 开始分页
        PageHelper.startPage(pageNo, pageSize);
        List<Company> list = companyDao.selectList(query);
        PageInfo<Company> listInfo = new PageInfo<>(list);
        long total = listInfo.getTotal();
        List<Company> infoList = listInfo.getList();

        return new CompanyListResDTO(infoList, total);
    }

    @Override
    public Company detail(Integer id) {
        QueryWrapper<Company> query = new QueryWrapper<>();
        query.eq("is_del", CommonConstant.IS_DEL_NO);
        query.eq("id", id);
        return companyDao.selectOne(query);
    }

    @Override
    public Boolean delete(Integer id, String token) {
        Long uid = sysUserLoginInfoService.getUidByToken(token);
        Company company = this.detail(id);
        if (Objects.isNull(company)){
            throw new BizException(ErrorEnum.ERROR_NOT_EXIST.code(), ErrorEnum.ERROR_NOT_EXIST.message());
        }
        company.setIsDel(CommonConstant.IS_DEL_YES);
        company.setUpdateId(uid.intValue());
        company.setUpdatedTime(new Date());
        int deleteResult = companyDao.updateById(company);
        return 1 == deleteResult;
    }


    @Override
    public Boolean add(CompanyAddDTO companyAddDTO, String token) {

        Company exist = detailByName(companyAddDTO.getName());
        if (Objects.nonNull(exist)){
            throw new BizException(ErrorEnum.ERROR_RENAME.code(), ErrorEnum.ERROR_RENAME.message());
        }
        Company company = new Company();
        company.setName(companyAddDTO.getName());
        company.setLegalPerson(companyAddDTO.getLegalPerson());
        company.setContacts(companyAddDTO.getContacts());
        company.setContactsPhone(companyAddDTO.getContactsPhone());
        company.setRegistrationStatus(companyAddDTO.getRegistrationStatus());
        company.setRegistrationTime(companyAddDTO.getRegistrationTime());
        company.setStaffSize(companyAddDTO.getStaffSize());
        company.setSocialCreditCode(companyAddDTO.getSocialCreditCode());
        company.setOrganizationCode(companyAddDTO.getOrganizationCode());
        company.setBusinessRegistrationNumber(companyAddDTO.getBusinessRegistrationNumber());
        company.setTaxpayerCode(companyAddDTO.getTaxpayerCode());
        company.setDealerId(companyAddDTO.getDealerId());
        company.setCreateId(sysUserLoginInfoService.getUidByToken(token).intValue());
        company.setCreatedTime(new Date());

        int insertResult = companyDao.insert(company);
        return insertResult == 1;
    }

    @Override
    public Boolean edit(CompanyEditDTO companyEditDTO, String token) {
        Company company = this.detail(companyEditDTO.getId());
        if (Objects.isNull(company)){
            throw new BizException(ErrorEnum.ERROR_NOT_EXIST.code(), ErrorEnum.ERROR_NOT_EXIST.message());
        }
        String name = companyEditDTO.getName();

        /**
         * 如果名字有
         * 同时如果名字相同但id不同,报错
         */
        Company companyByName = detailByName(name);
        if (Objects.nonNull(companyByName)){
            if (companyByName.getName().equals(companyEditDTO.getName()) && !companyByName.getId().equals(companyEditDTO.getId())){
                throw new BizException(ErrorEnum.ERROR_RENAME.code(), ErrorEnum.ERROR_RENAME.message());
            }
        }

        company.setName(name);
        company.setLegalPerson(companyEditDTO.getLegalPerson());
        company.setContacts(companyEditDTO.getContacts());
        company.setContactsPhone(companyEditDTO.getContactsPhone());
        company.setRegistrationStatus(companyEditDTO.getRegistrationStatus());
        company.setRegistrationTime(companyEditDTO.getRegistrationTime());
        company.setStaffSize(companyEditDTO.getStaffSize());
        company.setSocialCreditCode(companyEditDTO.getSocialCreditCode());
        company.setOrganizationCode(companyEditDTO.getOrganizationCode());
        company.setBusinessRegistrationNumber(companyEditDTO.getBusinessRegistrationNumber());
        company.setTaxpayerCode(companyEditDTO.getTaxpayerCode());
        company.setDealerId(companyEditDTO.getDealerId());
        company.setUpdateId(sysUserLoginInfoService.getUidByToken(token).intValue());
        company.setUpdatedTime(new Date());

        int editResult = companyDao.updateById(company);
        return 1 == editResult;
    }

    /**
     * 根据名称获取该条
     * @param name
     * @return
     */
    public Company detailByName(String name){
        QueryWrapper<Company> query = new QueryWrapper<>();
        query.eq("is_del", CommonConstant.IS_DEL_NO);
        query.eq("name", name);
        return companyDao.selectOne(query);
    }
}

这里面使用了比较常见的curd操作,可以直接使用。

springboot增删改查一套Service模板_Code