记录一个关于mybatis xml批量更新的问题: 2020-04-05

数据库url:jdbc:mysql://localhost:3306/test_lib_dev?serverTimezone=Asia/Shanghai

mybatis xml批量更新配置:

String[] adminAccounts = new String[] {"20204058631b264"};
Integer delTAdminInfo = mapper.delTAdminInfo(adminAccounts);
 Integer delTAdminInfo(String[] list);
Integer delTAdminInfo = mapper.delTAdminInfo("20204058631b264");
 Integer delTAdminInfo(String... list);

<update id="updTAdminInfos" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" open="" close="" separator=";">
            update t_admin_info 
              <set>
                  <if test="item.adminName != null and item.adminName != ''">
                       admin_name = #{item.adminName,jdbcType=VARCHAR},
                  </if>
                  <if test="item.pwd != null and item.pwd != ''">
                        pwd = #{item.pwd,jdbcType=VARCHAR},
                  </if>
                  <if test="item.gender != null and item.gender != ''">
                       gender = #{item.gender,jdbcType=VARCHAR},
                  </if>
                  <if test="item.age != null">
                       age = #{item.age,jdbcType=INTEGER},
                  </if>
                  <if test="item.idNum != null and item.idNum != ''">
                       id_num = #{item.idNum,jdbcType=VARCHAR},
                  </if>
                  <if test="item.telNum != null and item.telNum != ''">
                       tel_num = #{item.telNum,jdbcType=VARCHAR},
                  </if>
                  <if test="item.mobilePhone != null and item.mobilePhone != ''">
                       mobile_phone = #{item.mobilePhone,jdbcType=VARCHAR},
                  </if>
                  <if test="item.email != null and item.email != ''">
                       email = #{item.email,jdbcType=VARCHAR},
                  </if>
                  <if test="item.image != null and item.image != ''">
                       image = #{item.image,jdbcType=VARCHAR},
                  </if>
                  <if test="item.birthDate != null and item.birthDate != ''">
                       birth_date = #{item.birthDate,jdbcType=DATE},
                  </if>
                  <if test="item.address != null and item.address != ''">
                       address = #{item.address,jdbcType=DATE},
                  </if>
                  <if test="item.roleId != null">
                       role_id = #{item.roleId,jdbcType=INTEGER},
                  </if>
                  <if test="item.libraryCode != null and item.libraryCode != ''">
                       library_code = #{item.libraryCode,jdbcType=VARCHAR},
                  </if>
                  <if test="item.libraryName != null and item.libraryName != ''">
                       library_name = #{item.libraryName,jdbcType=VARCHAR},
                  </if>
                  <if test="item.status != null">
                       status = #{item.status,jdbcType=TINYINT},
                  </if>
                  <if test="item.hobby != null and item.hobby != ''">
                       hobby = #{item.hobby,jdbcType=VARCHAR},
                  </if>
                  <if test="item.updatedBy != null and item.updatedBy != ''">
                       updated_by = #{item.updatedBy,jdbcType=VARCHAR},
                  </if>
                       updated_dt = now(),
              </set>
                  where 
                       admin_account = #{item.adminAccount,jdbcType=VARCHAR}
        </foreach>
     </update>
### 提示如下错误:
  org.apache.ibatis.logging.jdbc.BaseJdbcLogger DEBUG  ==>  Preparing: update t_admin_info SET admin_name = ?, updated_dt = now() where admin_account = ? ; update t_admin_info SET admin_name = ?, updated_dt = now() where admin_account = ?  
org.apache.ibatis.logging.jdbc.BaseJdbcLogger DEBUG  ==> Parameters: 开发3(String), 20204058631b263(String), 开发4(String), 20204058631b264(String) 
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error updating database.  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 t_admin_info 
               SET admin_name = '开发4',
                  
' at line 24
修改数据库url:
数据库url:jdbc:mysql://localhost:3306/test_lib_dev?allowMultiQueries=true&serverTimezone=Asia/Shanghai

又提示:
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause: org.xml.sax.SAXParseException; lineNumber: 17; columnNumber: 114; 对实体 "serverTimezone" 的引用必须以 ';' 分隔符结尾。
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:64)
    at com.zj.test.TestUpdate.main(TestUpdate.java:25)
最后将数据库url:jdbc:mysql://localhost:3306/test_lib_dev?allowMultiQueries=true&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true&amp;zeroDateTimeBehavior=convertToNull&amp;serverTimezone=Asia/Shanghai
问题解决,记录一哈!
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容