u012503756
bulidfer
采纳率35.3%
2016-07-25 10:01 浏览 3.4k

mybatis insert插入语句以后 一直报错怎么回事 怎么在保存以后获取保存对象的id

10

项目采用的框架是springmvc mybatis
数据库 mysql

控制器代码:

还有怎么在保存一一张表的同时 保存到另一张表 把A表的id 保存到B表(关联的表)

 List<EarDiseaseSwtj> listEdSwtj=earStudyService.findEdByid(id1);
            if (listEdSwtj.size() != 0) {
                //int num2 = earDisease100Service.updateEdSwtj(id1, name, desc);// 修改的仅是ear_disease_100这个表
                int num2=earStudyService.updateEdSwtj(id1, name, desc);
                if (num2 != 0) {
                    request.setAttribute("ok", "修改成功");
                }
                return "manage/forJsp.do?&requestType=17&num1=" + currentPageNum1;
            } else {
                //earStudyService.saveEdSwtj(id1, name, desc);
                earStudyService.saveEdSwtj(name, desc);
                request.setAttribute("ok", "添加成功");
                return "manage/forJsp.do?&requestType=17&num1=" + currentPageNum1;
            }
        }

mybatis sql如下:

 <insert id="saveEd" parameterType="EarDiseaseSwtj">
        insert into dt_ear_disease_swtj
        (name,desc)
        values
        (#{name},#{desc})
    </insert>

报错代码如下:

HTTP Status 500 - Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException:

type Exception report

message Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException:

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is 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 'desc)
        values
        ('qqqe','eqeqe')' at line 2
### The error may involve com.app.dao.mapper.EarDiseaseSwtjMapper.saveEd-Inline
### The error occurred while setting parameters
### SQL: insert into dt_ear_disease_swtj   (name,desc)   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 'desc)
        values
        ('qqqe','eqeqe')' at line 2
; 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 'desc)
        values
        ('qqqe','eqeqe')' at line 2
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
root cause

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 'desc)
        values
        ('qqqe','eqeqe')' at line 2
### The error may involve com.app.dao.mapper.EarDiseaseSwtjMapper.saveEd-Inline
### The error occurred while setting parameters
### SQL: insert into dt_ear_disease_swtj   (name,desc)   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 'desc)
        values
        ('qqqe','eqeqe')' at line 2
; 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 'desc)
        values
        ('qqqe','eqeqe')' at line 2
    org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:233)
    org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
    org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:71)
    org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:365)
    sun.proxy.$Proxy10.insert(Unknown Source)
    org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:237)
    org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:79)
    org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:40)
    sun.proxy.$Proxy39.saveEd(Unknown Source)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:601)
    org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
    org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    sun.proxy.$Proxy40.saveEd(Unknown Source)
    com.app.dao.impl.EarDiseaseSwtjDaoImpl.saveEd(Unknown Source)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:601)
    org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
    org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    sun.proxy.$Proxy41.saveEd(Unknown Source)
    com.app.service.impl.EarStudyServiceImpl.saveEdSwtj(Unknown Source)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:601)
    org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
    org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    sun.proxy.$Proxy99.saveEdSwtj(Unknown Source)
    com.app.totalController.ManageController.forJsp(Unknown Source)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:601)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:427)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
root cause
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

8条回答 默认 最新

  • zy_281870667 Bug开发攻城狮 2016-07-25 10:08

    1.报错,是因为desc是关键字,用引号引起来就好

    2.获得插入的id,用keyProperty
    相关资料,http://chenzhou123520.iteye.com/blog/1849881

    点赞 1 评论 复制链接分享
  • diaoliwei2 DreamTHT 2016-07-25 10:17


    <![CDATA[
    SELECT LAST_INSERT_ID() AS ID
    ]]>

    <![CDATA[
    insert into dt_ear_disease_swtj
    (name,desc)
    values
    (#{name},#{desc})
    ]]>

    
    
    点赞 1 评论 复制链接分享
  • u012503756 bulidfer 2016-07-25 11:59

    update更新的话就不会报错

    点赞 评论 复制链接分享
  • Mr_li13 黯雅悸动 2016-07-25 12:47

    desc是关键字,错误已经提示了

    点赞 评论 复制链接分享
  • u012503756 bulidfer 2016-07-25 12:53

    这样写就可以把关键字放进去了

        <!-- 添加辅助治疗科目疾病信息 -->
        <insert id="saveEd" parameterType="EarDiseaseSwtj">
        <![CDATA[
    
            insert into dt_ear_disease_swtj (`name`,`desc`) values (#{name},#{desc})
    
    ]]>
    </insert>
    
    点赞 评论 复制链接分享
  • u012503756 bulidfer 2016-07-25 13:21

    还是获取不到id呀

     EarDiseaseSwtj earDiseaseSwtj=new EarDiseaseSwtj();
                    earStudyService.saveEdSwtj(name, desc);
                    System.out.println(earDiseaseSwtj.getId());
                    request.setAttribute("ok", "添加成功");
                    return "manage/forJsp.do?&requestType=17&num1="
                            + currentPageNum1;
    
    点赞 评论 复制链接分享
  • diaoliwei2 DreamTHT 2016-07-26 02:05
    
    



    <![CDATA[
    SELECT LAST_INSERT_ID() AS ID
    ]]>

    <![CDATA[
    insert into dt_ear_disease_swtj (name,desc) values (#{name},#{desc})
    ]]>

    点赞 评论 复制链接分享
  • diaoliwei2 DreamTHT 2016-07-26 02:06

    图片说明

    点赞 评论 复制链接分享

相关推荐