SSM Mybatis将新增数据写入数据库时出现的500状态码:Error updating database.的可能
2023-09-11 14:22:20 时间
关于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
上面那个#和$的错误其实是看了一篇错的博客导致的,不过,也是因为那篇博客才让我去关注到这个问题,虽然现在还不是很理解
相关文章
- Ignoring query to other database
- Database数据库的分库分表,表映射,切换表
- ORA-01102: cannot mount database in EXCLUSIVE mode
- MySql: show databases/tables use database desc table
- [AWS DA] Database types & SSL
- 【Oracle 集群】ORACLE DATABASE 11G RAC 知识图文详细教程之集群概念介绍(一)
- 本地存储(LocalStorage、SessionStorage、Web SQL Database、Indexed DB)
- 如何使用ABAP code inspector找出所有在LOOP里访问database的操作
- Database之SQL:RDBMS关系型数据库的简介、SQL语言的简介(原理/各大方言对比)、基础(SQL执行顺序/五大类函数/索引/视图/事务/安全/存储过程/游标/优化)之详细攻略
- Database之SQLSever:SQLSever数据库管理学习并深入理解SQL命令语句进阶综合篇《初级→中级→高级》(持续更新,建议收藏)
- MybatisPlus出现Error querying database. Cause: java.lang.IndexOutOfBoundsException: Index: 6, Size: 6
- Getting Started with Database Administration
- VB.net:使用Visual Studio 2010中的VB.net语言工具箱DataGridView调用SQL数据库Database的表格文件
- MySQL创建数据库(CREATE DATABASE语句)