Mybatis-plus增删改查

Mybatis-plus增删改查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

@Autowired
private UserMapper commonMapper;
@Autowired
private UserService commonService;
private final String id = "User_id";

@Operation(summary = "列表")
@PostMapping("/list")
public Result listUser() {
return Result.success("数据列表", commonService.list());
}

@Operation(summary = "存在")
@PostMapping("/exist")
public Result existUser(@RequestBody @Validated IdParam param) {
QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
wrapper.eq(id.toLowerCase(Locale.ROOT), param.getId());
long count = commonService.count(wrapper);
if (count == 0) return Result.success("ID不存在", false);
return Result.success("ID已存在", true);
}

@Operation(summary = "保存")
@PostMapping("/insert")
public Result insertUser(@RequestBody @Validated UserEntity param) {
QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
wrapper.eq(id.toLowerCase(Locale.ROOT), param.getUserId());
if (commonService.count(wrapper) != 0) return Result.failure("ID已存在", new Date());
if (commonService.save(param)) return Result.success("保存成功", new Date());
return Result.failure("保存失败", new Date());
}

@Operation(summary = "删除")
@PostMapping("/delete")
public Result deleteUser(@RequestBody @Validated IdParam param) {
QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
wrapper.eq(id.toLowerCase(Locale.ROOT), param.getId());
if (commonService.count(wrapper) == 0) return Result.failure("ID不存在", param.getId());
if (commonService.remove(wrapper)) return Result.success("删除成功", param.getId());
return Result.failure("删除失败", param.getId());
}

@Operation(summary = "修改")
@PostMapping("/update")
public Result updateUser(@RequestBody @Validated UserEntity param) {
QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
wrapper.eq(id.toLowerCase(Locale.ROOT), param.getUserId());
if (commonService.count(wrapper) == 0) return Result.failure("ID不存在", new Date());
if (commonService.updateById(param)) return Result.success("修改成功", new Date());
return Result.failure("修改失败", new Date());
}

@Operation(summary = "查询")
@PostMapping("/select")
public Result selectUser(@RequestBody @Validated IdParam param) {
QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
wrapper.eq(id.toLowerCase(Locale.ROOT), param.getId());
if (commonService.count(wrapper) == 0) return Result.failure("ID不存在", param.getId());
return Result.success(commonService.getOne(wrapper));
}


@Operation(summary = "查询byAcc")
@PostMapping("/selectByAcc/{param}")
public Result selectUserByAcc(@PathVariable @Validated String param) {
QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
wrapper.eq("user_acc", param);
if (commonService.count(wrapper) == 0) return Result.failure("ID不存在", param);
return Result.success(commonService.getOne(wrapper));
}


@Operation(summary = "列表分页")
@PostMapping("/listPage")
public Result listPageUser(@RequestParam Integer page, @RequestParam Integer pageSize) {
//分页参数
Page<UserEntity> rowPage = new Page(page, pageSize);
//queryWrapper组装查询where条件
LambdaQueryWrapper<UserEntity> queryWrapper = new LambdaQueryWrapper<>();
rowPage = commonMapper.selectPage(rowPage, queryWrapper);
return Result.success("数据列表", rowPage);
}

@Operation(summary = "导出数据")
@PostMapping("exportExcel")
public void exportExcelUser(HttpServletResponse response) throws IOException {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("轮播图", StandardCharsets.UTF_8).replaceAll("\\+", "%20");
List<UserEntity> list = commonService.list();
response.setHeader("Content-disposition", "attachment;filename*=" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), UserEntity.class).sheet("轮播图").doWrite(list);
}

@Operation(summary = "导入数据")
@PostMapping("/importExcel")
public Result importExcelUser(MultipartFile file) {
try {
//获取文件的输入流
InputStream inputStream = file.getInputStream();
List<UserEntity> list = EasyExcel.read(inputStream) //调用read方法
//注册自定义监听器,字段校验可以在监听器内实现
//.registerReadListener(new UserListener())
.head(UserEntity.class) //对应导入的实体类
.sheet(0) //导入数据的sheet页编号,0代表第一个sheet页,如果不填,则会导入所有sheet页的数据
.headRowNumber(1) //列表头行数,1代表列表头有1行,第二行开始为数据行
.doReadSync();//开始读Excel,返回一个List<T>集合,继续后续入库操作
//模拟导入数据库操作
for (UserEntity entity : list) {
commonService.saveOrUpdate(entity);
}
return Result.success("导入成功", new Date());
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2022-2024 何福海
  • 访问人数: | 浏览次数:

请我喝杯奶茶吧~

支付宝
微信