最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決mybatis使用foreach批量insert異常的問題

 更新時(shí)間:2021年01月08日 16:00:37   作者:zm98  
這篇文章主要介紹了解決mybatis使用foreach批量insert異常的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

異常

org.springframework.jdbc.BadSqlGrammarException: 
### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'INSERT INTO t_user_role(userid,roleid)VALUES(1,3)
		 ; 
			INSERT INTO t_user_ro' at line 3
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: INSERT INTO t_user_role(userid,roleid)VALUES(?,?) ;  INSERT INTO t_user_role(userid,roleid)VALUES(?,?) ;  INSERT INTO t_user_role(userid,roleid)VALUES(?,?)
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'INSERT INTO t_user_role(userid,roleid)VALUES(1,3)
		 ; 
			INSERT INTO t_user_ro' at line 3
; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'INSERT INTO t_user_role(userid,roleid)VALUES(1,3)
		 ; 
			INSERT INTO t_user_ro' at line 3
	at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:231)
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:371)
	at com.sun.proxy.$Proxy13.insert(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:240)
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:51)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52)
	at com.sun.proxy.$Proxy14.addRoles(Unknown Source)
	at com.atguigu.atcrowdfunding.manager.service.impl.UserServiceImpl.addRoles(UserServiceImpl.java:139)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
	at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
	at com.sun.proxy.$Proxy17.addRoles(Unknown Source)
	at com.atguigu.atcrowdfunding.manager.controller.UserController.doAddRoles(UserController.java:271)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:833)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1526)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1482)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Unknown Source)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'INSERT INTO t_user_role(userid,roleid)VALUES(1,3)
		 ; 
			INSERT INTO t_user_ro' at line 3
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
	at com.mysql.jdbc.Util.getInstance(Util.java:387)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:941)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3870)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3806)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2470)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2617)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2550)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
	at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1192)
	at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.execute(NewProxyPreparedStatement.java:823)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:62)
	at com.sun.proxy.$Proxy24.execute(Unknown Source)
	at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:44)
	at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:69)
	at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:48)
	at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:105)
	at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:71)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:152)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:141)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:358)
	... 65 more

異常分析

mapper.xml導(dǎo)致的錯(cuò)誤地方

 <insert id="addRoles">
 <foreach collection="data.ids" item="id" separator=";">
 INSERT INTO t_user_role(userid,roleid)VALUES(#{userId},#{id})
 </foreach>
 </insert>

異常中說

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'INSERT INTO t_user_role(userid,roleid)VALUES(1,3)

首先覺得是 sql語句的問題,但是用sqlyog測試發(fā)現(xiàn)沒有問題

后來發(fā)現(xiàn)是數(shù)據(jù)庫對多個(gè)語句拼在一起的操作不支持問題

解決辦法

在jdbcUrl中加入allowMultiQueries=true即可解決

jdbc.url=jdbc:mysql://localhost:3306/atcrowdfunding?allowMultiQueries=true&rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8

補(bǔ)充:Mybatis foreach嵌套 批量insert map list數(shù)據(jù)

方式一:

以Teacher和Students為例 ,兩者為一對多關(guān)系:**

Teacher 實(shí)體類

private String tid;
private String tName;
private List<Student> studentList; 
public String getTid() {
 return tid;
}
 
public void setTid(String tid) {
 this.tid = tid;
}
 
public String gettName() {
 return tName;
}
 
public void settName(String tName) {
 this.tName = tName;
}
 
public List<Student> getStudentList() {
 return studentList;
}
 
public void setStudentList(List<Student> studentList) {
 this.studentList = studentList;
}

Student實(shí)體類

private String sid;
private String sName;
public String getSid() {
 return sid;
}
 
public void setSid(String sid) {
 this.sid = sid;
}
 
public String getsName() {
 return sName;
}
 
public void setsName(String sName) {
 this.sName = sName;
}

應(yīng)用場景 :在老師的service層批量插入老師數(shù)據(jù)

int nums=1000;
  
 //定義teacher list
 List<Teacher> teaList=new ArrayList<Teacher>();
 //數(shù)據(jù)是通過excel讀取的 此處模擬循環(huán)讀取excel row數(shù)據(jù)
 for(int rowi=0;rowi<nums;rowi++){
  Teacher teacher=new Teacher();
  teacher.settName("xxx");
 
  List<Student> stuList=new ArrayList<Student>();
  Student student=new Student();
  //此處獲取Students集合
  for(xxx){
   stuList.add(student);
  }
  //把student集合和teacher綁定
  teacher.setStudentList(stuList);
 
  //把teacher放入list
  teaList.add(teacher);
 }
 // 批量插入老師數(shù)據(jù)
 teacherMapper.batchInsertTeacher(teaList);
 //此時(shí)每個(gè)teacher對象都有返回的主鍵id值
 //老師主鍵 對應(yīng)一個(gè)list(學(xué)生信息) map
 Map<String,List<Student>> stuMap=new HashMap<>();
 //填入數(shù)據(jù) 老師主鍵一對多學(xué)生信息
 for(Teacher tea:teaList){
  //非空判斷 避免老師沒有對應(yīng)學(xué)生的情況(此處只進(jìn)行邏輯處理 不考慮現(xiàn)實(shí)中老師沒有學(xué)生)
  if(tea.getStudentList()!=null&&tea.getStudentList().size()>0){
   stuMap.put(tea.getTid(),tea.getStudentList());
  }
 }
 //調(diào)用學(xué)生service的批量保存學(xué)生方法
 studentService.batchInsertStudent(stuMap);

student Mapper接口文件

int batchInsertStudent(@Param("stuMap") Map<String,List<Student>> stuMap);

student mapper.xml文件(寫法一:)

<insert id="batchInsertStudent" parameterType="java.util.Map">
 INSERT INTO bs_student (sid,sName,tid) 
 values
 <foreach collection="stuMap.keys" index="key" item="itemKey" separator=",">
   <foreach collection="stuMap[itemKey]" index="index_list" item="list" separator="," >
    ( (select REPLACE(UUID(),'-','') AS sid) ,#{list.sName},#{itemKey} )
   </foreach>
 </foreach>
</insert>

student mapper.xml文件(寫法二:)

<insert id="batchInsertStudent" >
  INSERT INTO bs_student (sid,sName,tid)
  <foreach collection="stuMap.keys" index="index" item="itemKey" separator="UNION ALL">
  <foreach collection="stuMap[itemKey]" index="index_list" item="list" separator="UNION ALL">
      (
      SELECT 
      (select REPLACE(UUID(),'-','') AS sid)
      ,#{list.sName}
      ,#{itemKey}
      FROM DUAL
     ?。?
  </foreach>
  </foreach>
</insert>

stuMap:就是在接口中使用@Param(“stuMap”)標(biāo)注了變量;

使用stuMap.keys可以取到所有的key,遍歷。

內(nèi)層循環(huán)中使用stuMap[itemKey]類似stuMap.get(“key”)取當(dāng)前key對應(yīng)的value值。由于value是list所以還需要遍歷。

結(jié)果保存成功。再次膜拜強(qiáng)大的Mybatis。

至此 成功插入student數(shù)據(jù)。

方式二(推薦):

自定義類

class EnclosingType{
 private String uuid;
 private List<ElementType> elements;
}
class ElementType{
 String a;
 String b;
 (...)
}

Mapper.xml(方法一)

<mapper namespace="my.example.ElementType">
 <insert id="insertElements" parameterType="EnclosingType">
  INSERT INTO table1(enclosingTypeId,column_a,column_b)
  VALUES
  <foreach collection="elements" index="index" item="list" separator=",">  
   ( 
   #{uuid,jdbcType=VARCHAR}
   ,#{list.a,jdbcType=VARCHAR}
   ,#{list.b,jdbcType=VARCHAR}
   )
  </foreach>
 </insert>
</mapper>

Mapper.xml(方法二)

<mapper namespace="my.example.ElementType">
 <insert id="insertElements" parameterType="EnclosingType">
  INSERT INTO table1(enclosingTypeId,column_a,column_b)
  <foreach collection="elements" index="index" item="list" separator="union all">
   (
   select #{uuid,jdbcType=VARCHAR}
   ,#{list.a,jdbcType=VARCHAR}
   ,#{list.b,jdbcType=VARCHAR}
   from dual
   )
  </foreach>
 </insert>
</mapper>

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • 解決IDEA2020控制臺亂碼的方法

    解決IDEA2020控制臺亂碼的方法

    這篇文章主要介紹了解決IDEA2020控制臺亂碼的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • java基礎(chǔ)的詳細(xì)了解第六天

    java基礎(chǔ)的詳細(xì)了解第六天

    這篇文章對Java編程語言的基礎(chǔ)知識作了一個(gè)較為全面的匯總,在這里給大家分享一下。需要的朋友可以參考,希望能給你帶來幫助
    2021-08-08
  • SpringBoot 過濾器與攔截器實(shí)例演示

    SpringBoot 過濾器與攔截器實(shí)例演示

    本文通過示例代碼給大家講解SpringBoot 過濾器與攔截器的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-11-11
  • java實(shí)現(xiàn)合并圖片的方法示例

    java實(shí)現(xiàn)合并圖片的方法示例

    這篇文章主要介紹了java實(shí)現(xiàn)合并圖片的方法,結(jié)合具體實(shí)例形式分析了java基于圖片的讀取、設(shè)置、生成等操作實(shí)現(xiàn)圖片合并功能的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-02-02
  • SpringBoot實(shí)現(xiàn)掃碼登錄的示例代碼

    SpringBoot實(shí)現(xiàn)掃碼登錄的示例代碼

    本文主要介紹了SpringBoot實(shí)現(xiàn)掃碼登錄的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • mybatis注解之@Mapper和@MapperScan的使用

    mybatis注解之@Mapper和@MapperScan的使用

    這篇文章主要介紹了mybatis注解之@Mapper和@MapperScan的使用,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 淺談Java工程讀取resources中資源文件路徑的問題

    淺談Java工程讀取resources中資源文件路徑的問題

    下面小編就為大家?guī)硪黄獪\談Java工程讀取resources中資源文件路徑的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07
  • 基于params、@PathVariabl和@RequestParam的用法與區(qū)別說明

    基于params、@PathVariabl和@RequestParam的用法與區(qū)別說明

    這篇文章主要介紹了方法參數(shù)相關(guān)屬性params、@PathVariabl和@RequestParam用法與區(qū)別,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • list轉(zhuǎn)tree和list中查找某節(jié)點(diǎn)下的所有數(shù)據(jù)操作

    list轉(zhuǎn)tree和list中查找某節(jié)點(diǎn)下的所有數(shù)據(jù)操作

    這篇文章主要介紹了list轉(zhuǎn)tree和list中查找某節(jié)點(diǎn)下的所有數(shù)據(jù)操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • 淺談springboot中tk.mapper代碼生成器的用法說明

    淺談springboot中tk.mapper代碼生成器的用法說明

    這篇文章主要介紹了淺談springboot中tk.mapper代碼生成器的用法說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09

最新評論

卓资县| 泰和县| 潜江市| 兴和县| 和静县| 兖州市| 玛曲县| 清远市| 寻甸| 抚顺市| 丹棱县| 平舆县| 巢湖市| 崇信县| 临城县| 弥勒县| 益阳市| 绥化市| 榆树市| 平舆县| 玉屏| 随州市| 会宁县| 城口县| 曲靖市| 白沙| 双牌县| 永寿县| 紫云| 兴仁县| 自治县| 南昌县| 房山区| 巴马| 皮山县| 宝坻区| 辽中县| 永安市| 徐水县| 深州市| 长泰县|