tkmybatis 中 主键采用UUID时主键回写的两种解决方案

在使用TkMybatis进行项目开发时,遇到在事务中需要获取新插入数据的UUID主键问题。由于官方未提供直接支持,本文档将分享两种通过Mybatis插件实现UUID主键回写的方案,旨在促进开发者之间的知识分享。
摘要由CSDN通过智能技术生成

最近用tkmybatis插件做项目时,发现在事务中有些新插入的数据需要被用到主键,主键不能回写,特意查了下原先作者的文档和博客,发现没有uuid的回写实现,只能自己写个用下,现发出来分享下,愿以后的小伙伴都能有分享精神。

主键采用UUID时主键回写的两种解决方案,都是才用mybatis插件形式实现。

1)线程局部变量实现方式

@Intercepts({
	@Signature(type=StatementHandler.class, method="update", args={ Statement.class }),
	@Signature(type=Executor.class, method="update", args={MappedStatement.class, Object.class})
})
public class BackWriteInterceptor implements Interceptor {

	private static final String DEFAULT_PRIMARY_KEY_NAME = "uid";

	private static final ThreadLocal<Boolean> LOCAL_BACK_WRITE = new ThreadLocal<Boolean>();

	private String primaryKeyPropertyName = DEFAULT_PRIMARY_KEY_NAME;

	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		Object target = invocation.getTarget();
		if (target
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值