@Intercepts({
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})
})
public class PrepareInterceptor implements Interceptor {
@Autowired
private OAuthUtils oAuthUtils;
@Override
public Object intercept(Invocation invocation) throws Throwable {
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
Object object = invocation.getArgs()[1];
// sql类型
SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
UserInfo userInfo = oAuthUtils.getUserInfo();
if (SqlCommandType.INSERT.equals(sqlCommandType)) {
// 插入操作
if (object instanceof DefaultSqlSession.StrictMap) {
// FIXME 批量操作(还有没有别的方式判断?)
Map map = (DefaultSqlSession.StrictMap) object;
List list = (List) map.get("list");
for (Object o : list) {
reflectCreateInfo(o);
}
} else {
// 单行
reflectCreateInfo(object);
}
} else if (SqlCommandType.UPDATE.equals(sqlCommandType)) {
mybatis自定义拦截器设置createBy&createTime
最新推荐文章于 2024-07-17 18:08:33 发布
本文介绍了如何在MyBatis中自定义拦截器,用于在插入数据时自动填充createBy和createTime字段,提高了代码的复用性和规范性。通过拦截Executor的update方法,解析SQL动态插入相关信息,确保每个新建记录都有准确的创建者和时间戳。

最低0.47元/天 解锁文章
1351

被折叠的 条评论
为什么被折叠?



