zoukankan      html  css  js  c++  java
  • SSM Mybatis将新增数据写入数据库时出现的500状态码:Error updating database.的可能

    关于maven下ssm整合的项目推荐这篇博客:https://www.cnblogs.com/yiye/p/5969157.html

    今日在ssm下的将新增数据写回数据库时遇到不少的问题,现作记录

    如果只是简单地使用Mybatis的话,具体的流程如下:

    创建配置文件——>根据配置文件来生成会话工厂——>通过工厂来生成会话——>通过会话操作数据库!

    具体可以参考这里:http://how2j.cn/k/mybatis/mybatis-crud/1088.html#nowhere

    我理解就是如下图的三个步骤

     

    好了,说了上面的废话其实是想说,在ssm中,操作数据库也是要配置sessionFactory的,只是不再像上图那样,而是在mybatis的配置文件(或者spring的相关配置文件)中完成,如下图:

     

    如果没整合过的,可以先看:https://www.cnblogs.com/yiye/p/5969157.html

     

    回归问题上来,在插入数据库时出现500的状态码错误!!!

     

     

    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: 
    ### 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 ',,,)' at line 1
    ### The error may involve com.example.edu.mapper.UserMapper.insert-Inline
    ### The error occurred while setting parameters
    ### SQL: insert into t_user (username,password,birthday,gender) values (,,,)
    ### 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 ',,,)' at line 1

    根据提示,是说SQL语法错误?然后,我发现自己瞎了,参考的视频是这样的(注意红色框内)

     

    没错,我以为红色框内的字段是用单引号引起来的,然后我的SQL语句中就都用单引号引起来了!!!
    谁知道ta用的是 ` 这个点?????

    所以最后的SQL语句应该是
     <insert id="insert" parameterType="User" useGeneratedKeys="true" keyProperty="id">
            insert into t_user (`username`,`password`,`birthday`,`gender`) values (#{username},#{password},#{birthday},#{gender})
      </insert>
    或者是(去掉那个点)
     <insert id="insert" parameterType="User" useGeneratedKeys="true" keyProperty="id">
            insert into t_user (username,password,birthday,gender) values (#{username},#{password},#{birthday},#{gender})
     </insert>

     

     

    然后还遇到了一个这样的问题

    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: 
    ### Error updating database.  Cause: java.sql.SQLSyntaxErrorException: Unknown column 'Karen' in 'field list'
    ### The error may involve com.example.edu.mapper.UserMapper.insert-Inline
    ### The error occurred while setting parameters
    ### SQL: insert into t_user (username,password,birthday,gender) values (Karen,kl,19990912,female)
    ### Cause: java.sql.SQLSyntaxErrorException: Unknown column 'Karen' in 'field list'
    
    

     

    原因是我的sql语句中的#全用了 $,就是下面这样的,是错的,是错的!
     <insert id="insert" parameterType="User" useGeneratedKeys="true" keyProperty="id">
            insert into t_user (username,password,birthday,gender) values (${username},${password},${birthday},${gender})
     </insert>

     

    引入了一个 关于 # 和 $ 的区别
    好吧我不知道什么鬼,看这篇博客吧:https://www.cnblogs.com/kangyun/p/5881531.html


    后续:

    这也是我第一次玩ssm,刚开始有以为也要想单一使用mybatis那样,要创建具体的会话工厂来操作数据库,要commit和close,后来才知道在xml中直接配置就行了,果然niubi
    上面那个#和$的错误其实是看了一篇错的博客导致的,不过,也是因为那篇博客才让我去关注到这个问题,虽然现在还不是很理解


  • 相关阅读:
    Gym101630A Archery Tournament
    BZOJ2588 Count on a tree
    Redis主从复制
    Redis事务
    Redis持久化RDB和AOF
    设计模式之代理模式
    Spring AOP(面向切面编程)
    基于TCP和UDP的Socket通信
    Ajax无法访问回调函数seccess问题
    SpringBoot Ajax跨域问题(session共享问题)
  • 原文地址:https://www.cnblogs.com/Guhongying/p/10692749.html
Copyright © 2011-2022 走看看