作者:中国中国NO1 | 来源:互联网 | 2023-09-17 16:50
1.在数据库中建张表(最大值,字符前缀) 2.mapper中的dao层根据字符前缀查询 3.mapper.xml文件SQL语句注:红色圈(存在时更新,不存在是添加。具体
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;
}