refactor: 使用 Hutool工具类优化空值判断- 在多个控制器和服务实现类中,将原始的空值判断替换为 Hutool 工具类中的方法
- 使用 ObjectUtil.isNull() 替换原始的 == null 判断 - 使用 ObjectUtil.isNotNull() 替换原始的 != null 判断 - 在某些情况下,使用 StrUtil.isBlank() 替换原始的 == null 判断,以更准确地检查字符串是否为空
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.test.bijihoudaun.controller;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.test.bijihoudaun.common.response.R;
|
||||
import com.test.bijihoudaun.entity.Grouping;
|
||||
import com.test.bijihoudaun.service.GroupingService;
|
||||
@@ -24,7 +25,7 @@ public class GroupingController {
|
||||
@Operation(summary = "创建分组")
|
||||
@PostMapping
|
||||
public R<Grouping> createGrouping(@RequestBody Grouping grouping) {
|
||||
if (grouping.getParentId() == null) {
|
||||
if (ObjectUtil.isNull(grouping.getParentId())) {
|
||||
grouping.setParentId(0L);
|
||||
}
|
||||
Grouping created = groupingService.createGrouping(grouping);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.test.bijihoudaun.controller;
|
||||
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.test.bijihoudaun.common.response.R;
|
||||
import com.test.bijihoudaun.entity.Image;
|
||||
import com.test.bijihoudaun.service.ImageService;
|
||||
@@ -63,7 +64,7 @@ public class ImageController {
|
||||
@GetMapping("/preview/{url}")
|
||||
@Operation(summary = "在线预览", description = "浏览器直接打开文件流")
|
||||
public void preview(@PathVariable String url, HttpServletResponse resp) throws IOException {
|
||||
if (url == null) {
|
||||
if (StrUtil.isBlank(url)) {
|
||||
resp.setStatus(404);
|
||||
R.fail("文件不存在");
|
||||
}
|
||||
@@ -113,7 +114,7 @@ public class ImageController {
|
||||
* @return 对应的MIME类型
|
||||
*/
|
||||
private String getContentTypeFromFileExtension(String fileName) {
|
||||
if (fileName == null || fileName.lastIndexOf('.') == -1) {
|
||||
if (StrUtil.isBlank(fileName) || !StrUtil.contains(fileName, '.')) {
|
||||
return "application/octet-stream";
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.test.bijihoudaun.controller;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.test.bijihoudaun.common.response.R;
|
||||
import com.test.bijihoudaun.entity.MarkdownFile;
|
||||
import com.test.bijihoudaun.entity.MarkdownFileVO;
|
||||
@@ -36,7 +37,7 @@ public class MarkdownController {
|
||||
@GetMapping("/{id}")
|
||||
public R<String> getMarkdownContent(@PathVariable Long id) {
|
||||
MarkdownFile file = markdownFileService.getMarkdownById(id);
|
||||
if (file != null) {
|
||||
if (ObjectUtil.isNotNull(file)) {
|
||||
return R.success(file.getContent());
|
||||
}
|
||||
return R.fail();
|
||||
@@ -89,7 +90,7 @@ public class MarkdownController {
|
||||
@PathVariable Long id,
|
||||
@RequestBody String title) {
|
||||
MarkdownFile updatedFile = markdownFileService.updateMarkdownTitle(id, title);
|
||||
if (updatedFile != null) {
|
||||
if (ObjectUtil.isNotNull(updatedFile)) {
|
||||
return R.success(updatedFile);
|
||||
}
|
||||
return R.fail("文件未找到或更新失败");
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.test.bijihoudaun.controller;
|
||||
|
||||
import com.test.bijihoudaun.bo.UpdatePasswordBo;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.test.bijihoudaun.common.response.R;
|
||||
import com.test.bijihoudaun.entity.User;
|
||||
import com.test.bijihoudaun.service.RegistrationCodeService;
|
||||
@@ -69,7 +70,7 @@ public class UserController {
|
||||
String username = userDetails.getUsername();
|
||||
User user = userService.getOne(new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<User>().eq("username", username));
|
||||
|
||||
if (user == null) {
|
||||
if (ObjectUtil.isNull(user)) {
|
||||
return R.fail("无法获取用户信息,删除失败");
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.test.bijihoudaun.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.test.bijihoudaun.entity.Grouping;
|
||||
@@ -39,7 +40,7 @@ public class GroupingServiceImpl
|
||||
|
||||
@Override
|
||||
public List<Grouping> getAllGroupings(Long parentId) {
|
||||
if (parentId == null){
|
||||
if (ObjectUtil.isNull(parentId)){
|
||||
return groupingMapper.selectList(null);
|
||||
}
|
||||
return groupingMapper.selectList(new LambdaQueryWrapper<Grouping>()
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.test.bijihoudaun.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.stream.CollectorUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
@@ -68,7 +69,7 @@ public class ImageServiceImpl
|
||||
@Override
|
||||
public boolean deleteImage(Long id) {
|
||||
Image image = imageMapper.selectById(id);
|
||||
if (image == null) {
|
||||
if (ObjectUtil.isNull(image)) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
@@ -87,7 +88,7 @@ public class ImageServiceImpl
|
||||
@Override
|
||||
public boolean deleteImageByUrl(String url) {
|
||||
Image image = imageMapper.selectOne(new QueryWrapper<Image>().eq("url", url));
|
||||
if (image == null) {
|
||||
if (ObjectUtil.isNull(image)) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
@@ -109,7 +110,7 @@ public class ImageServiceImpl
|
||||
}
|
||||
for (String url : urls) {
|
||||
Image image = imageMapper.selectOne(new QueryWrapper<Image>().eq("url", url));
|
||||
if (image != null) {
|
||||
if (ObjectUtil.isNotNull(image)) {
|
||||
this.deleteImageByUrl(url);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.test.bijihoudaun.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
@@ -40,7 +41,7 @@ public class MarkdownFileServiceImpl
|
||||
long id;
|
||||
markdownFile.setUpdatedAt(new Date());
|
||||
// 如果ID为空或0,则视为新文件
|
||||
if (markdownFile.getId() == null || markdownFile.getId() == 0L) {
|
||||
if (ObjectUtil.isNull(markdownFile.getId()) || markdownFile.getId() == 0L) {
|
||||
long l = snowflakeIdGenerator.nextId();
|
||||
markdownFile.setId(l);
|
||||
markdownFile.setCreatedAt(new Date());
|
||||
@@ -97,7 +98,7 @@ public class MarkdownFileServiceImpl
|
||||
@Override
|
||||
public MarkdownFile updateMarkdownTitle(Long id, String title) {
|
||||
MarkdownFile file = this.getById(id);
|
||||
if (file != null) {
|
||||
if (ObjectUtil.isNotNull(file)) {
|
||||
file.setTitle(title);
|
||||
file.setUpdatedAt(new Date());
|
||||
this.updateById(file);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.test.bijihoudaun.service.impl;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.test.bijihoudaun.entity.RegistrationCode;
|
||||
@@ -36,7 +37,7 @@ public class RegistrationCodeServiceImpl extends ServiceImpl<RegistrationCodeMap
|
||||
queryWrapper.eq("code", code);
|
||||
RegistrationCode registrationCode = getOne(queryWrapper);
|
||||
|
||||
if (registrationCode == null) {
|
||||
if (ObjectUtil.isNull(registrationCode)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.test.bijihoudaun.service.impl;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.test.bijihoudaun.entity.SystemSetting;
|
||||
import com.test.bijihoudaun.mapper.SystemSettingMapper;
|
||||
@@ -17,7 +18,7 @@ public class SystemSettingServiceImpl extends ServiceImpl<SystemSettingMapper, S
|
||||
public boolean isRegistrationEnabled() {
|
||||
SystemSetting setting = getById(REGISTRATION_ENABLED_KEY);
|
||||
// 默认开启注册
|
||||
return setting == null || Boolean.parseBoolean(setting.getSettingValue());
|
||||
return ObjectUtil.isNull(setting) || Boolean.parseBoolean(setting.getSettingValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.test.bijihoudaun.service.impl;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
@@ -33,7 +34,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
User user = userMapper.findByUsername(username);
|
||||
if (user == null) {
|
||||
if (ObjectUtil.isNull(user)) {
|
||||
throw new UsernameNotFoundException("User not found with username: " + username);
|
||||
}
|
||||
return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), new ArrayList<>());
|
||||
@@ -85,13 +86,13 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
||||
.eq(User::getToken, token);
|
||||
User user = getOne(queryWrapper);
|
||||
// 修改过期检查逻辑
|
||||
return user != null && new Date().before(user.getTokenEnddata());
|
||||
return ObjectUtil.isNotNull(user) && new Date().before(user.getTokenEnddata());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updatePassword(Long userId, UpdatePasswordBo updatePasswordBo) {
|
||||
User user = getById(userId);
|
||||
if (user == null) {
|
||||
if (ObjectUtil.isNull(user)) {
|
||||
throw new BusinessException("用户不存在");
|
||||
}
|
||||
if (!PasswordUtils.verify(updatePasswordBo.getOldPassword(), user.getPassword())) {
|
||||
|
||||
Reference in New Issue
Block a user