5个模块重构保存代码:取消自增主键,使用雪花算法

This commit is contained in:
dataprince 2023-10-01 11:26:54 +08:00
parent c23d23159a
commit a17df59f45
22 changed files with 43 additions and 76 deletions

View File

@ -86,8 +86,8 @@ public class SysConfigController extends BaseController {
if (!configService.checkConfigKeyUnique(config)) { if (!configService.checkConfigKeyUnique(config)) {
return R.fail("新增参数'" + config.getConfigName() + "'失败,参数键名已存在"); return R.fail("新增参数'" + config.getConfigName() + "'失败,参数键名已存在");
} }
int insertedRows = configService.insertConfig(config); boolean inserted = configService.insertConfig(config);
if (insertedRows != 1) { if (!inserted) {
return R.fail("新增参数记录失败!"); return R.fail("新增参数记录失败!");
} }
return R.ok(); return R.ok();

View File

@ -85,8 +85,8 @@ public class SysDeptController extends BaseController {
if (!deptService.checkDeptNameUnique(dept)) { if (!deptService.checkDeptNameUnique(dept)) {
return R.fail("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在"); return R.fail("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
} }
int inserted = deptService.insertDept(dept); boolean inserted = deptService.insertDept(dept);
if (inserted != 1) { if (!inserted) {
return R.fail("新增部门记录失败!"); return R.fail("新增部门记录失败!");
} }
return R.ok(); return R.ok();

View File

@ -91,8 +91,8 @@ public class SysDictDataController extends BaseController {
@Log(title = "字典数据", businessType = BusinessType.INSERT) @Log(title = "字典数据", businessType = BusinessType.INSERT)
@PostMapping @PostMapping
public R<Void> add(@Validated @RequestBody SysDictDataBo dictDataBo) { public R<Void> add(@Validated @RequestBody SysDictDataBo dictDataBo) {
int insertedRows = dictDataService.insertDictData(dictDataBo); boolean inserted = dictDataService.insertDictData(dictDataBo);
if (insertedRows == 0) { if (!inserted) {
return R.fail("新增字典数据记录失败!"); return R.fail("新增字典数据记录失败!");
} }
return R.ok(); return R.ok();

View File

@ -63,8 +63,8 @@ public class SysNoticeController extends BaseController
@PostMapping @PostMapping
public R<Void> add(@Validated @RequestBody SysNoticeBo noticeBo) public R<Void> add(@Validated @RequestBody SysNoticeBo noticeBo)
{ {
int insertedRows = noticeService.insertNotice(noticeBo); boolean inserted = noticeService.insertNotice(noticeBo);
if (insertedRows != 1) { if (!inserted) {
return R.fail("新增通知公告记录失败!"); return R.fail("新增通知公告记录失败!");
} }
return R.ok(); return R.ok();

View File

@ -85,8 +85,8 @@ public class SysPostController extends BaseController
{ {
return R.fail("新增岗位'" + postBo.getPostName() + "'失败,岗位编码已存在"); return R.fail("新增岗位'" + postBo.getPostName() + "'失败,岗位编码已存在");
} }
int insertedRows = postService.insertPost(postBo); boolean inserted = postService.insertPost(postBo);
if (insertedRows != 1) { if (!inserted) {
return R.fail("新增岗位记录失败!"); return R.fail("新增岗位记录失败!");
} }
return R.ok(); return R.ok();

View File

@ -1,9 +1,7 @@
package com.ruoyi.system.domain; package com.ruoyi.system.domain;
import com.mybatisflex.annotation.Id; import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table; import com.mybatisflex.annotation.Table;
import com.mybatisflex.core.keygen.KeyGenerators;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import com.ruoyi.common.orm.core.domain.BaseEntity; import com.ruoyi.common.orm.core.domain.BaseEntity;
@ -21,11 +19,7 @@ public class SysConfig extends BaseEntity
{ {
/** 参数主键 */ /** 参数主键 */
// @Id(keyType = KeyType.Generator, value = KeyGenerators.snowFlakeId) //TODO:ID主键修改为雪花算法 @Id
// @Id(keyType = KeyType.Generator, value = KeyGenerators.flexId)
//@Id
//@Excel(name = "参数主键", cellType = ColumnType.NUMERIC)
@Id(keyType = KeyType.Auto)
private Long configId; private Long configId;
/** 参数名称 */ /** 参数名称 */

View File

@ -29,7 +29,7 @@ public class SysDept extends BaseEntity
{ {
/** 部门ID */ /** 部门ID */
@Id(keyType = KeyType.Auto) @Id
private Long deptId; private Long deptId;
/** 父部门ID */ /** 父部门ID */

View File

@ -1,7 +1,6 @@
package com.ruoyi.system.domain; package com.ruoyi.system.domain;
import com.mybatisflex.annotation.Id; import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table; import com.mybatisflex.annotation.Table;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
@ -19,7 +18,7 @@ import com.ruoyi.common.orm.core.domain.BaseEntity;
public class SysDictData extends BaseEntity public class SysDictData extends BaseEntity
{ {
/** 字典编码 */ /** 字典编码 */
@Id(keyType = KeyType.Auto) @Id
private Long dictCode; private Long dictCode;
/** 字典排序 */ /** 字典排序 */

View File

@ -1,7 +1,6 @@
package com.ruoyi.system.domain; package com.ruoyi.system.domain;
import com.mybatisflex.annotation.Id; import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table; import com.mybatisflex.annotation.Table;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
@ -18,7 +17,7 @@ import com.ruoyi.common.orm.core.domain.BaseEntity;
public class SysDictType extends BaseEntity public class SysDictType extends BaseEntity
{ {
/** 字典主键 */ /** 字典主键 */
@Id(keyType = KeyType.Auto) @Id
private Long dictId; private Long dictId;
/** 字典名称 */ /** 字典名称 */

View File

@ -1,7 +1,6 @@
package com.ruoyi.system.domain; package com.ruoyi.system.domain;
import com.mybatisflex.annotation.Id; import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table; import com.mybatisflex.annotation.Table;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
@ -18,7 +17,7 @@ import com.ruoyi.common.orm.core.domain.BaseEntity;
public class SysNotice extends BaseEntity public class SysNotice extends BaseEntity
{ {
/** 公告ID */ /** 公告ID */
@Id(keyType = KeyType.Auto) @Id
private Long noticeId; private Long noticeId;
/** 公告标题 */ /** 公告标题 */

View File

@ -2,21 +2,11 @@ package com.ruoyi.system.domain;
import com.mybatisflex.annotation.Column; import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Id; import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table; import com.mybatisflex.annotation.Table;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.core.annotation.Excel;
import com.ruoyi.common.core.annotation.Excel.ColumnType;
import com.ruoyi.common.orm.core.domain.BaseEntity; import com.ruoyi.common.orm.core.domain.BaseEntity;
import java.io.Serial;
/** /**
* 岗位表 sys_post * 岗位表 sys_post
* *
@ -28,7 +18,7 @@ import java.io.Serial;
public class SysPost extends BaseEntity public class SysPost extends BaseEntity
{ {
/** 岗位序号 */ /** 岗位序号 */
@Id(keyType = KeyType.Auto) @Id
private Long postId; private Long postId;
/** 岗位编码 */ /** 岗位编码 */

View File

@ -59,9 +59,9 @@ public interface ISysConfigService extends IBaseService<SysConfig>
* 新增参数配置 * 新增参数配置
* *
* @param config 参数配置信息 * @param config 参数配置信息
* @return 结果 * @return true 操作成功false 操作失败
*/ */
int insertConfig(SysConfigBo config); boolean insertConfig(SysConfigBo config);
/** /**
* 修改参数配置 * 修改参数配置

View File

@ -90,9 +90,9 @@ public interface ISysDeptService extends IBaseService<SysDept>
* 新增保存部门信息 * 新增保存部门信息
* *
* @param deptBo 部门信息 * @param deptBo 部门信息
* @return 受影响的行数 * @return true 操作成功false 操作失败
*/ */
int insertDept(SysDeptBo deptBo); boolean insertDept(SysDeptBo deptBo);
/** /**
* 修改保存部门信息 * 修改保存部门信息

View File

@ -77,9 +77,9 @@ public interface ISysDictDataService extends IBaseService<SysDictData>
* 新增保存字典数据信息 * 新增保存字典数据信息
* *
* @param dataBo 字典数据信息 * @param dataBo 字典数据信息
* @return 结果:受影响的行数 * @return true 操作成功false 操作失败
*/ */
int insertDictData(SysDictDataBo dataBo); boolean insertDictData(SysDictDataBo dataBo);
/** /**
* 修改保存字典数据信息 * 修改保存字典数据信息

View File

@ -43,9 +43,9 @@ public interface ISysNoticeService extends IBaseService<SysNotice>
* 新增公告 * 新增公告
* *
* @param noticeBo 公告信息 * @param noticeBo 公告信息
* @return 结果:受影响的行数 * @return true 操作成功false 操作失败
*/ */
int insertNotice(SysNoticeBo noticeBo); boolean insertNotice(SysNoticeBo noticeBo);
/** /**
* 修改公告 * 修改公告

View File

@ -98,9 +98,9 @@ public interface ISysPostService extends IBaseService<SysPost>
* 新增保存岗位信息 * 新增保存岗位信息
* *
* @param post 岗位信息 * @param post 岗位信息
* @return 结果 * @return true 操作成功false 操作失败
*/ */
int insertPost(SysPostBo post); boolean insertPost(SysPostBo post);
/** /**
* 修改保存岗位信息 * 修改保存岗位信息

View File

@ -5,8 +5,6 @@ import java.util.List;
import com.mybatisflex.core.paginate.Page; import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.query.QueryWrapper;
import com.ruoyi.common.core.constant.CacheNames; import com.ruoyi.common.core.constant.CacheNames;
import com.ruoyi.common.core.core.page.PageDomain;
import com.ruoyi.common.core.core.page.TableSupport;
import com.ruoyi.common.core.service.ConfigService; import com.ruoyi.common.core.service.ConfigService;
import com.ruoyi.common.core.utils.MapstructUtils; import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.core.utils.SpringUtils; import com.ruoyi.common.core.utils.SpringUtils;
@ -40,8 +38,6 @@ import static com.ruoyi.system.domain.table.SysConfigTableDef.SYS_CONFIG;
@RequiredArgsConstructor @RequiredArgsConstructor
@Service @Service
public class SysConfigServiceImpl extends BaseServiceImpl<SysConfigMapper, SysConfig> implements ISysConfigService, ConfigService { public class SysConfigServiceImpl extends BaseServiceImpl<SysConfigMapper, SysConfig> implements ISysConfigService, ConfigService {
@Resource
private SysConfigMapper configMapper;
@Override @Override
public QueryWrapper query() { public QueryWrapper query() {
@ -148,19 +144,13 @@ public class SysConfigServiceImpl extends BaseServiceImpl<SysConfigMapper, SysCo
* 新增参数配置 * 新增参数配置
* *
* @param configBo 参数配置信息 * @param configBo 参数配置信息
* @return 插入行数 * @return true 操作成功false 操作失败
*/ */
@CacheEvict(cacheNames = CacheNames.SYS_CONFIG, key = "#configBo.configKey") @CacheEvict(cacheNames = CacheNames.SYS_CONFIG, key = "#configBo.configKey")
@Override @Override
public int insertConfig(SysConfigBo configBo) { public boolean insertConfig(SysConfigBo configBo) {
SysConfig sysConfig = MapstructUtils.convert(configBo, SysConfig.class); SysConfig sysConfig = MapstructUtils.convert(configBo, SysConfig.class);
//boolean ret = this.save(sysConfig); return this.save(sysConfig);
//int ret = this.getMapper().insert(sysConfig,false);
//return this.getMapper().insertSelective(sysConfig);
//return configMapper.insertSelective(sysConfig);
int ret=configMapper.insert(sysConfig,false);
//boolean saved = this.save(sysConfig);
return ret;
} }
/** /**

View File

@ -244,10 +244,10 @@ public class SysDeptServiceImpl extends BaseServiceImpl<SysDeptMapper, SysDept>
* 新增保存部门信息 * 新增保存部门信息
* *
* @param deptBo 部门信息 * @param deptBo 部门信息
* @return 受影响的行数 * @return true 操作成功false 操作失败
*/ */
@Override @Override
public int insertDept(SysDeptBo deptBo) { public boolean insertDept(SysDeptBo deptBo) {
SysDeptVo info = selectDeptById(deptBo.getParentId()); SysDeptVo info = selectDeptById(deptBo.getParentId());
// 如果父节点不为正常状态,则不允许新增子节点 // 如果父节点不为正常状态,则不允许新增子节点
if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) { if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) {
@ -256,7 +256,7 @@ public class SysDeptServiceImpl extends BaseServiceImpl<SysDeptMapper, SysDept>
SysDept dept = MapstructUtils.convert(deptBo, SysDept.class); SysDept dept = MapstructUtils.convert(deptBo, SysDept.class);
dept.setAncestors(info.getAncestors() + "," + dept.getParentId()); dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
dept.setDelFlag("0");//0代表存在 dept.setDelFlag("0");//0代表存在
return deptMapper.insert(dept, false); return this.save(dept);
} }
/** /**

View File

@ -172,14 +172,14 @@ public class SysDictDataServiceImpl extends BaseServiceImpl<SysDictDataMapper, S
* 新增保存字典数据信息 * 新增保存字典数据信息
* *
* @param dataBo 字典数据信息 * @param dataBo 字典数据信息
* @return 结果:受影响的行数 * @return true 操作成功false 操作失败
*/ */
@CacheEvict(cacheNames = CacheNames.SYS_DICT, key = "#dataBo.dictType") @CacheEvict(cacheNames = CacheNames.SYS_DICT, key = "#dataBo.dictType")
@Override @Override
public int insertDictData(SysDictDataBo dataBo) public boolean insertDictData(SysDictDataBo dataBo)
{ {
SysDictData data = MapstructUtils.convert(dataBo, SysDictData.class); SysDictData data = MapstructUtils.convert(dataBo, SysDictData.class);
return dictDataMapper.insert(data,false); return this.save(data);
} }
/** /**

View File

@ -48,9 +48,6 @@ import static com.ruoyi.system.domain.table.SysDictTypeTableDef.SYS_DICT_TYPE;
@Service @Service
public class SysDictTypeServiceImpl extends BaseServiceImpl<SysDictTypeMapper, SysDictType> implements ISysDictTypeService, DictService public class SysDictTypeServiceImpl extends BaseServiceImpl<SysDictTypeMapper, SysDictType> implements ISysDictTypeService, DictService
{ {
@Resource
private SysDictTypeMapper dictTypeMapper;
@Resource @Resource
private ISysDictDataService sysDictDataService; private ISysDictDataService sysDictDataService;
@ -204,8 +201,8 @@ public class SysDictTypeServiceImpl extends BaseServiceImpl<SysDictTypeMapper, S
public List<SysDictDataVo> insertDictType(SysDictTypeBo sysDictTypeBo) public List<SysDictDataVo> insertDictType(SysDictTypeBo sysDictTypeBo)
{ {
SysDictType dict = MapstructUtils.convert(sysDictTypeBo, SysDictType.class); SysDictType dict = MapstructUtils.convert(sysDictTypeBo, SysDictType.class);
int row = dictTypeMapper.insert(dict,false); boolean saved = this.save(dict);
if (row > 0) if (saved)
{ {
// 新增 type 下无 data 数据 返回空防止缓存穿透 // 新增 type 下无 data 数据 返回空防止缓存穿透
return new ArrayList<>(); return new ArrayList<>();

View File

@ -108,13 +108,12 @@ public class SysNoticeServiceImpl extends BaseServiceImpl<SysNoticeMapper, SysNo
* 新增公告 * 新增公告
* *
* @param noticeBo 公告信息 * @param noticeBo 公告信息
* @return 受影响的行数 * @return true 操作成功false 操作失败
*/ */
@Override @Override
public int insertNotice(SysNoticeBo noticeBo) { public boolean insertNotice(SysNoticeBo noticeBo) {
SysNotice sysNotice = MapstructUtils.convert(noticeBo, SysNotice.class); SysNotice sysNotice = MapstructUtils.convert(noticeBo, SysNotice.class);
int ret = noticeMapper.insert(sysNotice, false); return this.save(sysNotice);
return ret;
} }
/** /**

View File

@ -216,13 +216,13 @@ public class SysPostServiceImpl extends BaseServiceImpl<SysPostMapper, SysPost>
* 新增保存岗位信息 * 新增保存岗位信息
* *
* @param postBo 岗位信息 * @param postBo 岗位信息
* @return 结果 * @return true 操作成功false 操作失败
*/ */
@Override @Override
public int insertPost(SysPostBo postBo) public boolean insertPost(SysPostBo postBo)
{ {
SysPost sysPost = MapstructUtils.convert(postBo, SysPost.class); SysPost sysPost = MapstructUtils.convert(postBo, SysPost.class);
return postMapper.insert(sysPost,false); return this.save(sysPost);
} }
/** /**