CustomInterceptor拦截@UpdateProvider链接方法,但执行两次

发布于 2025-01-18 13:17:51 字数 2175 浏览 2 评论 0原文

我定义了一个拦截器来拦截更新方法。该方法主体仅在截距的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文