自动生成编码(java后台)
作者:互联网
1.在数据库中建张表(最大值,字符前缀)
2.mapper中的dao层
根据字符前缀查询
3.mapper.xml文件SQL语句
注:红色圈(存在时更新,不存在是添加。具体访问:):https://www.cnblogs.com/liaojie970/p/6824773.html
根据字符前缀查询的SQL
4.封装方法(参数拼接)
5.封装方法(字符拼接)
@Service public class SysAssetsCodeService implements ISysAssetsCodeService { private Lock lock = new ReentrantLock(); @Autowired private SysAssetsCodeMapper codeMapper; @Override @Transactional public String getCode(String preFixe,int numCode) { try { lock.lock(); int result = codeMapper.insertAssetsCode(preFixe); SysAssetsCode sysAssetsCode = codeMapper.selectByPrefix(preFixe); if(sysAssetsCode != null && result > 0 ){ return String.format("%s%"+numCode+"d", preFixe,sysAssetsCode.getMaxinx()).replace(" ", "0"); }else{ throw new RuntimeException("获取编码失败"); } } finally { lock.unlock(); } } }
6.Controller控制层
/** * 根据资产编号获取详细信息 */ @PreAuthorize("@ss.hasPermi('system:assets:query')") @GetMapping(value ={"/", "/{id}"}) public AjaxResult getInfo(@PathVariable(value = "id", required = false) Long id) { AjaxResult ajax = AjaxResult.success(); SysAssets assets = null; if(StringUtils.isNotNull(id)){ assets = assetsService.selectAssetsById(id); }else{ assets = new SysAssets(); assets.setCodeId(codeService.getCode("YFCX",5)); } ajax.put(AjaxResult.DATA_TAG,assets); return ajax; }
标签:编码,AjaxResult,java,assets,lock,后台,id,preFixe,String 来源: https://www.cnblogs.com/springclout/p/15424858.html