CustomInterceptor拦截@UpdateProvider链接方法,但执行两次
我定义了一个拦截器来拦截更新方法。该方法主体仅在截距的SQL上打印。
SQL是通过 @ UpdateProvider中的buildupdatesql()方法构造的,并使用“org。Apache。Ibatis。JDBC。SQL”内部创建。
但是buildoupdatesql()方法执行了两次。
效果如下图所示:
mybatis版本
3.5.7
数据库供应商和版本
mysql 5.7.34-log
预期结果
buildupdatesql()方法仅执行一次。
实际结果
buildupdatesql()方法执行两次。
内核代码
@Repository
public interface UserMapper {
List<User> Sel();
@UpdateProvider(type = UserMapperProvider.class, method = "buildUpdateSql")
public int updateSubSite(int id);
public static class UserMapperProvider{
public String buildUpdateSql(int id) {
String sql = "update user set name='gfgfgfg' where id=#{id}";
System.out.println("构造的sql时:"+sql);
return sql;
}
}
}
@Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }) })
public class CustomInterceptor implements Interceptor {
private Properties properties;
@Override
public Object intercept(Invocation invocation) throws Throwable {
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
String sql = mappedStatement.getBoundSql(invocation.getArgs()[1]).getSql();
System.out.println("获取到SQL语句:" + sql);
}
}
@Configuration
public class MybatisConfig {
@Autowired
private List<SqlSessionFactory> sqlSessionFactoryList;
@PostConstruct
public void addMySqlInterceptor() {
CustomInterceptor interceptor = new CustomInterceptor();
for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
}
}
}
I defined an interceptor to intercept the update method.The method body only prints the intercepted SQL.
The SQL is constructed through the buildupdatesql () method in @ updateprovider and internally created using "org. Apache. Ibatis. JDBC. SQL".
But the buildoupdatesql () method was executed twice.
The effect is shown in the figure below:
MyBatis version
3.5.7
Database vendor and version
MySQL 5.7.34-log
Expected result
buildUpdateSql() method only execute once.
Actual result
buildUpdateSql() method execute twice.
Kernel Code
@Repository
public interface UserMapper {
List<User> Sel();
@UpdateProvider(type = UserMapperProvider.class, method = "buildUpdateSql")
public int updateSubSite(int id);
public static class UserMapperProvider{
public String buildUpdateSql(int id) {
String sql = "update user set name='gfgfgfg' where id=#{id}";
System.out.println("构造的sql时:"+sql);
return sql;
}
}
}
@Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }) })
public class CustomInterceptor implements Interceptor {
private Properties properties;
@Override
public Object intercept(Invocation invocation) throws Throwable {
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
String sql = mappedStatement.getBoundSql(invocation.getArgs()[1]).getSql();
System.out.println("获取到SQL语句:" + sql);
}
}
@Configuration
public class MybatisConfig {
@Autowired
private List<SqlSessionFactory> sqlSessionFactoryList;
@PostConstruct
public void addMySqlInterceptor() {
CustomInterceptor interceptor = new CustomInterceptor();
for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论