【mybatis】IF判断的坑 (实现if test= status == 'zhangsan')
转自:http://blog.csdn.net/z69183787/article/details/51589171
最近在项目使用mybatis中碰到个问题
- <if test="type=='y'">
- and status = 0
- </if>
当传入的type的值为y的时候,if判断内的sql也不会执行,抱着这个疑问就去看了mybatis是怎么解析sql的。下面我们一起来看一下mybatis 的执行过程。
DefaultSqlSession.class 121行
- public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) {
- try {
- MappedStatement ms = configuration.getMappedStatement(statement);
- executor.query(ms, wrapCollection(parameter), rowBounds, handler);
- } catch (Exception e) {
- throw ExceptionFactory.wrapException("Error querying database. Cause: " + e, e);
- } finally {
- ErrorContext.instance().reset();
- }
- }
在 executor.query(ms, wrapCollection(parameter), rowBounds, handler);
执行到BaseExecutor.class执行器中的query方法
- public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
- BoundSql boundSql = ms.getBoundSql(parameter);
- CacheKey key = createCacheKey(ms, parameter, rowBounds, boundSql);
- return query(ms, parameter, rowBounds, resultHandler, key, boundSql);
- }
在query的方法中看到boundSql,是通过 ms.getBoundSql(parameter);获取的。
再点进去可以看到MappedStatement.class类中的getBoundSql方法
- public BoundSql getBoundSql(Object parameterObject) {
- BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
- List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
- if (parameterMappings == null || parameterMappings.size() <= 0) {
- boundSql = new BoundSql(configuration, boundSql.getSql(), parameterMap.getParameterMappings(), parameterObject);
- }
- // check for nested result maps in parameter mappings (issue #30)
- for (ParameterMapping pm : boundSql.getParameterMappings()) {
- String rmId = pm.getResultMapId();
- if (rmId != null) {
- ResultMap rm = configuration.getResultMap(rmId);
- if (rm != null) {
- hasNestedResultMaps |= rm.hasNestedResultMaps();
- }
- }
- }
- return boundSql;
- }
看到其中有sqlSource.getBoundSql(parameterObject); sqlsource是一个接口。
- /**
- *
- * This bean represets the content of a mapped statement read from an XML file
- * or an annotation. It creates the SQL that will be passed to the database out
- * of the input parameter received from the user.
- *
- */
- public interface SqlSource {
- BoundSql getBoundSql(Object parameterObject);
- }
类中getBoundSql是一个核心方法,mybatis 也是通过这个方法来为我们构建sql。BoundSql 对象其中保存了经过参数解析,以及判断解析完成sql语句。比如<if> <choose> <when> 都回在这一层完成,具体的完成方法往下看,那最常用sqlSource的实现类是DynamicSqlSource.class
- public class DynamicSqlSource implements SqlSource {
- private Configuration configuration;
- private SqlNode rootSqlNode;
- public DynamicSqlSource(Configuration configuration, SqlNode rootSqlNode) {
- this.configuration = configuration;
- this.rootSqlNode = rootSqlNode;
- }
- public BoundSql getBoundSql(Object parameterObject) {
- DynamicContext context = new DynamicContext(configuration, parameterObject);
- rootSqlNode.apply(context);
- SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration);
- Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
- SqlSource sqlSource = sqlSourceParser.parse(context.getSql(), parameterType, context.getBindings());
- BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
- for (Map.Entry<String, Object> entry : context.getBindings().entrySet()) {
- boundSql.setAdditionalParameter(entry.getKey(), entry.getValue());
- }
- return boundSql;
- }
- }
核心方法是调用了rootSqlNode.apply(context); rootSqlNode是一个接口
- public interface SqlNode {
- boolean apply(DynamicContext context);
- }
可以看到类中 rootSqlNode.apply(context); 的方法执行就是一个递归的调用,通过不同的
实现类执行不同的标签,每一次appll是完成了我们<></>一次标签中的sql创建,计算出标签中的那一段sql,mybatis通过不停的递归调用,来为我们完成了整个sql的拼接。那我们主要来看IF的实现类IfSqlNode.class
- public class IfSqlNode implements SqlNode {
- private ExpressionEvaluator evaluator;
- private String test;
- private SqlNode contents;
- public IfSqlNode(SqlNode contents, String test) {
- this.test = test;
- this.contents = contents;
- this.evaluator = new ExpressionEvaluator();
- }
- public boolean apply(DynamicContext context) {
- if (evaluator.evaluateBoolean(test, context.getBindings())) {
- contents.apply(context);
- return true;
- }
- return false;
- }
- }
可以看到IF的实现中,执行了 if (evaluator.evaluateBoolean(test, context.getBindings())) 如果返回是false的话直接返回,否则继续递归解析IF标签以下的标签,并且返回true。那继续来看 evaluator.evaluateBoolean 的方法
- public class ExpressionEvaluator {
- public boolean evaluateBoolean(String expression, Object parameterObject) {
- Object value = OgnlCache.getValue(expression, parameterObject);
- if (value instanceof Boolean) return (Boolean) value;
- if (value instanceof Number) return !new BigDecimal(String.valueOf(value)).equals(BigDecimal.ZERO);
- return value != null;
- }
关键点就在于这里,在OgnlCache.getValue中调用了Ognl.getValue,看到这里恍然大悟,mybatis是使用的OGNL表达式来进行解析的,在OGNL的表达式中,'y'会被解析成字符,因为java是强类型的,char 和 一个string 会导致不等。所以if标签中的sql不会被解析。具体的请参照 OGNL 表达式的语法。到这里,上面的问题终于解决了,只需要把代码修改成:
- <if test='type=="y"'>
- and status = 0
- </if>
就可以执行了,这样"y"解析出来是一个字符串,两者相等!
【mybatis】IF判断的坑 (实现if test= status == 'zhangsan')相关推荐
- mybatis如何判断一个字段不为空,或空字符串
mybatis如何判断一个字段不为空,或空字符串 <select id="countStudentNumber" resultType="java.lang.Int ...
- else if mybatis 嵌套_mybatis踩坑之——foreach循环嵌套if判断
作为小猿一枚,记录一下工作中踩过的坑. 今天在修改别人的代码bug时,有一个需求是在做导出excel功能时,mybatis动态构建sql语句的时候,要根据传进来的map中的一个值来判断是不是null, ...
- 【if test=takeWay == '0'】mybatis的if判断有坑
昨天码代码,掉到坑里,耽误了几个小时才从坑里爬出来-_-||| 单个的字符要写到双引号里面才行,改为<if test='takeWay == "1"'>或者改为< ...
- 程序员,Mybatis 你踩过坑吗?
大多数开发者应该都使用过Hibernate或者Mybatis的框架,或多或少都踩过一些坑! 如在MyBatis/Ibatis中#和$的区别,#方式能够很大程度防止sql注入,$方式无法防止Sql注入. ...
- mybatis 查询的时间不对_程序员,Mybatis 你踩过坑吗?
点击上方"Java基基",选择"设为星标" 做积极的人,而不是积极废人! 源码精品专栏 中文详细注释的开源项目 RPC 框架 Dubbo 源码解析 网络应用框架 ...
- mybatis学习与踩坑记录
mybatis resultmap高级映射 应用场景:如果sql查询的列名和pojo的属性名不一致,可以使用resultMap将列名和pojo的属性名作一个对应关系,就可以映射成功了.(如果返回值为i ...
- Mybatis if 判断等于一个字符串
在做开发的时候遇到这样一个问题:当传入的type的值为y的时候,if判断内的sql也不会执行. <if test="type=='y'"> and status = 0 ...
- java 一级缓存,MyBatis一级缓存避坑完全指南
一级缓存概念 当我们使用Mybatis进行数据库的操作时候,会创建一个SqlSession来进行一次数据库的会话,会话结束则关闭SqlSession对象.那么一个SqlSession的生命周期即对应于 ...
- mybatis常用判断语法(标签)
作为java开发,我们常用的判断有if.switch语句,其实在MyBatis中也有对应的标签,用于动态生成sql语句. 1. if判断 <where><if test=" ...
最新文章
- 指纹图谱相似度评价软件_远志与炆远志指纹图谱比较
- java过滤乱码 \u形式乱码 unicode乱码
- 理解Java枚举类型
- 【若依(ruoyi)】jQuery.validator
- MySQL(1)数据库介绍,配置MySQL的tab补全
- @Html.Action()
- AcWing 211. 计算系数
- [Leetcode][程序员面试金典][面试题16.11][JAVA][跳水板][数学][动态规划]
- HDU Problem 4857 逃生【拓扑排序+优先队列】
- 全站仪和手机连接软件_全站仪各方面应用的原理、操作及计算,看这篇就对了...
- 函数重载函数的引用算重载吗_了解C ++中的函数重载
- 修改某张表的结构_在T-SQL语言中,若要修改某张表的结构,应该使用的修改关键字是Alter。...
- mysql5.7导出数据提示–secure-file-priv选项问题的解决方法
- Cython 的简要入门、编译及使用
- Centernet 生成高斯热图
- sql trim函数_SQL TRIM函数
- python_常微分方程的求解
- RGB数字信号VESA标准时序verilog设计
- vivado Non_project
- 国际户外运动品牌简介