*初学SSM,记录一下开发项目中遇到的问题。
1 个目录
项目开始测试,记录一下遇到的一些错误。
一、问题一
1.具体错误
### 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 'where id = 5' 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 'where id = 5' at line 3
2.解决方案
有以下两种可能1:
- 使用了Mysql的关键字
- WHERE前面有
,
本人属于后者,删掉即可
二、问题二:Mybatis中多参数传递
1.具体错误
忘记复制错误了,大意是找不到status字段。以下为报错的Mapper
相关代码:
//Mappe.java
int updateFriendMessageOfStatus(int id,int status);
//Mappe.xml
<update id="updateFriendMessageOfStatus" parameterType="int">
update friendsmessage
set status = #{status}
where id = #{id}
</update>
显然,是Mybatis
传递多参数的问题
2.解决方案
本人采用给每个参数加上@Param("")
注释
int updateFriendMessageOfStatus(@Param("id")int id,@Param("status")int status);
还有其他方法,详见这篇文章2。
三、问题三:Mabatis返回类型为int,但查询结果为空
1.具体错误
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Mapper method 'com.hua.dao.UserMapper.emailIsExist attempted to return null from a method with a primitive return type (int).
Mybatis
查询返回类型为int
,但查询结果为空NULL
,此时报错。
2.解决方案
通过IFNULL()
实现NULL-》0
的转换,以下为原代码和修改后的代码:
SELECT id
from user_common
where email=#{email}
SELECT IFNULL(MAX(id),0)
from user_common
where email=#{email}
四、问题四:Unknown column ‘nickName’ in ‘field list’
1.具体错误
### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'nickName' in 'field list'
在数据库中没有找到nickName
这个字段
2.解决方案
检查一下数据库中的字段名和实体类中的字段名是否一致。
五、问题五:前端传递参数与Controller接受参数不匹配
1.具体错误
The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
2.解决方案
前端传递参数与Controller接受参数不匹配,检查一下。
六、问题六:前端Json传值,后端Controller无法获取
1.具体错误
前端Json
传值,后端Controller
无法获取。打印了一下,发现用来接收的实体类为空。
一开始以为是Json写错了,没有和实体类的字段对应上。后来发现是后端无法识别Json
中的key值,需要加上@RequestBody
。
2.解决方案
后端@RequestBody注解对应的类在将HTTP的输入流(含请求体)装配到目标类(即:@RequestBody后面类)时,会根据json字符串中的key来匹配对应实体类的属性,如果匹配一致且json中的该key对应的值符合(或可转换为)实体类的对应属性的类型要求时,会调用实体类的setter方法将值赋给该属性。
————————————————
原文链接:https://blog.csdn.net/justry_deng/article/details/80972817/
给函数参数中的实体类加上@RequestBody
注解。
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: Optional int parameter 'fromUserId' is present but cannot be translated into a null value due to being declared as a primitive type. Consider declaring it as object wrapper for the corresponding primitive type.
和问题六类似的情况,但是不是实体类,需要用@RequestParam
注解。用于绑定controller上的参数,可以是多个参数,也可以是一个Map集合
七、问题七:esockettimedout
1.具体错误
使用apipost
测试的时候,返回esockettimedout
2.解决方案
实际上只是网速太差,超时了。最终服务端还是会响应成功。