mybatis 增删改查遇到的问题和解决方案 Error getting generated key 和 参数变成 ?

报错一

运行环境 将下列散装参数添加到 一个id自增的表格中,会出现以下错误

<!--添加数据 -->
    <insert id="add" useGeneratedKeys="true" keyProperty="id">
        insert into store(shop_owner, id_number, name, industry, area, phone, status, audit_time)
        values (#{shopOwner},
                #{idNumber},
                #{name},
                #{industry},
                #{area},
                #{phone},
                #{status},
                #{auditTime});</insert>

Error updating database.  Cause: org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: org.apache.ibatis.executor.ExecutorException: Could not determine which parameter to assign generated keys to. Note that when there are multiple parameters, 'keyProperty' must include the parameter name (e.g. 'param.id'). Specified key properties are [id] and available parameters are [area, param7, param8, param5, param6, shopOwner, industry, idNumber, param3, param4, param1, param2, phone, auditTime, name, status]

解决:

此处并非网上搜到的mybatis版本问题,关闭useGeneratedKeys虽然可以添加,但不符合需求。

此处直接在第一列数据添加id会报错。应该封装成类后用类添加,并且在第一列填写null。

  <insert id="addStore" useGeneratedKeys="true" keyProperty="id">
        insert into store
        values (null,
                #{shopOwner},
                #{idNumber},
                #{name},
                #{industry},
                #{area},
                #{phone},
                #{status},
                #{auditTime})
    </insert>

报错2

环境 使用map参数 条件查询

Map<String, Object> condition = new HashMap<>();
        condition.put("phone", "%18933283299%");
        condition.put("startTime", sdf.parse(startTime));
        condition.put("endTime", sdf.parse(endTime));
        condition.put("status", 1);
        condition.put("idNumber", "%441322199309273014%");

        List<Store> condition1 = mapper.findCondition(condition);

<select id="findCondition" resultMap="storeresultMap">
        select * from store
        <where>
            <if test="auditTime != null">
                and audit_time <![CDATA[
                >
                ]]> #{startTime} and audit_time <![CDATA[
                <
                ]]> #{endTime}
            </if>
            <if test="phone != null and phone !='' ">
                and phone like "%18933283299%"
            </if>

            <if test="status != null and status !='' ">
                and status = #{status}
            </if>

            <if test="idNumber != null and idNumber !='' ">
                and id_number like "%441322199309273014%"
            </if>

        </where>
    </select>

查不到数据  发现其中 status 被赋值为? 

Preparing: select * from store WHERE phone like "%18933283299%" and status = ? and id_number like "%441322199309273014%" 

原因是#{}为了防止注入会将数字1进行转义,所以此处应该使用${}来接收参数。

错误3

### Error updating database.  Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String

此为将日期格式的参数与空字符串 '' 进行比较判断 ,错误的语法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值