mybatis往配置文件中传入拼接好的条件语句

在程序中生成了字符串 “( age >'20' and '200'>age and sex='1' ) ”,现在要传入到配置文件中,大于号换成>单引号换成'都试过了,配置文件里面是这么写的
<select id="queryByCondition" parameterType="java.util.HashMap"
resultMap="BaseResultModelMap">
select * from user where 1=1
<if test="Condition!=null">
#{Condition,jdbcType=VARCHAR}
</if>
<if test="start!=null">
limit #{start,jdbcType=INTEGER} ,#{pageSize,jdbcType=INTEGER}
</if>
</select>
Condition就是我传入的字符串,现在总是报错,错误信息是
DEBUG - ==> Executing: select * from user where 1=1 ?
DEBUG - ==> Parameters: and ( age >'20' and '200'>age and sex='1' ) (String)
org.apache.ibatis.exceptions.IbatisException:
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''and ( age >\'20\' and \'200\'>age and sex=\'1\' ) '' at line 3
### The error may involve com.goalwisdom.gwnis.repository.dao.UserDao.queryByCondition-Inline
### The error occurred while setting parameters
提问中的大于号& g t;直接被网页转义了,分号也是,
最好是谁有写过这样的代码把字符串的格式和XML文件中接收方式粘贴上来

用${} 就不会当成字符串解析 会直接加上去 不过这个要慎用 防止被sql注入
mybatis xml 解析传入参数有2种 #{} 与${} 前面那种会把你传入的参数当成字符串解析 也就是你无论传入什么 都会被加上''号 后面那一种 你传入什么 就是什么 。

提交个回答还被要求详细描述! 相信懂的看到第一句都懂了吧··
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-08-25

用转义符:demo如下

<![CDATA[ 
when min(starttime)<='12:00' and max(endtime)<='12:00' 
]]>

追问

不行,这个我试过了的,接收字符串的方式应该存在问题,网上能查的基本都查过了,需要有写过的人的完整代码

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网