Skip to content

Commit

Permalink
proxy for mybatis batch process
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzaiplus committed Jul 12, 2020
1 parent fee4119 commit 653a38e
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency>

</dependencies>

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/wangzaiplus/test/common/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public class Constant {

public static final int MAX_SIZE_PER_TIME = 1000;

public interface Redis {
String OK = "OK";
Integer EXPIRE_TIME_MINUTE = 60;// 过期时间, 60s, 一分钟
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/com/wangzaiplus/test/controller/TestController.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.wangzaiplus.test.controller;

import com.google.common.collect.Lists;
import com.wangzaiplus.test.annotation.AccessLimit;
import com.wangzaiplus.test.annotation.ApiIdempotent;
import com.wangzaiplus.test.common.ServerResponse;
import com.wangzaiplus.test.mapper.MsgLogMapper;
import com.wangzaiplus.test.mapper.UserMapper;
import com.wangzaiplus.test.pojo.Mail;
import com.wangzaiplus.test.pojo.User;
import com.wangzaiplus.test.service.TestService;
import com.wangzaiplus.test.service.batch.mapperproxy.MapperProxy;
import com.wangzaiplus.test.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
Expand All @@ -13,6 +19,8 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/test")
@Slf4j
Expand All @@ -21,6 +29,12 @@ public class TestController {
@Autowired
private TestService testService;

@Autowired
private UserMapper userMapper;

@Autowired
private MsgLogMapper msgLogMapper;

@ApiIdempotent
@PostMapping("testIdempotence")
public ServerResponse testIdempotence() {
Expand All @@ -42,4 +56,51 @@ public ServerResponse sendMail(@Validated Mail mail, Errors errors) {

return testService.send(mail);
}

@PostMapping("single")
public ServerResponse single(int size) {
List<User> list = Lists.newArrayList();

for (int i = 0; i < size; i++) {
String str = RandomUtil.UUID32();
User user = User.builder().username(str).password(str).build();
list.add(user);
}

long startTime = System.nanoTime();
log.info("batch insert costs: {} ms", (System.nanoTime() - startTime) / 1000000);

return ServerResponse.success();
}

@PostMapping("batchInsert")
public ServerResponse batchInsert(int size) {
List<User> list = Lists.newArrayList();

for (int i = 0; i < size; i++) {
String str = RandomUtil.UUID32();
User user = User.builder().username(str).password(str).build();
list.add(user);
}

new MapperProxy<User>(userMapper).batchInsert(list);

return ServerResponse.success();
}

@PostMapping("batchUpdate")
public ServerResponse batchUpdate(String ids) {
List<User> list = Lists.newArrayList();

String[] split = ids.split(",");
for (String id : split) {
User user = User.builder().id(Integer.valueOf(id)).username("batchUpdate_" + RandomUtil.UUID32()).password("123456").build();
list.add(user);
}

new MapperProxy<User>(userMapper).batchUpdate(list);

return ServerResponse.success();
}

}
3 changes: 2 additions & 1 deletion src/main/java/com/wangzaiplus/test/mapper/MsgLogMapper.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.wangzaiplus.test.mapper;

import com.wangzaiplus.test.pojo.MsgLog;
import com.wangzaiplus.test.service.batch.BatchProcessMapper;

import java.util.List;

public interface MsgLogMapper {
public interface MsgLogMapper extends BatchProcessMapper<MsgLog> {

void insert(MsgLog msgLog);

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/wangzaiplus/test/mapper/UserMapper.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.wangzaiplus.test.mapper;

import com.wangzaiplus.test.pojo.User;
import com.wangzaiplus.test.service.batch.BatchProcessMapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface UserMapper {
public interface UserMapper extends BatchProcessMapper<User> {

List<User> selectAll();

Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/wangzaiplus/test/mapper/UserMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,33 @@
FROM user
where username = #{username} and password = #{password}
</select>

<insert id="batchInsert" parameterType="list">
insert into user(username, password)
values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.username}, #{item.password})
</foreach>
</insert>

<insert id="batchUpdate" parameterType="list">
update user
<trim prefix="set" suffixOverrides=",">
<trim prefix="username=case" suffix="end,">
<foreach collection="list" item="item" index="index">
when id = #{item.id} then #{item.username}
</foreach>
</trim>
<trim prefix="password=case" suffix="end,">
<foreach collection="list" item="item" index="index">
when id = #{item.id} then #{item.password}
</foreach>
</trim>
</trim>
where id in
<foreach collection="list" item="item" index="index" separator="," open="(" close=")">
#{item.id}
</foreach>
</insert>

</mapper>
1 change: 1 addition & 0 deletions src/main/java/com/wangzaiplus/test/pojo/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Builder
public class User {

private Integer id;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/wangzaiplus/test/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ public interface UserService {

ServerResponse login(String username, String password);

void batchInsert(List<User> list);

void batchUpdate(List<User> list);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.wangzaiplus.test.service.batch;

import java.util.List;

public interface BatchProcessMapper<T> {

void batchInsert(List<T> list);

void batchUpdate(List<T> list);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.wangzaiplus.test.service.batch.mapperproxy;

import com.google.common.collect.Lists;
import com.wangzaiplus.test.service.batch.BatchProcessMapper;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;

import static com.wangzaiplus.test.common.Constant.MAX_SIZE_PER_TIME;

public class MapperProxy<T> implements BatchProcessMapper<T> {

private BatchProcessMapper batchProcessMapper;

public MapperProxy(BatchProcessMapper batchProcessMapper) {
this.batchProcessMapper = batchProcessMapper;
}

@Override
public void batchInsert(List<T> list) {
if (CollectionUtils.isEmpty(list)) {
return;
}

List<List<T>> partition = Lists.partition(list, MAX_SIZE_PER_TIME);
for (List<T> batchList : partition) {
batchProcessMapper.batchInsert(batchList);
}
}

@Override
public void batchUpdate(List<T> list) {
if (CollectionUtils.isEmpty(list)) {
return;
}

List<List<T>> partition = Lists.partition(list, MAX_SIZE_PER_TIME);
for (List<T> batchList : partition) {
batchProcessMapper.batchUpdate(batchList);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.wangzaiplus.test.pojo.MsgLog;
import com.wangzaiplus.test.pojo.User;
import com.wangzaiplus.test.service.UserService;
import com.wangzaiplus.test.service.batch.mapperproxy.MapperProxy;
import com.wangzaiplus.test.util.JedisUtil;
import com.wangzaiplus.test.util.JodaTimeUtil;
import com.wangzaiplus.test.util.RandomUtil;
Expand Down Expand Up @@ -108,4 +109,15 @@ private void saveAndSendMsg(User user) {
MsgLog msgLog = new MsgLog(msgId, loginLog, RabbitConfig.LOGIN_LOG_EXCHANGE_NAME, RabbitConfig.LOGIN_LOG_ROUTING_KEY_NAME);
msgLogMapper.insert(msgLog);
}

@Override
public void batchInsert(List<User> list) {
new MapperProxy<User>(userMapper).batchInsert(list);
}

@Override
public void batchUpdate(List<User> list) {
new MapperProxy<User>(userMapper).batchUpdate(list);
}

}

0 comments on commit 653a38e

Please sign in to comment.