报错一
运行环境 将下列散装参数添加到 一个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
此为将日期格式的参数与空字符串 '' 进行比较判断 ,错误的语法