Mybatis使用if标签传入参数为String可能遇到的错误

说明

最近在使用<if>标签合并接口和SQL的过程遇到了一些坑,放在这记录一下

首先我们有下面这样一个mapper接口(根据status变量来选择不同的SQL):

List<xxxxxxDTO> findAll(String status);

问题一:There is no getter for property named 'xxxx' in 'class java.lang.String'

原因:Mybatis默认采用ONGL解析参数,所以会自动采用对象树的形式取 string.xxx 值,如果没在在方法中定义,则会抛异常报错。

解决方案1:

在Mapper接口使用@Param注解标记参数名

List<xxxxxxDTO> findAll(@Param(value = "status") String status);

然后在mapper.xml里就可以这样写

    <select id="findAll" resultMap="baseResultMap" parameterType="String">
        <if test='status=="0"'><!-- 注意是单引号里套双引号,待会说为什么 -->
            SELECT xxxxxxx FROM xxxxxxxx_table WHERE status='0'
        </if>
    </select>

解决方案2:

把mapper.xml使用的 变量名(如本例的status)修改为  _parameter 即可

    <select id="findAll" resultMap="baseResultMap" parameterType="String">
        <if test='_parameter=="0"'><!-- 注意是单引号里套双引号,待会说为什么 -->
            SELECT xxxxxxx FROM xxxxxxxx_table WHERE status='0'
        </if>
    </select>

问题2:nested exception is java.sql.SQLException: sql injection violation, syntax error: ERROR. pos 23, line 1, column 23, token RPAREN : select count(0) from () tmp_count或者是出现:NumberFormatException: For input string:"xx"

原因:如果你在xml写成了下面的形式:

    <select id="findAll" resultMap="baseResultMap" parameterType="String">
        <if test="_parameter=='0'"><!-- 或者是用了@Param写成 "status=='0'" -->
            SELECT xxxxxxx FROM xxxxxxxx_table WHERE status='0'
        </if>
    </select>

原因给的是字符串,却拿一个数字比较,mybatis会做一些转化,结果在数字格式化时发生异常。

解决方案1:

将数值转为字符串

    <select id="findAll" resultMap="baseResultMap" parameterType="String">
        <if test="status=='0'.toString()">
            SELECT xxxxxxx FROM xxxxxxxx_table WHERE status='0'
        </if>
    </select>

解决方案2:

加转义实体字符串引号&quot;

    <select id="findAll" resultMap="baseResultMap" parameterType="String">
        <if test="status==&quot;0&quot;">
            SELECT xxxxxxx FROM xxxxxxxx_table WHERE status='0'
        </if>
    </select>

解决方案3:(推荐)

外部单双引号交换

    <select id="findAll" resultMap="baseResultMap" parameterType="String">
        <if test='status=="0"'>
            SELECT xxxxxxx FROM xxxxxxxx_table WHERE status='0'
        </if>
    </select>

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以回答你关于MyBatis中if标签使用方式。 在MyBatis中,if标签通常用于动态生成SQL语句。它可以用来判断某个字段是否为空,从而根据不同情况生成不同的SQL语句。 以下是一些使用if标签的示例: 1. 判断一个参数是否为null或空字符串: ``` <select id="selectUser" parameterType="java.lang.String" resultType="User"> SELECT * FROM users <where> <if test="username != null and username != ''"> AND username = #{username} </if> </where> </select> ``` 这个例子中,如果传入的username参数不为null或空字符串,则会将其加入到SQL语句中,生成类似于“AND username = 'xxx'”这样的条件语句。 2. 判断一个对象的某个属性是否为null: ``` <select id="selectUser" parameterType="User" resultType="User"> SELECT * FROM users <where> <if test="email != null"> AND email = #{email} </if> </where> </select> ``` 这个例子中,如果传入的User对象的email属性不为null,则会将其加入到SQL语句中,生成类似于“AND email = 'xxx'”这样的条件语句。 3. 判断一个集合是否为空: ``` <select id="selectUser" parameterType="java.util.Map" resultType="User"> SELECT * FROM users <where> <if test="ids != null and ids.size() > 0"> AND id IN <foreach item="id" collection="ids" open="(" separator="," close=")"> #{id} </foreach> </if> </where> </select> ``` 这个例子中,如果传入的Map对象中包含一个名为ids的集合属性且不为空,则会将其加入到SQL语句中,生成类似于“AND id IN (1,2,3)”这样的条件语句。 以上是一些if标签使用示例,希望能对你有所帮助。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值