perf: 优化批量保存代码

This commit is contained in:
CaptainB 2023-06-01 10:36:15 +08:00 committed by 刘瑞斌
parent 9b6be2e36d
commit 56ce70a82c
3 changed files with 31 additions and 45 deletions

View File

@ -29,15 +29,20 @@ public class UserController {
} }
@PostMapping("/add") @PostMapping("/add")
public boolean addUser(@Validated({Created.class}) @RequestBody UserDTO user) { public UserDTO addUser(@Validated({Created.class}) @RequestBody UserDTO user) {
return userService.add(user); return userService.add(user);
} }
@PostMapping("/update") @PostMapping("/update")
public boolean updateUser(@Validated({Updated.class}) @RequestBody UserDTO user) { public UserDTO updateUser(@Validated({Updated.class}) @RequestBody UserDTO user) {
return userService.update(user); return userService.update(user);
} }
@GetMapping("/delete/{userId}")
public UserDTO deleteUser(@PathVariable String userId) {
return userService.delete(userId);
}
@PostMapping("/batch-add2") @PostMapping("/batch-add2")
public boolean batchSaveUser2(@Validated({Created.class}) @RequestBody List<User> user) { public boolean batchSaveUser2(@Validated({Created.class}) @RequestBody List<User> user) {
return userService.batchSave2(user); return userService.batchSave2(user);

View File

@ -29,7 +29,7 @@ public class UserService {
@Resource @Resource
private SqlSessionFactory sqlSessionFactory; private SqlSessionFactory sqlSessionFactory;
public boolean add(UserDTO entity) { public UserDTO add(UserDTO entity) {
// todo 后台直接获取在线用户 // todo 后台直接获取在线用户
entity.setCreateUser("admin"); entity.setCreateUser("admin");
entity.setCreateTime(System.currentTimeMillis()); entity.setCreateTime(System.currentTimeMillis());
@ -39,10 +39,10 @@ public class UserService {
UserExtend userExtend = new UserExtend(); UserExtend userExtend = new UserExtend();
BeanUtils.copyBean(userExtend, entity); BeanUtils.copyBean(userExtend, entity);
userExtendMapper.insert(userExtend); userExtendMapper.insert(userExtend);
return true; return entity;
} }
public boolean update(UserDTO entity) { public UserDTO update(UserDTO entity) {
entity.setCreateUser(null); entity.setCreateUser(null);
entity.setCreateTime(null); entity.setCreateTime(null);
entity.setUpdateTime(System.currentTimeMillis()); entity.setUpdateTime(System.currentTimeMillis());
@ -53,7 +53,7 @@ public class UserService {
BeanUtils.copyBean(userExtend, entity); BeanUtils.copyBean(userExtend, entity);
userExtendMapper.updateByPrimaryKeySelective(userExtend); userExtendMapper.updateByPrimaryKeySelective(userExtend);
} }
return true; return baseUserMapper.selectById(entity.getId());
} }
public UserDTO getById(String id) { public UserDTO getById(String id) {
@ -87,11 +87,12 @@ public class UserService {
int batchSize = 100; int batchSize = 100;
int size = users.size(); int size = users.size();
int pageSize = size / batchSize; int pageSize = size / batchSize;
if (pageSize == 0) {
baseUserMapper.batchSave(users); users.forEach(user -> {
System.out.println("batch save cost: " + (System.currentTimeMillis() - start) + "ms"); user.setCreateUser("admin");
return true; user.setCreateTime(System.currentTimeMillis());
} user.setUpdateTime(System.currentTimeMillis());
});
for (int i = 0; i < pageSize; i++) { for (int i = 0; i < pageSize; i++) {
int startIndex = i * batchSize; int startIndex = i * batchSize;
@ -112,4 +113,10 @@ public class UserService {
return userMapper.countByExample(new UserExample()); return userMapper.countByExample(new UserExample());
} }
public UserDTO delete(String userId) {
UserDTO userDTO = baseUserMapper.selectById(userId);
userMapper.deleteByPrimaryKey(userId);
userExtendMapper.deleteByPrimaryKey(userId);
return userDTO;
}
} }

View File

@ -85,43 +85,22 @@ public class UserControllerTests {
.andExpect(content().contentType(MediaType.APPLICATION_JSON)); .andExpect(content().contentType(MediaType.APPLICATION_JSON));
} }
@Test @Test
@Order(5) @Order(5)
public void testSelectAll() throws Exception { public void testDeleteUser() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/user/list-all"))
mockMvc.perform(MockMvcRequestBuilders.get("/user/delete/admin"))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.data[0].id").value("admin")); .andExpect(jsonPath("$.data.id").value("admin"));
} }
@Test
// @Test
@Order(6)
public void testBatchAddUser2() throws Exception {
var users = new ArrayList<User>();
for (int i = 0; i < 1000; i++) {
User user = new User();
user.setId("batch2_" + i);
user.setName("batch2_" + i);
user.setSource("LOCAL");
user.setEmail("bin@fit2cloud.com");
user.setStatus("enabled");
users.add(user);
}
mockMvc.perform(MockMvcRequestBuilders.post("/user/batch-add2")
.content(JSON.toJSONString(users))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
}
// @Test
@Order(7) @Order(7)
public void testBatchAddUser3() throws Exception { public void testBatchAddUser3() throws Exception {
var users = new ArrayList<User>(); var users = new ArrayList<User>();
for (int i = 0; i < 1000; i++) { int size = 123;
for (int i = 0; i < size; i++) {
User user = new User(); User user = new User();
user.setId("batch3_" + i); user.setId("batch3_" + i);
user.setName("batch3_" + i); user.setName("batch3_" + i);
@ -136,16 +115,11 @@ public class UserControllerTests {
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)); .andExpect(content().contentType(MediaType.APPLICATION_JSON));
}
// @Test
@Order(8)
public void testCount() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/user/count") mockMvc.perform(MockMvcRequestBuilders.get("/user/count")
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.data").value(3001)); .andExpect(jsonPath("$.data").value(size));
} }
} }