Springboot+Mybatis实现merge into数据的批量插入更新(有则插入无则更新)

User实体类:

@Data
public class User {

    private String id;

    private String userName;

    private String sex;

    private String age;

}

UserMapper.java

@Mapper
public interface UserMapper {

    boolean insertUpdate(List<User> user);

}

UserMapper.xml

<update id="insertUpdate" parameterType="com.haohao.model.User">
	merge into user a using(
	<foreach collection="list" index="index" item="item"
			 separator="union all">
		select
		#{item.id} as id,
		#{item.userName} as userName,
		#{item.sex} as sex,
		#{item.age} as age
		from dual
	</foreach>
	) b
	on (a.id=b.id)
	when matched then
	update set
		a.userName=b.userName,
		a.sex=b.sex,
		a.age=b.age
		where a.id=b.id
	when not matched then
	insert
		(a.id,
		a.userName,
		a.sex,
		a.age)
</update>

具体实现:

List<User> userList = new ArrayList<>();
List<User> list = new ArrayList<>();
	for (User item : userList) {
		User user = new User();
		user.setId("123456789");
		user.setUserName("haohao");
		user.setSex("1");
		user.setAge("20");
		list.add(user);
	}
boolean flag = UserMapper.insertUpdate(list);

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
首先,需要在Mybatis中配置分表策略和数据源路由。这里使用ShardingSphere进行分表和数据源路由的配置。 1. 添加ShardingSphere的相关依赖 ```xml <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-core</artifactId> <version>${shardingsphere.version}</version> </dependency> ``` 2. 配置ShardingSphere的数据源和分表策略 ```yaml spring: shardingsphere: datasource: names: ds0, ds1 ds0: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/user0?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf8 username: root password: root ds1: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/user1?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf8 username: root password: root sharding: tables: user: actualDataNodes: ds${0..1}.user_${0..1} tableStrategy: standard: shardingColumn: id shardingAlgorithmName: userTableShardingAlgorithm keyGenerateStrategy: column: id keyGeneratorName: snowflake defaultDatabaseStrategy: inline: shardingColumn: id algorithmExpression: ds${id % 2} shardingAlgorithms: userTableShardingAlgorithm: type: INLINE props: algorithm-expression: user_${id % 2} keyGenerators: snowflake: type: SNOWFLAKE props: worker-id: 123 ``` 3. 创建User实体类和Mapper接口 ```java @Data public class User { private Long id; private String name; private Integer age; } @Mapper public interface UserMapper { @Insert("INSERT INTO user_${id % 2}(name, age) VALUES(#{name}, #{age})") int insert(User user); } ``` 4. 创建Service层,调用Mapper插入数据 ```java @Service public class UserService { @Autowired private UserMapper userMapper; public void save(User user) { userMapper.insert(user); } } ``` 以上就是使用SpringBootMybatis实现user表分表插入用户数据的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦想天涯~路在脚下

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值