記錄一個關于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: 開發(fā)3(String), 20204058631b263(String), 開發(fā)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 = '開發(fā)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
問題解決,記錄一哈!
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容