代码规范 controller service mapper

楮乐邦
2023-12-01
  1. swagger为接口文档
     
  2. 接口参数必须带说明
     
  3. 接口命名参考http规范
  4. 根据id得到唯一信息建议不用path,用?
  5. 查询多个参数建议封装对象,使用post 放在body里面 ,requestBody
  6. 接口规则
    package ink.taigu.yingke.user.account.web.controller.account;
    
    import com.github.pagehelper.PageInfo;
    import ink.taigu.yingke.common.common.param.EnableParam;
    import ink.taigu.yingke.common.common.wrapper.Result;
    import ink.taigu.yingke.user.account.client.dto.account.AccountDTO;
    import ink.taigu.yingke.user.account.client.dto.account.UpdateAccountDTO;
    import ink.taigu.yingke.user.account.client.param.account.AccountGetParam;
    import ink.taigu.yingke.user.account.client.param.account.AccountPwdParam;
    import ink.taigu.yingke.user.account.client.param.account.AccountQueryParam;
    import ink.taigu.yingke.user.account.client.service.account.AccountFacadeService;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiImplicitParam;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import javax.validation.Valid;
    
    /**
     * 账号
     * @author dell
     */
    @Api(tags = "001001000_user_account|&账号", description = "账号")
    @RestController
    @RequestMapping("/user/account")
    public class AccountController {
    
        /**
     * 账号
     */
     @Autowired
     private AccountFacadeService accountFacadeService;
    
    
     @ApiOperation(value = "001001001_user_account_add|&添加账号", notes = "添加账号")
        @PostMapping("/add")
        public Result<Integer> addAccount(@Valid @RequestBody AccountDTO accountDTO) {
    
            return accountFacadeService.addAccount(accountDTO);
     }
    
        @ApiOperation(value = "001001002_user_account_update|&修改账号", notes = "修改账号")
        @PostMapping("/update")
        public Result<Integer> updateAccount(@Valid @RequestBody UpdateAccountDTO updateAccountDTO) {
    
            return accountFacadeService.updateAccount(updateAccountDTO);
     }
    
        @ApiOperation(value = "001001003_user_account_enable_set|&设置账号可用不可用", notes = "设置账号可用不可用")
        @PostMapping("/enable/set")
        public Result<Integer> setAccountEnable(@Valid @RequestBody EnableParam enableParam) {
            return accountFacadeService.setAccountEnable(enableParam);
     }
    
    
        @ApiOperation(value = "001001004_user_account_pwd_reset|&重置账号密码", notes = "重置账号密码")
        @PostMapping("/pwd/reset")
        public Result<Integer> resetPassword(@Valid @RequestBody AccountPwdParam accountPwdParam) {
    
            return accountFacadeService.resetPassword(accountPwdParam);
     }
    
        @ApiOperation(value = "001001005_user_account_get|&根据ID获取一个账号", notes = "根据ID获取一个账号")
        @ApiImplicitParam(name = "accountId", value = "账号ID", required = true, dataType = "Long")
        @GetMapping("/get")
        public Result<AccountDTO> getByAccountId(@RequestParam("accountId") Long accountId) {
    
            return accountFacadeService.getByAccountId(accountId);
     }
    
        @ApiOperation(value = "001001006_user_account_condition_get|&根据条件获取一个账号", notes = "根据条件获取一个账号")
        @PostMapping("/condition/get")
        public Result<AccountDTO> getByAccountParam(@RequestBody AccountGetParam accountGetParam) {
            return accountFacadeService.getByAccountParam(accountGetParam);
     }
    
        @ApiOperation(value = "001001007_user_account_query|&分页查询账号", notes = "分页查询账号")
        @PostMapping("/query")
        public Result<PageInfo<AccountDTO>> queryByAccountQueryParam(@RequestBody AccountQueryParam accountQueryParam) {
    
            return accountFacadeService.queryByAccountQueryParam(accountQueryParam);
     }
    
        @ApiOperation(value = "001001008_user_account_exist|&判断账号是否存在", notes = "判断账号是否存在")
        @PostMapping("/exist")
        public Result<Boolean> accountIfExist(@RequestBody AccountGetParam accountGetParam) {
            return accountFacadeService.ifExistAccount(accountGetParam);
     }
    
    }

     

     

     

     

  7. code说明

    swagger为接口文档

    接口参数必须带说明

    接口命名参考http规范 根据id得到唯一信息建议不用path,用? 查询多个参数建议封装对象,使用post 放在body里面 ,requestBody 接口规则
    package ink.taigu.yingke.user.account.web.controller.account;
    
    import com.github.pagehelper.PageInfo;
    import ink.taigu.yingke.common.common.param.EnableParam;
    import ink.taigu.yingke.common.common.wrapper.Result;
    import ink.taigu.yingke.user.account.client.dto.account.AccountDTO;
    import ink.taigu.yingke.user.account.client.dto.account.UpdateAccountDTO;
    import ink.taigu.yingke.user.account.client.param.account.AccountGetParam;
    import ink.taigu.yingke.user.account.client.param.account.AccountPwdParam;
    import ink.taigu.yingke.user.account.client.param.account.AccountQueryParam;
    import ink.taigu.yingke.user.account.client.service.account.AccountFacadeService;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiImplicitParam;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import javax.validation.Valid;
    
    /**
     * 账号
     * @author dell
     */
    @Api(tags = "001001000_user_account|&账号", description = "账号")
    @RestController
    @RequestMapping("/user/account")
    public class AccountController {
    
        /**
     * 账号
     */
     @Autowired
     private AccountFacadeService accountFacadeService;
    
    
     @ApiOperation(value = "001001001_user_account_add|&添加账号", notes = "添加账号")
        @PostMapping("/add")
        public Result<Integer> addAccount(@Valid @RequestBody AccountDTO accountDTO) {
    
            return accountFacadeService.addAccount(accountDTO);
     }
    
        @ApiOperation(value = "001001002_user_account_update|&修改账号", notes = "修改账号")
        @PostMapping("/update")
        public Result<Integer> updateAccount(@Valid @RequestBody UpdateAccountDTO updateAccountDTO) {
    
            return accountFacadeService.updateAccount(updateAccountDTO);
     }
    
        @ApiOperation(value = "001001003_user_account_enable_set|&设置账号可用不可用", notes = "设置账号可用不可用")
        @PostMapping("/enable/set")
        public Result<Integer> setAccountEnable(@Valid @RequestBody EnableParam enableParam) {
            return accountFacadeService.setAccountEnable(enableParam);
     }
    
    
        @ApiOperation(value = "001001004_user_account_pwd_reset|&重置账号密码", notes = "重置账号密码")
        @PostMapping("/pwd/reset")
        public Result<Integer> resetPassword(@Valid @RequestBody AccountPwdParam accountPwdParam) {
    
            return accountFacadeService.resetPassword(accountPwdParam);
     }
    
        @ApiOperation(value = "001001005_user_account_get|&根据ID获取一个账号", notes = "根据ID获取一个账号")
        @ApiImplicitParam(name = "accountId", value = "账号ID", required = true, dataType = "Long")
        @GetMapping("/get")
        public Result<AccountDTO> getByAccountId(@RequestParam("accountId") Long accountId) {
    
            return accountFacadeService.getByAccountId(accountId);
     }
    
        @ApiOperation(value = "001001006_user_account_condition_get|&根据条件获取一个账号", notes = "根据条件获取一个账号")
        @PostMapping("/condition/get")
        public Result<AccountDTO> getByAccountParam(@RequestBody AccountGetParam accountGetParam) {
            return accountFacadeService.getByAccountParam(accountGetParam);
     }
    
        @ApiOperation(value = "001001007_user_account_query|&分页查询账号", notes = "分页查询账号")
        @PostMapping("/query")
        public Result<PageInfo<AccountDTO>> queryByAccountQueryParam(@RequestBody AccountQueryParam accountQueryParam) {
    
            return accountFacadeService.queryByAccountQueryParam(accountQueryParam);
     }
    
        @ApiOperation(value = "001001008_user_account_exist|&判断账号是否存在", notes = "判断账号是否存在")
        @PostMapping("/exist")
        public Result<Boolean> accountIfExist(@RequestBody AccountGetParam accountGetParam) {
            return accountFacadeService.ifExistAccount(accountGetParam);
     }
    
    }

     

     

     

     

    code说明

     项目 地址code数字编码 子分类一地址code数字编码操作地址code数字编码最后接口地址最后接口Code
    1user-center用户/useruser_001账户accout/user/accoutuser_account_001添加/user/accout/addadd001/user/accout/add
     001001001_user_account_add
    2     公司company/user/companyuser_company_002     001002
    3     公司员工company/employ/user/company/employuser_company_employ_002     001002
    4gateway-center用户/useruser_001角色role/user/roleuser_role_003     001003
    5     角色资源resource/user/role/resourceuser_role_resource_003     
    001003
    6     角色菜单menu/user/role/menuuser_role_menu_003     001003
    7     角色模块module/user/role/moduleuser_role_module_003     001003
    8     微信wechat/user/wechatuser_wechat_004     001004

     

    函数排序。add,update,set操作等非幂等操作,然后是查询操作,先根据得到一个什么,后分页查询。

    状态,可用不可用,不要在update里修改,提供单独接口函数。update只修改基础信息。 feign和controller一起改。 controller和service函数名相同。 查询函数写法。
    111参数检查
    222分页设置
    333转型返回
    @Override
    public Result<PageInfo<CompanyDTO>> queryCompanyByQueryParam(CompanyQueryParam companyQueryParam) {
        if (ifAllFieldNull(companyQueryParam)) {
            log.info("companyQueryParam对象或者对象属性为null");
     return Result.createFailure(CommonMessage.PARAM_EMPTY);
     }
        PageHelper.startPage(companyQueryParam.getPageNum(), companyQueryParam.getPageSize());
     List<CompanyPO> companyPOList = companyMapper.selectByPage(companyQueryParam);
     PageInfo<CompanyPO> companyPOPageInfo = new PageInfo<>(companyPOList);
     return Result.createSuccess(MyBeanUtils.convertPageInfoList(companyPOPageInfo, CompanyDTO.class));
    }
    非幂等函数写法update,add。
    111参数检查
    222规则检查,唯一索引
    333能和add或者其他规则一起写的检查就一起写一份
    444赋值
    555返回
    666调用方传Code防止重复提交
    @Override
    @Transactional
    public Result<Integer> updateCompany(UpdateCompanyDTO updateCompanyDTO) {
        if (ifAllFieldNull(updateCompanyDTO)) {
            log.info("updateCompanyDTO对象或者对象属性为null");
     return Result.createFailure(CommonMessage.PARAM_EMPTY);
     }
        CompanyPO operateCompanyPO = MyBeanUtils.convert(updateCompanyDTO, CompanyPO.class);
     //ID不为空
     if (null == operateCompanyPO.getCompanyId()) {
            log.info(CommonMessage.ID_EMPTY.getMsg());
     return Result.createFailure(CommonMessage.ID_EMPTY);
     }
        if (operateCompanyPO.getCompanyId().longValue() <= 0) {
            log.info(CommonMessage.ID_ZERO.getMsg());
     return Result.createFailure(CommonMessage.ID_ZERO);
     }
        //检查
     Result<Integer> checkCompanyResult = this.checkCompany(operateCompanyPO);
     if (null != checkCompanyResult && !checkCompanyResult.ifSuccess()) {
            return checkCompanyResult;
     }
        //公司全称唯一
     CompanyGetParam companyGetParam = new CompanyGetParam();
     companyGetParam.setFullName(operateCompanyPO.getFullName());
     CompanyPO companyPO = companyMapper.selectByGetParam(companyGetParam);
     if (null != companyPO && companyPO.getCompanyId() != operateCompanyPO.getCompanyId()) {
            log.info(CompanyMessage.COMPANY_FULL_NAME_IS_EXIST.getMsg());
     return Result.createFailure(CompanyMessage.COMPANY_FULL_NAME_IS_EXIST);
     }
        //赋值
     //修改待审核
     operateCompanyPO.setAuditStatus(CompanyAuditStatus.TO_AUDIT.getCode());
     operateCompanyPO.setMaterialStatus(CompanyMaterialStatus.UPDATE_TO_AUDIT.getCode());
     //设置公司简称
     if (StringUtils.isEmpty(operateCompanyPO.getShotName())) {
            operateCompanyPO.setShotName(operateCompanyPO.getFullName());
     }
        return Result.createSuccess(companyMapper.updateByPrimaryKeySelective(operateCompanyPO));
    }

    接口分类

    类型说明备注例子
    add*添加参数*DTO
    addCompany(@Valid @RequestBody CompanyDTO companyDTO)
    update*修改参数Update*DTO 必须带Id
    updateCompany(@Valid @RequestBody UpdateCompanyDTO companyDTO)

     

    set*设置状态和可用不可用参数
    statusTypeParam
    enableParam
    setCompanyAudit(@Valid @RequestBody StatusTypeParam statusTypeParam)
    getById根据Id得到一个参数Id
     getCompanyById(@RequestParam("companyId") Long companyId) 
    @RequestParam要添加属性值
    get根据唯一条件得到一个参数
    companyGetParam

    其实对应数据库里的唯一索引

    getCompanyByParam(@RequestBody CompanyGetParam companyGetParam)
    query分页查询多个参数
    companyQueryParam

     

    queryCompanyByQueryParam(@RequestBody CompanyQueryParam companyQueryParam)
    其他特殊   

     

    mapper(组合查询获取唯一一个对象,获取一组对象,insert,update,selectById,其他操作)
    xml里面有*GetParam得到唯一一个对象,有*QueryParam构造组合查询,可以用<if test=判断是否为空>,可以用<where>去掉and。其他delete ,insert,select不建议使用 if test做成组合查询,不建议使用where,容易多操作数据。 

     

     
                                                                                                                                           

     

  8. 排序。add,update,set操作等非幂等操作,然后是查询操作,先根据得到一个什么,后分页查询。

  9. 状态,可用不肯用不要在update里修改,提供单独函数。update只修改基础信息。
  10. feign和controller一起改。
  11. controller和service函数名相同。
  12. 查询函数写法。
    111参数检查
    222分页设置
    333转型返回
    @Override
    public Result<PageInfo<CompanyDTO>> queryCompanyByQueryParam(CompanyQueryParam companyQueryParam) {
        if (ifAllFieldNull(companyQueryParam)) {
            log.info("companyQueryParam对象或者对象属性为null");
     return Result.createFailure(CommonMessage.PARAM_EMPTY);
     }
        PageHelper.startPage(companyQueryParam.getPageNum(), companyQueryParam.getPageSize());
     List<CompanyPO> companyPOList = companyMapper.selectByPage(companyQueryParam);
     PageInfo<CompanyPO> companyPOPageInfo = new PageInfo<>(companyPOList);
     return Result.createSuccess(MyBeanUtils.convertPageInfoList(companyPOPageInfo, CompanyDTO.class));
    }
  13. 非幂等函数写法update,add。
    111参数检查
    222规则检查,唯一索引
    333能和add或者其他规则一起写的检查就一起写一份
    444赋值
    555返回
    @Override
    @Transactional
    public Result<Integer> updateCompany(UpdateCompanyDTO updateCompanyDTO) {
        if (ifAllFieldNull(updateCompanyDTO)) {
            log.info("updateCompanyDTO对象或者对象属性为null");
     return Result.createFailure(CommonMessage.PARAM_EMPTY);
     }
        CompanyPO operateCompanyPO = MyBeanUtils.convert(updateCompanyDTO, CompanyPO.class);
     //ID不为空
     if (null == operateCompanyPO.getCompanyId()) {
            log.info(CommonMessage.ID_EMPTY.getMsg());
     return Result.createFailure(CommonMessage.ID_EMPTY);
     }
        if (operateCompanyPO.getCompanyId().longValue() <= 0) {
            log.info(CommonMessage.ID_ZERO.getMsg());
     return Result.createFailure(CommonMessage.ID_ZERO);
     }
        //检查
     Result<Integer> checkCompanyResult = this.checkCompany(operateCompanyPO);
     if (null != checkCompanyResult && !checkCompanyResult.ifSuccess()) {
            return checkCompanyResult;
     }
        //公司全称唯一
     CompanyGetParam companyGetParam = new CompanyGetParam();
     companyGetParam.setFullName(operateCompanyPO.getFullName());
     CompanyPO companyPO = companyMapper.selectByGetParam(companyGetParam);
     if (null != companyPO && companyPO.getCompanyId() != operateCompanyPO.getCompanyId()) {
            log.info(CompanyMessage.COMPANY_FULL_NAME_IS_EXIST.getMsg());
     return Result.createFailure(CompanyMessage.COMPANY_FULL_NAME_IS_EXIST);
     }
        //赋值
     //修改待审核
     operateCompanyPO.setAuditStatus(CompanyAuditStatus.TO_AUDIT.getCode());
     operateCompanyPO.setMaterialStatus(CompanyMaterialStatus.UPDATE_TO_AUDIT.getCode());
     //设置公司简称
     if (StringUtils.isEmpty(operateCompanyPO.getShotName())) {
            operateCompanyPO.setShotName(operateCompanyPO.getFullName());
     }
        return Result.createSuccess(companyMapper.updateByPrimaryKeySelective(operateCompanyPO));
    }
  14. 接口分类

    类型说明备注例子
    add*添加参数*DTO
    addCompany(@Valid @RequestBody CompanyDTO companyDTO)
    update*修改参数Update*DTO 必须带Id
    updateCompany(@Valid @RequestBody UpdateCompanyDTO companyDTO)

     

    set*设置状态和可用不可用参数
    statusTypeParam
    enableParam
    setCompanyAudit(@Valid @RequestBody StatusTypeParam statusTypeParam)
    getById根据Id得到一个参数Id
     getCompanyById(@RequestParam("companyId") Long companyId) 
    @RequestParam要添加属性值
    get根据唯一条件得到一个参数
    companyGetParam

    其实对应数据库里的唯一索引

    getCompanyByParam(@RequestBody CompanyGetParam companyGetParam)
    query分页查询多个参数
    companyQueryParam

     

    queryCompanyByQueryParam(@RequestBody CompanyQueryParam companyQueryParam)
    其他特殊   

     

 
 类似资料: