接口分析
今天写了一个删除用户运动指数的接口,运动指数包括了两个属性,一个每周运动次数和每次运动时间,我们需要做的就是把这两个字段清空,因为这个字段在user表中,同时我们删除他的时候不能把其他的属性删除,所以我们就不能直接使用delete方法,而是删除再添加 再修改。
问题与解决
重新启动项目时,数据库ip与配置文件中的地址不同导致报错,因为连的是我的数据库,所以我可以直接使用localhost但是这样我上传的项目给队友拷贝下来的就又会有问题,所以我需要查看我自己电脑的ip地址
Service
/**
* 用户Controller
*
* @author ruoyi
* @date 2023-03-24
*/
@RestController
@RequestMapping("/business/user")
public class TbUserController extends BaseController
{
// private String prefix = "system/user";
@Autowired
private ITbUserService tbUserService;
@Autowired
private ITbUserFamilyhistoryService tbUserFamilyhistoryService;
@Autowired
private ITbUserMedicineService tbUserMedicineService;
@Autowired
private ITbFamilyhistoryService tbFamilyhistoryService;
/**
* 删除患者用户运动情况新信息
* @param userId 用户id
* @return
*/
@DeleteMapping("/deleteMontionInfo")
public AjaxResult deleteMontionInfo(Long userId){
boolean flag = tbUserService.deleteMontionInfo(userId);
if (flag){
return AjaxResult.success("删除成功!");
}else {
return AjaxResult.error("删除失败!");
}
}
/**
* 删除患者用户体重指数信息
* @param userId 用户id
* @return
*/
@DeleteMapping("/deleteWeightInfo")
public AjaxResult deleteWeightInfo(Long userId){
boolean flag = tbUserService.deleteWeightInfo(userId);
if (flag){
return AjaxResult.success("删除成功!");
}else {
return AjaxResult.error("删除失败!");
}
}
/**
* 删除患者用户高血压敏感指数信息
* @param userId 用户id
* @return
*/
@DeleteMapping("/deleteBloodInfo")
public AjaxResult deleteBloodInfo(Long userId){
boolean flag = tbUserService.deleteBloodInfo(userId);
if (flag){
return AjaxResult.success("删除成功!");
}else {
return AjaxResult.error("删除失败!");
}
}
}
/**
* 血压Controller
*
* @author ruoyi
* @date 2023-03-29
*/
@RestController
@RequestMapping("/business/blood")
public class TbBloodController extends BaseController
{
@Autowired
private ITbBloodService tbBloodService;
/**
* 删除血压指数信息
* @param userId 用户id
* @return
*/
@DeleteMapping("/deleteBlood")
public AjaxResult deleteBlood(Long userId){
boolean flag = tbBloodService.deleteBlood(userId);
if (flag){
return AjaxResult.success("删除成功!");
}else {
return AjaxResult.error("删除失败!");
}
}
}
Impl
/**
* 用户Service业务层处理
*
* @author ruoyi
* @date 2023-03-24
*/
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper,TbUser> implements ITbUserService
{
@Autowired
private TbUserMapper tbUserMapper;
@Autowired
private ITbUserService iTbUserService;
@Autowired
private ITbDoctorService iTbDoctorService;
@Autowired
private ITbFamilyhistoryService iTbFamilyhistoryService;
@Autowired
private ITbUserFamilyhistoryService iTbUserFamilyhistoryService;
@Autowired
private ITbUserMedicineService iTbUserMedicineService;
/**
* 删除用户运动指数信息
* @param userId 用户id
*/
@Override
public boolean deleteMontionInfo(Long userId) {
//先得到这个用户的信息
LambdaQueryWrapper<TbUser> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(TbUser::getUserId,userId);
TbUser one = iTbUserService.getOne(queryWrapper);
//让这两个数据不进行数据库的操作,相当于就是直接把属性设置为null
one.setMotionNumber(null);
one.setMotionTime(null);
//创建一个容器,用来装原先的数据
TbUser tbUser = new TbUser();
tbUser.setUserId(one.getUserId());
//先进行删除
boolean remove = iTbUserService.remove(queryWrapper);
//进行添加
boolean save = iTbUserService.save(tbUser);
//进行修改,确保update_time可以成功更改
boolean update = iTbUserService.update(one, queryWrapper);
return remove && save && update;
}
/**
* 删除用户体重指数信息
* @param userId 用户id
* @return
*/
@Override
public boolean deleteWeightInfo(Long userId) {
//先得到这个用户的信息
LambdaQueryWrapper<TbUser> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(TbUser::getUserId,userId);
TbUser one = iTbUserService.getOne(queryWrapper);
//让这两个数据不进行数据库的操作,相当于就是直接把属性设置为null
one.setWeight(null);
one.setHeight(null);
//创建一个容器,用来装原先的数据
TbUser tbUser = new TbUser();
tbUser.setUserId(one.getUserId());
//先进行删除
boolean remove = iTbUserService.remove(queryWrapper);
//进行添加
boolean save = iTbUserService.save(tbUser);
//进行修改,确保update_time可以成功更改
boolean update = iTbUserService.update(one, queryWrapper);
return remove && save && update;
}
/**
* 删除高血压敏感指数信息
* @param userId 用户id
* @return
*/
@Override
public boolean deleteBloodInfo(Long userId) {
//去用户与家族病史表中对数据进行删除
LambdaQueryWrapper<TbUserFamilyhistory> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(TbUserFamilyhistory::getUserId,userId);
boolean remove = iTbUserFamilyhistoryService.remove(queryWrapper);
//对用户的饮食习惯进行删除
LambdaQueryWrapper<TbUser> queryWrapper1 = new LambdaQueryWrapper<>();
queryWrapper1.eq(TbUser::getUserId,userId);
TbUser one2 = iTbUserService.getOne(queryWrapper1);
//不然饮食习惯这个字段进行数据库操作
one2.setDiet(null);
//先进行删除
boolean remove1 = iTbUserService.remove(queryWrapper1);
//创建一个容器 用来装数据
TbUser tbUser = new TbUser();
tbUser.setUserId(one2.getUserId());
//先保存,再进行修改,修改的目的就是更改update_time 字段
boolean save = iTbUserService.save(tbUser);
boolean update = iTbUserService.update(one2, queryWrapper1);
//对服药表中的数据进行删除
LambdaQueryWrapper<TbUserMedicine> queryWrapper2 = new LambdaQueryWrapper<>();
queryWrapper2.eq(TbUserMedicine::getUserId,userId);
boolean remove2 = iTbUserMedicineService.remove(queryWrapper2);
return remove && remove1 && save && update && remove2;
}
}
/**
* 血压Service业务层处理
*
* @author ruoyi
* @date 2023-03-29
*/
@Service
public class TbBloodServiceImpl extends ServiceImpl<TbBloodMapper,TbBlood> implements ITbBloodService
{
/**
* 删除用户血压指数信息
* @param userId 患者用户id
* @return
*/
@Override
public boolean deleteBlood(Long userId) {
//根据用户id去用户表中找到血压id
LambdaQueryWrapper<TbUser> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(TbUser::getUserId,userId);
TbUser one = iTbUserService.getOne(queryWrapper);
//先把这个的血压id进行记录
String bloodId = one.getBloodId();
//不让这个属性参加数据库运算
one.setBloodId(null);
//删除这个用户信息
boolean remove = iTbUserService.remove(queryWrapper);
TbUser tbUser = new TbUser();
tbUser.setUserId(one.getUserId());
//添加一个空的用户信息 再把数据添加进去
boolean save = iTbUserService.save(one);
//进行数据的修改 更改update_time
boolean update = iTbUserService.update(one, queryWrapper);
//把血压表中的数据进行删除
LambdaQueryWrapper<TbBlood> queryWrapper1 = new LambdaQueryWrapper<>();
queryWrapper1.eq(TbBlood::getBloodId,bloodId);
boolean remove1 = iTbBloodService.remove(queryWrapper1);
return remove && save && update && remove1;
}
}
Controller
/**
* 血压Controller
*
* @author ruoyi
* @date 2023-03-29
*/
@RestController
@RequestMapping("/business/blood")
public class TbBloodController extends BaseController
{
@Autowired
private ITbBloodService tbBloodService;
/**
* 删除血压指数信息
* @param userId 用户id
* @return
*/
@DeleteMapping("/deleteBlood")
public AjaxResult deleteBlood(Long userId){
boolean flag = tbBloodService.deleteBlood(userId);
if (flag){
return AjaxResult.success("删除成功!");
}else {
return AjaxResult.error("删除失败!");
}
}
}
/**
* 用户Controller
*
* @author ruoyi
* @date 2023-03-24
*/
@RestController
@RequestMapping("/business/user")
public class TbUserController extends BaseController
{
@Autowired
private ITbUserService tbUserService;
@Autowired
private ITbUserFamilyhistoryService tbUserFamilyhistoryService;
@Autowired
private ITbUserMedicineService tbUserMedicineService;
@Autowired
private ITbFamilyhistoryService tbFamilyhistoryService;
/**
* 删除患者用户运动情况新信息
* @param userId 用户id
* @return
*/
@DeleteMapping("/deleteMontionInfo")
public AjaxResult deleteMontionInfo(Long userId){
boolean flag = tbUserService.deleteMontionInfo(userId);
if (flag){
return AjaxResult.success("删除成功!");
}else {
return AjaxResult.error("删除失败!");
}
}
/**
* 删除患者用户体重指数信息
* @param userId 用户id
* @return
*/
@DeleteMapping("/deleteWeightInfo")
public AjaxResult deleteWeightInfo(Long userId){
boolean flag = tbUserService.deleteWeightInfo(userId);
if (flag){
return AjaxResult.success("删除成功!");
}else {
return AjaxResult.error("删除失败!");
}
}
/**
* 删除患者用户高血压敏感指数信息
* @param userId 用户id
* @return
*/
@DeleteMapping("/deleteBloodInfo")
public AjaxResult deleteBloodInfo(Long userId){
boolean flag = tbUserService.deleteBloodInfo(userId);
if (flag){
return AjaxResult.success("删除成功!");
}else {
return AjaxResult.error("删除失败!");
}
}
}
总结
今天写了四个删除接口,删除接口主要的问题就是在同一张表中,多个字段不是用一个时间增加和删除的,同时还有个update_time这个字段,只要数据被修改,这个字段的值就应该被修改,所以删除的功能也要算,在用户表中,删除的一个功能,只删除几个字段,所以我们就要先把这条数据拿出来,把不需要的字段设置为null然后在把这个数据存进去,再进行修改就可以使update_time这个字段记录当前修改的时间。今天的删除功能其实原理都差不多,没有很大的难度,继续努力。
标签:脑干,return,删除,userId,用户,接口,boolean,软件,AjaxResult From: https://www.cnblogs.com/choajiwudibaolongzhanshi/p/17282451.html