当前位置: 移动技术网 > IT编程>开发语言>Java > Mybatis使用问题汇总-采坑和实践

Mybatis使用问题汇总-采坑和实践

2019年09月04日  | 移动技术网IT编程  | 我要评论

xlecho编辑整理,欢迎转载,转载请声明文章来源。欢迎添加echo微信(微信号:t2421499075)交流学习。 百战不败,依不自称常胜,百败不颓,依能奋力前行。——这才是真正的堪称强大!!


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

在这里插入图片描述


做一个有底线的博客主

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网