mybatis if标签判断的问题
<?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类里的属性么?求解答啊,在线等 mybatis sql java springMVC?mybatis
[解决办法]
map类型的参数,使用#keyName#来引用,keyName为键名????
[解决办法]
MyBatis,数据库映射这一块。 <if test="end != null and end != ''">and #{end} > date </if>参数是你方法里面传过来参数的实体解析,或者键值对的解析。parameterType是参数类型。可以是map,也可以是你的实体类(完整的包名)
[解决办法]
你传的值是map键值对。。就看map键值对的KEY属性,,map和实体对象都可以的。实体里面没有那些属性的,就用map
[解决办法]
如果你操作的数据字段有对应的实体你就用实体对象做参数,如果没有就用map,MyBatis在数据库映射这一部分,的所有参数都市你传过来的对象属性(实体---实体属性)(map---map的KEY)