您现在的位置是:首页 > 文章详情

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

日期:2019-09-03点击:296

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


JDK1.8分组问题产生的SQL需求

需求一

传入的值数据结构为:List(Map)

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)

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

在这里插入图片描述

原文链接:https://yq.aliyun.com/articles/717282
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章