perf: 优化批量保存代码
This commit is contained in:
parent
9b6be2e36d
commit
56ce70a82c
|
@ -29,15 +29,20 @@ public class UserController {
|
|||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public boolean addUser(@Validated({Created.class}) @RequestBody UserDTO user) {
|
||||
public UserDTO addUser(@Validated({Created.class}) @RequestBody UserDTO user) {
|
||||
return userService.add(user);
|
||||
}
|
||||
|
||||
@PostMapping("/update")
|
||||
public boolean updateUser(@Validated({Updated.class}) @RequestBody UserDTO user) {
|
||||
public UserDTO updateUser(@Validated({Updated.class}) @RequestBody UserDTO user) {
|
||||
return userService.update(user);
|
||||
}
|
||||
|
||||
@GetMapping("/delete/{userId}")
|
||||
public UserDTO deleteUser(@PathVariable String userId) {
|
||||
return userService.delete(userId);
|
||||
}
|
||||
|
||||
@PostMapping("/batch-add2")
|
||||
public boolean batchSaveUser2(@Validated({Created.class}) @RequestBody List<User> user) {
|
||||
return userService.batchSave2(user);
|
||||
|
|
|
@ -29,7 +29,7 @@ public class UserService {
|
|||
@Resource
|
||||
private SqlSessionFactory sqlSessionFactory;
|
||||
|
||||
public boolean add(UserDTO entity) {
|
||||
public UserDTO add(UserDTO entity) {
|
||||
// todo 后台直接获取在线用户
|
||||
entity.setCreateUser("admin");
|
||||
entity.setCreateTime(System.currentTimeMillis());
|
||||
|
@ -39,10 +39,10 @@ public class UserService {
|
|||
UserExtend userExtend = new UserExtend();
|
||||
BeanUtils.copyBean(userExtend, entity);
|
||||
userExtendMapper.insert(userExtend);
|
||||
return true;
|
||||
return entity;
|
||||
}
|
||||
|
||||
public boolean update(UserDTO entity) {
|
||||
public UserDTO update(UserDTO entity) {
|
||||
entity.setCreateUser(null);
|
||||
entity.setCreateTime(null);
|
||||
entity.setUpdateTime(System.currentTimeMillis());
|
||||
|
@ -53,7 +53,7 @@ public class UserService {
|
|||
BeanUtils.copyBean(userExtend, entity);
|
||||
userExtendMapper.updateByPrimaryKeySelective(userExtend);
|
||||
}
|
||||
return true;
|
||||
return baseUserMapper.selectById(entity.getId());
|
||||
}
|
||||
|
||||
public UserDTO getById(String id) {
|
||||
|
@ -87,11 +87,12 @@ public class UserService {
|
|||
int batchSize = 100;
|
||||
int size = users.size();
|
||||
int pageSize = size / batchSize;
|
||||
if (pageSize == 0) {
|
||||
baseUserMapper.batchSave(users);
|
||||
System.out.println("batch save cost: " + (System.currentTimeMillis() - start) + "ms");
|
||||
return true;
|
||||
}
|
||||
|
||||
users.forEach(user -> {
|
||||
user.setCreateUser("admin");
|
||||
user.setCreateTime(System.currentTimeMillis());
|
||||
user.setUpdateTime(System.currentTimeMillis());
|
||||
});
|
||||
|
||||
for (int i = 0; i < pageSize; i++) {
|
||||
int startIndex = i * batchSize;
|
||||
|
@ -112,4 +113,10 @@ public class UserService {
|
|||
return userMapper.countByExample(new UserExample());
|
||||
}
|
||||
|
||||
public UserDTO delete(String userId) {
|
||||
UserDTO userDTO = baseUserMapper.selectById(userId);
|
||||
userMapper.deleteByPrimaryKey(userId);
|
||||
userExtendMapper.deleteByPrimaryKey(userId);
|
||||
return userDTO;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -85,43 +85,22 @@ public class UserControllerTests {
|
|||
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
public void testSelectAll() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/user/list-all"))
|
||||
public void testDeleteUser() throws Exception {
|
||||
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/user/delete/admin"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(jsonPath("$.data[0].id").value("admin"));
|
||||
.andExpect(jsonPath("$.data.id").value("admin"));
|
||||
}
|
||||
|
||||
|
||||
// @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
|
||||
@Test
|
||||
@Order(7)
|
||||
public void testBatchAddUser3() throws Exception {
|
||||
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.setId("batch3_" + i);
|
||||
user.setName("batch3_" + i);
|
||||
|
@ -136,16 +115,11 @@ public class UserControllerTests {
|
|||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
|
||||
}
|
||||
|
||||
// @Test
|
||||
@Order(8)
|
||||
public void testCount() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/user/count")
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(jsonPath("$.data").value(3001));
|
||||
.andExpect(jsonPath("$.data").value(size));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue