最近用tkmybatis插件做项目时,发现在事务中有些新插入的数据需要被用到主键,主键不能回写,特意查了下原先作者的文档和博客,发现没有uuid的回写实现,只能自己写个用下,现发出来分享下,愿以后的小伙伴都能有分享精神。
主键采用UUID时主键回写的两种解决方案,都是才用mybatis插件形式实现。
@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