mybatis if标签判断的问题

无敌小贱 2013-12-04 09:57:22
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lypaydb.mapper.Order_DayMapper">
<resultMap type="com.lypaydb.pojo.Order_Day" id="odMap">
<id property="id" column="id" />
<result property="date" column="date" />
<result property="total" column="total" />
<result property="aisle" column="aisle" />
<result property="operators" column="operators" />
<result property="channelid" column="channelid" />
<result property="appid" column="appid" />
<result property="paycnt" column="paycnt" />
</resultMap>

<!-- /*sql -->
<select id="findod" parameterType="java.util.Map" resultMap="odMap">
select * from order_day where 1 = 1
<if test="${start} != null and ${start != ''}">
and date >= #{start}
</if>
<if test="${end} != null and ${end} != ''">
and #{end} > date
</if>
<if test="appid != null and appid != ''">
and appid=${appid}
</if>
<if test="operators != null and operators != ''">
and operators=${operators}
</if>
limit ${Page.startPos},${Page.pageSize};
</select>

<select id="getAllCount" parameterType="java.util.Map" resultType="java.lang.Integer">
select count(*) from order_day where 1=1
<if test="${start} != null and ${start != ''}">
and date >= #{start}
</if>
<if test="${end} != null and ${end} != ''">
and #{end} > date
</if>
<if test="appid != null and appid != ''">
and appid=${appid}
</if>
<if test="operators != null and operators != ''">
and operators=${operators}
</if>
</select>
<!-- sql*/ -->
</mapper>


主要看我的findod方法里边的前两个if判断,由于我这个POJO类里边只有一个date,日期类型。我要传过来两个时间点供查询,我是放到一个map里边,key-value分别是("start",时间)和("end",时间)。
为什么报这个错
nested exception is org.apache.ibatis.builder.BuilderException: Error evaluating expression '${start} != null and ${start != ''}'. Cause: org.apache.ibatis.ognl.ExpressionSyntaxException: Malformed OGNL expression: ${start} != null and ${start != ''} [org.apache.ibatis.ognl.ParseException: Encountered "$" at line 1, column 1. Was expecting one of: ":" ... "not" ... "+" ... "-" ... "~" ... "!" ... "(" ... "true" ... "false" ... "null" ... "#this" ... "#root" ... "#" ... "[" ... "{" ... "@" ... "new" ... ... ... "\'" ... "`" ... "\"" ... ... ... ]

而我把start和end的条件判断去掉,下边两个条件都能正常使用,怎么解决啊?难道这个if判断的条件只能是POJO类里的属性么?求解答啊,在线等
...全文
140676 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
ggwhx 2015-06-24
  • 打赏
  • 举报
回复
同样的问题找了好久终于在这里找到答案了。。。原来if()条件判断里面直接传自带就行,不需要加 #{}括起来了。mark下
无敌小贱 2013-12-17
  • 打赏
  • 举报
回复
引用 14 楼 u011055344 的回复:
[quote=引用 11 楼 bqmcjl 的回复:] [quote=引用 10 楼 u011055344 的回复:] MyBatis,数据库映射这一块。 <if test="end != null and end != ''">and #{end} > date </if>参数是你方法里面传过来参数的实体解析,或者键值对的解析。parameterType是参数类型。可以是map,也可以是你的实体类(完整的包名)
但是我实体类里边没有end和start这个属性,传过来实体类(完整的包名)会报错找不到属性的吧?这个end只是map定义的key值。这样应该可以吧?[/quote]如果你操作的数据字段有对应的实体你就用实体对象做参数,如果没有就用map,MyBatis在数据库映射这一部分,的所有参数都市你传过来的对象属性(实体---实体属性)(map---map的KEY)[/quote] 哦~~了解了。3Q
u011055344 2013-12-10
  • 打赏
  • 举报
回复
引用 11 楼 bqmcjl 的回复:
[quote=引用 10 楼 u011055344 的回复:] MyBatis,数据库映射这一块。 <if test="end != null and end != ''">and #{end} > date </if>参数是你方法里面传过来参数的实体解析,或者键值对的解析。parameterType是参数类型。可以是map,也可以是你的实体类(完整的包名)
但是我实体类里边没有end和start这个属性,传过来实体类(完整的包名)会报错找不到属性的吧?这个end只是map定义的key值。这样应该可以吧?[/quote]如果你操作的数据字段有对应的实体你就用实体对象做参数,如果没有就用map,MyBatis在数据库映射这一部分,的所有参数都市你传过来的对象属性(实体---实体属性)(map---map的KEY)
u011055344 2013-12-10
  • 打赏
  • 举报
回复
你传的值是map键值对。。就看map键值对的KEY属性,,map和实体对象都可以的。实体里面没有那些属性的,就用map
u011055344 2013-12-10
  • 打赏
  • 举报
回复
MyBatis,数据库映射这一块。 <if test="end != null and end != ''">and #{end} > date </if>参数是你方法里面传过来参数的实体解析,或者键值对的解析。parameterType是参数类型。可以是map,也可以是你的实体类(完整的包名)
无敌小贱 2013-12-10
  • 打赏
  • 举报
回复
引用 9 楼 u013098227 的回复:
看了半天没弄明白怎么回事,真是隔行如隔山啊
无敌小贱 2013-12-10
  • 打赏
  • 举报
回复
引用 10 楼 u011055344 的回复:
MyBatis,数据库映射这一块。 <if test="end != null and end != ''">and #{end} > date </if>参数是你方法里面传过来参数的实体解析,或者键值对的解析。parameterType是参数类型。可以是map,也可以是你的实体类(完整的包名)
但是我实体类里边没有end和start这个属性,传过来实体类(完整的包名)会报错找不到属性的吧?这个end只是map定义的key值。这样应该可以吧?
金刚呼噜娃娃 2013-12-09
  • 打赏
  • 举报
回复
看了半天没弄明白怎么回事,真是隔行如隔山啊
无敌小贱 2013-12-04
  • 打赏
  • 举报
回复
好吧我自己解决了
无敌小贱 2013-12-04
  • 打赏
  • 举报
回复
没人来么......
无敌小贱 2013-12-04
  • 打赏
  • 举报
回复
自己顶!!!!求大神啊
无敌小贱 2013-12-04
  • 打赏
  • 举报
回复
不知道我理解的对不对,希望有经验的人来指点一下该注意的地方
无敌小贱 2013-12-04
  • 打赏
  • 举报
回复
引用 4 楼 jsynzzp 的回复:
最后是什么问题?
这是修改之后的
<select id="findod" parameterType="map" resultMap="odMap">
              select * from order_day where 1 = 1 
              <if test="start != null and start != ''">
                and   date >= #{start} 
              </if>
              <if test="end != null and end != ''">
                and #{end} > date  
              </if>
              <if test="appid != null and appid != ''">
                and  appid=${appid}
              </if>
              <if test="operators != null and operators != ''">
                and operators=${operators}  
              </if>
              limit ${Page.startPos},${Page.pageSize};
          </select>
就把parameterType="java.lang.Map" 改成="map"了,不过好像这个没关系。。。然后if判断的条件直接就写map里边的key值,不管它是不是POJO类里边的,反正写KEY值就是了。不知道我理解的对不对,反正是行得通了。
无敌小贱 2013-12-04
  • 打赏
  • 举报
回复
引用 5 楼 jackwumengfeng 的回复:
[quote=引用 3 楼 bqmcjl 的回复:] 好吧我自己解决了
map类型的参数,使用#keyName#来引用,keyName为键名???? [/quote] 啊。我说的是if判断的条件,不是sql的部分。我知道sql使用 #{key}或者${key}来获取。 我说的是 <if test="** != null">这里的**怎么获取map中的值
南猿北蛰 2013-12-04
  • 打赏
  • 举报
回复
引用 3 楼 bqmcjl 的回复:
好吧我自己解决了
map类型的参数,使用#keyName#来引用,keyName为键名????
-妖孽 2013-12-04
  • 打赏
  • 举报
回复
最后是什么问题?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧