专业表操作鉴权增、删、改、id查、全查、名字模糊查
第一版已上线,开始丰富项目功能,给专业表写了几个基础接口
总结:
1、一般这种对数据库直接操作的接口尽量对用户权限以角色进行约束,本项目和负责人对接后,只有当前用户有超级管理员角色才能对专业表操作
2、新增时,要拿入参专业名查重
3、修改、删除、查询时,最好对入参id进行合法性判断,防穿透(非空非负)
4、修改、删除时,要拿入参id查一下这条数据是否存在于数据库
5、修改时,还要拿入参专业名查重;专业名和已存在的所有班组比较,不能重复;注意:这里比较之前先剔除自己,因为有可能只是改本专业的描述,是不修改专业名的,所以可以传当前id对应的的专业名
6、id查、全查、专业名模糊查都可以在控制层中返回,直接用lambdaQuery操作即可
7、最重要的一点:这么简单的接口在一开始的时候,无论怎么调试,都进不了数据库,一开始以为是新增的字段属性问题,一直在调试实体类、DTO、专业表的属性字段和包装类型,但是一直报这个错:
"errorMsg": "\r\n
### Error updating database. Cause: java.sql.SQLException: Connection is read-only.
Queries leading to data modification are not allowed\r\n### The error may exist in cn/esky/backend/authority/dao/team/TeamSpecialityMapper.java (best guess)\r\n### The error may involve cn.esky.backend.authority.dao.team.TeamSpecialityMapper.insert-Inline\r\n### The error occurred while setting parameters\r\n### SQL: INSERT INTO c_speciality ( id, speciality_name, description_, update_time, updated_by, create_time, created_by ) VALUES ( ?, ?, ?, ?, ?, ?, ? )\r\n### Cause: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed\n; Connection is read-only. Queries leading to data modification are not allowed; nested exception is java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed",
其实从###后面就已经提示的很明显了,只读
在查资料后在serviceImpl里每一个方法开头都加上:
@Transactional(rollbackFor = Exception.class)
即可解决
新增专业addSpeciality
@Override
@Transactional(rollbackFor = Exception.class)
public R<String> addSpeciality(SpecialitySaveDTO saveDTO) {
String msg;
Long userId = ContextUtil.getUserId();
//userId=1452475321122029568L;
List<Role> roleList = roleService.findRoleByUserId(userId);
当前用户需要在<用户_角色表>配置角色
if (null == roleList || roleList.isEmpty()) {
msg = "通过用户id查找所属角色信息失败,userId:" userId;
log.error(msg);
return R.fail(msg);
}
Set<String> roleCodeSet = new HashSet<>();
roleList.forEach(role -> roleCodeSet.add(role.getCode()));
判断当前用户是不是内置管理员
if (!roleCodeSet.contains(SUPER_ADMIN)) {
msg = "当前用户非内置管理员,无权进行新增专业操作";
log.error(msg);
return R.fail(msg);
}
String specialityName = saveDTO.getSpecialityName();
专业名不能为空
if(null==specialityName||specialityName.isEmpty()){
msg = "专业名不能为空!";
log.error(msg);
return R.fail(msg);
}
专业名不能和现有的重复
List<TeamSpeciality> specialityList = teamSpecialityService.lambdaQuery().eq(TeamSpeciality::getSpecialityName, specialityName).list();
if (specialityList.size() > 0) {
msg = "此专业名已存在,请重新输入!";
log.error(msg);
return R.fail(msg);
}
TeamSpeciality teamSpeciality = new TeamSpeciality();
BeanUtils.copyProperties(saveDTO, teamSpeciality);
状态设置为可用
teamSpeciality.setStates(VALID_STATUS);
if (!teamSpecialityService.save(teamSpeciality)) {
msg = "新增失败,请稍后重试!";
log.error(msg);
return R.fail(msg);
}
msg = "新增专业成功!";
return R.success(msg);
}
修改专业updateSpeciality
@Override
@Transactional(rollbackFor = Exception.class)
public R<String> updateSpeciality(SpecialityUpdateDTO updateDTO) {
String msg;
Long userId = ContextUtil.getUserId();
//模拟管理员操作
//userId=1452475321122029568L;
List<Role> roleList = roleService.findRoleByUserId(userId);
判断当前用户是不是内置管理员
if (null == roleList || roleList.isEmpty()) {
msg = "通过用户id查找所属角色信息失败,userId:" userId;
log.error(msg);
return R.fail(msg);
}
Set<String> roleCodeSet = new HashSet<>();
roleList.forEach(role -> roleCodeSet.add(role.getCode()));
判断当前用户是不是内置管理员
if (!roleCodeSet.contains(SUPER_ADMIN)) {
msg = "当前用户非内置管理员,无权进行新增专业操作";
log.error(msg);
return R.fail(msg);
}
Long id = updateDTO.getId();
String specialityName = updateDTO.getSpecialityName();
先判断id合法性 防穿透
if (null == id || id < 0) {
msg = "请输入有效的id!";
log.error(msg);
return R.fail(msg);
}
有效性验证
TeamSpeciality oldSpeciality = teamSpecialityService.getById(id);
if (null == oldSpeciality) {
msg = "请输入有效的id!";
log.error(msg);
return R.fail(msg);
}
专业名不能为空
if(null==specialityName||specialityName.isEmpty()){
msg = "专业名不能为空!";
log.error(msg);
return R.fail(msg);
}
专业名不能重复(除自己以外,因为有可能只是改自己专业的描述)
List<TeamSpeciality> specialityList = teamSpecialityService.lambdaQuery().eq(TeamSpeciality::getStates, VALID_STATUS).list();
Iterator<TeamSpeciality> it = specialityList.iterator();
while (it.hasNext()) {
TeamSpeciality eachSpeciality = it.next();
if (oldSpeciality.getId() == eachSpeciality.getId()) {
it.remove();
}
}
for (int i = 0; i < specialityList.size(); i ) {
if (specialityName.equals(specialityList.get(i).getSpecialityName())) {
msg = "此专业名已存在,请重新输入!";
log.error(msg);
return R.fail(msg);
}
}
BeanUtils.copyProperties(updateDTO, oldSpeciality);
if (!teamSpecialityService.updateById(oldSpeciality)) {
msg = "修改失败,请稍后重试!";
log.error(msg);
return R.fail(msg);
}
msg = "修改专业成功!";
return R.success(msg);
}
删除专业deleteSpeciality
@Override
@Transactional(rollbackFor = Exception.class)
public R<String> deleteSpeciality(SpecialityUpdateDTO updateDTO) {
String msg;
Long userId = ContextUtil.getUserId();
//模拟管理员操作
//userId=1452475321122029568L;
List<Role> roleList = roleService.findRoleByUserId(userId);
当前用户需要在<用户_角色表>配置角色
if (null == roleList || roleList.isEmpty()) {
msg = "通过用户id查找所属角色信息失败,userId:" userId;
log.error(msg);
return R.fail(msg);
}
Set<String> roleCodeSet = new HashSet<>();
roleList.forEach(role -> roleCodeSet.add(role.getCode()));
判断当前用户是不是内置管理员
if (!roleCodeSet.contains(SUPER_ADMIN)) {
msg = "当前用户非内置管理员,无权进行新增专业操作";
log.error(msg);
return R.fail(msg);
}
Long id = updateDTO.getId();
id合法性 穿透
if (null == id || id < 0) {
msg = "请输入有效的id!";
log.error(msg);
return R.fail(msg);
}
有效性验证
TeamSpeciality oldSpeciality = teamSpecialityService.getById(id);
if (null == oldSpeciality) {
msg = "请输入有效的id!";
log.error(msg);
return R.fail(msg);
}
如果还有其他关联信息,就不能删除
List<Team> teamList = teamService.lambdaQuery().eq(Team::getSpecialityId, id).list();
if (null != teamList) {
msg = "该专业在班组详情中尚有关联信息,不可删除!";
log.error(msg);
return R.fail(msg);
}
if (!teamSpecialityService.removeById(id)) {
msg = "删除失败,请稍后重试!";
log.error(msg);
return R.fail(msg);
}
return R.success("删除专业信息成功!");
}
用于新增的入参类:SaveDTO
-
-
-
-
-
-
-
-
-
public class SpecialitySaveDTO implements Serializable {
-
private static final long serialVersionUID = 1L;
-
-
-
-
-
protected String specialityName;
-
-
-
-
-
protected String description;
-
-
-
-
}
用于删除、修改、查询时的入参类:SpecialityUpdateDTO
-
-
-
-
-
-
-
-
-
public class SpecialityUpdateDTO implements Serializable {
-
private static final long serialVersionUID = 1L;
-
-
-
-
-
private Long id;
-
-
-
-
protected String specialityName;
-
-
-
-
-
protected String description;
-
-
-
-
}
控制层
-
-
-
-
public R<String> addSpeciality( SpecialitySaveDTO saveDTO) {
-
return baseService.addSpeciality(saveDTO);
-
}
-
-
-
-
-
public R<String> deleteSpeciality( SpecialityUpdateDTO updateDTO) {
-
return baseService.deleteSpeciality(updateDTO);
-
}
-
-
-
-
-
public R<String> updateSpeciality( SpecialityUpdateDTO updateDTO) {
-
return baseService.updateSpeciality(updateDTO);
-
}
-
-
-
-
-
public R<TeamSpeciality> selectSpecialityById( SpecialityUpdateDTO updateDTO) {
-
TeamSpeciality teamSpeciality = teamSpecialityService.getById(updateDTO.getId());
-
CommonUtil.notNull(teamSpeciality, "该专业不存在,请联系管理员!");
-
return R.success(teamSpeciality);
-
}
-
-
-
-
-
public R<List<TeamSpeciality>> selectSpecialityListOrList( SpecialityUpdateDTO updateDTO) {
-
List<TeamSpeciality> teamSpecialities;
-
if (null != updateDTO.getSpecialityName() && !updateDTO.getSpecialityName().isEmpty()) {
-
teamSpecialities = teamSpecialityService.lambdaQuery()
-
.like(TeamSpeciality::getSpecialityName, updateDTO.getSpecialityName())
-
.eq(TeamSpeciality::getStates, VALID_STATUS).list();
-
} else {
-
teamSpecialities = teamSpecialityService.lambdaQuery().eq(TeamSpeciality::getStates, VALID_STATUS).list();
-
}
-
return R.success(teamSpecialities);
-
}
Service (三个查询的接口都在控制层使用lambdaQuery查询,直接返回即可)
-
* 新增专业
-
*/
-
R<String> addSpeciality(SpecialitySaveDTO saveDTO);
-
-
/**
-
* 修改专业
-
*/
-
R<String> updateSpeciality(SpecialityUpdateDTO updateDTO);
-
-
/**
-
* 删除专业
-
*/
-
R<String> deleteSpeciality(SpecialityUpdateDTO updateDTO);
ServiceImpl
-
@Override
-
@Transactional(rollbackFor = Exception.class)
-
public R<String> addSpeciality(SpecialitySaveDTO saveDTO) {
-
String msg;
-
Long userId = ContextUtil.getUserId();
-
//userId=1452475321122029568L;
-
List<Role> roleList = roleService.findRoleByUserId(userId);
-
if (null == roleList || roleList.isEmpty()) {
-
msg = "通过用户id查找所属角色信息失败,userId:" userId;
-
log.error(msg);
-
return R.fail(msg);
-
}
-
Set<String> roleCodeSet = new HashSet<>();
-
roleList.forEach(role -> roleCodeSet.add(role.getCode()));
-
//判断当前用户是不是内置管理员
-
if (!roleCodeSet.contains(SUPER_ADMIN)) {
-
msg = "当前用户非内置管理员,无权进行新增专业操作";
-
log.error(msg);
-
return R.fail(msg);
-
}
-
-
String specialityName = saveDTO.getSpecialityName();
-
-
//专业名不能为空
-
if(null==specialityName||specialityName.isEmpty()){
-
msg = "专业名不能为空!";
-
log.error(msg);
-
return R.fail(msg);
-
}
-
-
//专业名不能重复
-
List<TeamSpeciality> specialityList = teamSpecialityService.lambdaQuery().eq(TeamSpeciality::getSpecialityName, specialityName).list();
-
if (specialityList.size() > 0) {
-
msg = "此专业名已存在,请重新输入!";
-
log.error(msg);
-
return R.fail(msg);
-
}
-
-
TeamSpeciality teamSpeciality = new TeamSpeciality();
-
-
BeanUtils.copyProperties(saveDTO, teamSpeciality);
-
teamSpeciality.setStates(VALID_STATUS);
-
-
if (!teamSpecialityService.save(teamSpeciality)) {
-
msg = "新增失败,请稍后重试!";
-
log.error(msg);
-
return R.fail(msg);
-
}
-
msg = "新增专业成功!";
-
return R.success(msg);
-
-
}
-
-
@Override
-
@Transactional(rollbackFor = Exception.class)
-
public R<String> updateSpeciality(SpecialityUpdateDTO updateDTO) {
-
String msg;
-
Long userId = ContextUtil.getUserId();
-
//模拟管理员操作
-
//userId=1452475321122029568L;
-
List<Role> roleList = roleService.findRoleByUserId(userId);
-
if (null == roleList || roleList.isEmpty()) {
-
msg = "通过用户id查找所属角色信息失败,userId:" userId;
-
log.error(msg);
-
return R.fail(msg);
-
}
-
Set<String> roleCodeSet = new HashSet<>();
-
roleList.forEach(role -> roleCodeSet.add(role.getCode()));
-
//判断当前用户是不是内置管理员
-
if (!roleCodeSet.contains(SUPER_ADMIN)) {
-
msg = "当前用户非内置管理员,无权进行新增专业操作";
-
log.error(msg);
-
return R.fail(msg);
-
}
-
-
Long id = updateDTO.getId();
-
String specialityName = updateDTO.getSpecialityName();
-
//id合法性 穿透
-
if (null == id || id < 0) {
-
msg = "请输入有效的id!";
-
log.error(msg);
-
return R.fail(msg);
-
}
-
//有效性验证
-
TeamSpeciality oldSpeciality = teamSpecialityService.getById(id);
-
if (null == oldSpeciality) {
-
msg = "请输入有效的id!";
-
log.error(msg);
-
return R.fail(msg);
-
}
-
-
//专业名不能为空
-
if(null==specialityName||specialityName.isEmpty()){
-
msg = "专业名不能为空!";
-
log.error(msg);
-
return R.fail(msg);
-
}
-
-
//专业名不能重复(除自己以外,因为有可能只是改自己专业的描述)
-
List<TeamSpeciality> specialityList = teamSpecialityService.lambdaQuery().eq(TeamSpeciality::getStates, VALID_STATUS).list();
-
-
Iterator<TeamSpeciality> it = specialityList.iterator();
-
while (it.hasNext()) {
-
TeamSpeciality eachSpeciality = it.next();
-
if (oldSpeciality.getId() == eachSpeciality.getId()) {
-
it.remove();
-
}
-
}
-
-
for (int i = 0; i < specialityList.size(); i ) {
-
if (specialityName.equals(specialityList.get(i).getSpecialityName())) {
-
msg = "此专业名已存在,请重新输入!";
-
log.error(msg);
-
return R.fail(msg);
-
}
-
}
-
-
BeanUtils.copyProperties(updateDTO, oldSpeciality);
-
if (!teamSpecialityService.updateById(oldSpeciality)) {
-
msg = "修改失败,请稍后重试!";
-
log.error(msg);
-
return R.fail(msg);
-
}
-
-
msg = "修改专业成功!";
-
return R.success(msg);
-
-
}
-
-
@Override
-
@Transactional(rollbackFor = Exception.class)
-
public R<String> deleteSpeciality(SpecialityUpdateDTO updateDTO) {
-
String msg;
-
Long userId = ContextUtil.getUserId();
-
//模拟管理员操作
-
//userId=1452475321122029568L;
-
List<Role> roleList = roleService.findRoleByUserId(userId);
-
if (null == roleList || roleList.isEmpty()) {
-
msg = "通过用户id查找所属角色信息失败,userId:" userId;
-
log.error(msg);
-
return R.fail(msg);
-
}
-
Set<String> roleCodeSet = new HashSet<>();
-
roleList.forEach(role -> roleCodeSet.add(role.getCode()));
-
//判断当前用户是不是内置管理员
-
if (!roleCodeSet.contains(SUPER_ADMIN)) {
-
msg = "当前用户非内置管理员,无权进行新增专业操作";
-
log.error(msg);
-
return R.fail(msg);
-
}
-
-
Long id = updateDTO.getId();
-
//id合法性 穿透
-
if (null == id || id < 0) {
-
msg = "请输入有效的id!";
-
log.error(msg);
-
return R.fail(msg);
-
}
-
//有效性验证
-
TeamSpeciality oldSpeciality = teamSpecialityService.getById(id);
-
if (null == oldSpeciality) {
-
msg = "请输入有效的id!";
-
log.error(msg);
-
return R.fail(msg);
-
}
-
-
List<Team> teamList = teamService.lambdaQuery().eq(Team::getSpecialityId, id).list();
-
if (null != teamList&&!teamList.isEmpty()) {
-
msg = "该专业在班组详情中尚有关联信息,不可删除!";
-
log.error(msg);
-
return R.fail(msg);
-
}
-
-
-
if (!teamSpecialityService.removeById(id)) {
-
msg = "删除失败,请稍后重试!";
-
log.error(msg);
-
return R.fail(msg);
-
}
-
-
return R.success("删除专业信息成功!");
-
-
-
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhiebfki
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
excel下划线不显示怎么办
PHP中文网 06-23 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
微信运动停用后别人还能看到步数吗
PHP中文网 07-22 -
excel打印预览压线压字怎么办
PHP中文网 06-22