Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL 替换功能存在重大缺陷,不能解决 MyBatis 动态 SQL #15

Open
lhsaq2009 opened this issue Dec 20, 2024 · 0 comments
Open

Comments

@lhsaq2009
Copy link

  • 目前代码比较乱,最起码的代码格式化都是乱七八糟
  • ducc 是个什么鬼玩意,知道它是个配置中心,但是全文找不到安装教程
  • SQL 替换功能存在重大缺陷,不能解决 MyBatis 动态 SQL

MyBatis 源码中,根据形参去判断 动态组装的 SQL,被脚本统一成固定 SQL

// 以下代码仅做示例展示
SqlSession sqlSession = MyBatisUtil.getSqlSession();
mapper = sqlSession.getMapper(TaskMapper.class);

System.out.println("1-----" + mapper.queryAll(Task.builder().title("任务1").build()));
System.out.println("2-----" + mapper.queryAll(Task.builder().createUser("zhangsan1").build()));

SqlReplaceConfig.setReplaceSqlBySqlId("com.jd.sql.analysis_samples.mapper.TaskMapper.queryAll"
        , "select * from task where create_user = ?");

System.out.println("3-----" + mapper.queryAll(Task.builder().title("任务2").build()));
System.out.println("4-----" + mapper.queryAll(Task.builder().createUser("zhangsan2").build()));

实际运行结果

Before Replace -> select id, title, content, create_user,create_time, update_time from task WHERE title like ?
com.jd.sql.analysis_samples.mapper.TaskMapper.queryAll -> select id, title, content, create_user,create_time, update_time from task WHERE title like '任务1'
1-----[Task(id=1, title=任务1, content=任务内容1, createUser=zhangsan1, createTime=Thu Oct 12 04:10:43 CST 2023, updateTime=Thu Oct 12 04:10:43 CST 2023)]

Before Replace -> select id, title, content, create_user,create_time, update_time from task WHERE create_user = ?
com.jd.sql.analysis_samples.mapper.TaskMapper.queryAll -> select id, title, content, create_user,create_time, update_time from task WHERE create_user = 'zhangsan1'
2-----[Task(id=1, title=任务1, content=任务内容1, createUser=zhangsan1, createTime=Thu Oct 12 04:10:43 CST 2023, updateTime=Thu Oct 12 04:10:43 CST 2023)]

Before Replace -> select id, title, content, create_user,create_time, update_time from task WHERE title like ?
 INFO [main] - sql analysis - sql replace old:select
         
        id, title, content, create_user,create_time, update_time
     
        from task
         WHERE  title like ?
 INFO [main] - sql analysis - sql replace new:select * from task where create_user = ?
com.jd.sql.analysis_samples.mapper.TaskMapper.queryAll -> select * from task where create_user = '任务2'
3-----[]                   <-----------   ❌ ❌ ❌  这个就异常了


Before Replace -> select id, title, content, create_user,create_time, update_time from task WHERE create_user = ?
 INFO [main] - sql analysis - sql replace old:select
         
        id, title, content, create_user,create_time, update_time
     
        from task
         WHERE  create_user = ?
 INFO [main] - sql analysis - sql replace new:select * from task where create_user = ?
com.jd.sql.analysis_samples.mapper.TaskMapper.queryAll -> select * from task where create_user = 'zhangsan2'
4-----[Task(id=2, title=任务2, content=任务内容2, createUser=zhangsan2, createTime=Fri Oct 13 05:10:43 CST 2023, updateTime=Fri Oct 13 05:10:43 CST 2023)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant