SSM项目开发中遇到的问题(三)——测试中遇到的一些问题

*初学SSM,记录一下开发项目中遇到的问题。

项目开始测试,记录一下遇到的一些错误。

一、问题一

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.解决方案

实际上只是网速太差,超时了。最终服务端还是会响应成功。

八、参考文章


  1. Mysql错误:check the manual that corresponds to your MySQL server version for the right syntax. ↩︎

  2. Mybatis注解方式传递多个参数的4种方式. ↩︎

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值