JDK1.8分组问题产生的SQL需求
需求一
传入的值数据结构为:List(Map<String, String>)
Dao层的代码:
List<RecordPo> selectConditionRecord(List<Map<String, String>> list);
xml层代码:
<select id="selectConditionRecord" resultMap="BaseResultMap" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" separator=";">
select
*
from
RECORD
<where>
<if test="item.code != null and item.code != ''">
code = #{item.code,jdbcType=VARCHAR}
</if>
<if test="item.account != null and item.account != ''">
and account = #{item.account,jdbcType=VARCHAR}
</if>
<if test="item.createdate != null">
and to_char(CREATEDATE, 'yyyy-MM-dd') = #{item.createdate,jdbcType=TIMESTAMP}
</if>
</where>
</foreach>
</select>
需求二
mybatis批量更新问题
传入的值数据结构为:List(Map<String, String>)
Dao层的代码:
void batchUpdate(List<RecordPo> list);
xml层代码:
<update id="batchUpdate" parameterType="java.util.List">
<foreach collection="list" index="index" item="item" separator=";">
update RECORD
<set>
<if test="item.id != null">
id = #{item.id,jdbcType=VARCHAR},
</if>
<if test="item.code != null">
code = #{item.code,jdbcType=VARCHAR},
</if>
<if test="item.createuser != null">
createuser = #{item.createuser,jdbcType=VARCHAR},
</if>
<if test="item.createdate != null">
createdate = #{item.createdate,jdbcType=TIMESTAMP},
</if>
</set>
<where>
id = #{item.id,jdbcType=VARCHAR}
</where>
</foreach>
</update>
- 出现的问题:
Cause: java.sql.SQLSyntaxErrorException: 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 'UPDATE hd_t_user\n SET username ='111' \n where\n ' at line 6\r\n### The error may exist in file;
- 解决方案:
allowMultiQueries=true