前两天在学spring boot的时候,出现了一个很奇怪的错误,因为是第一次使用spring boot,所以没想到会遇到这种莫名其妙的bug,即调用接口删除数据库中一条记录的时候,数据库中记录事实上以及被删除了,但是却返回一个null,这就令我百思不得其解了,理论上,删除的话,会返回受影响的记录的条数。

最后排查了一圈,结果却十分令我大跌眼镜,真的很简单!下面写的代码:

  • controller类,这里由于后来数据库sql改了,为了测试like的搜索功能,所以前面的index方法参数并未进行及时修改,由于本文不涉及该方法,所以请忽略!
  1. package site.wangxin520.springboot.web;
  2.  
  3. import java.util.Map;
  4. import java.util.Map.Entry;
  5. import java.util.Set;
  6.  
  7. import javax.servlet.http.HttpServletRequest;
  8.  
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.web.bind.annotation.PathVariable;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.RequestMethod;
  13. import org.springframework.web.bind.annotation.RestController;
  14.  
  15. import site.wangxin520.springboot.service.IndexService;
  16.  
  17. @RequestMapping("/")
  18. @RestController
  19. public class Index {
  20.  
  21. @Autowired
  22. private IndexService indexService;
  23.  
  24. /**
  25. * resful的风格,从path里面获取到id,读取数据库展示数据
  26. * @param request
  27. * @param id
  28. * @return
  29. */
  30. @RequestMapping("/{id}")
  31. public String index(HttpServletRequest request,@PathVariable("id") String id){
  32.  
  33. Map<String, String[]> parameterMap = request.getParameterMap();
  34. Set<Entry<String, String[]>> entrySet = parameterMap.entrySet();
  35. for (Entry<String, String[]> entry : entrySet) {
  36. System.out.println(entry.getKey()+"\t:\t"+entry.getValue());
  37. }
  38. String name = indexService.getName(id);
  39. return name;
  40.  
  41. }
  42.  
  43. /**
  44. * 通过id,去删除数据
  45. * @param request
  46. * @param id
  47. * @return
  48. */
  49. @RequestMapping(value="/",method={RequestMethod.DELETE})
  50. public String deleteById(HttpServletRequest request,Integer id){
  51.  
  52. int deleteById = indexService.deleteById(id);
  53.  
  54. return deleteById+"";
  55.  
  56. }
  57.  
  58. }
  • service接口
  1. package site.wangxin520.springboot.service;
  2.  
  3. public interface IndexService {
  4.  
  5. /**
  6. * 通过id,获取名字
  7. * @param id
  8. * @return
  9. */
  10. public String getName(String id);
  11.  
  12. /**
  13. * 通过id,删除元素
  14. * @param id
  15. * @return
  16. */
  17. public int deleteById(Integer id);
  18.  
  19. }
  • service实现类
  1. package site.wangxin520.springboot.service.impl;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5.  
  6. import site.wangxin520.springboot.dao.IndexMapper;
  7. import site.wangxin520.springboot.service.IndexService;
  8.  
  9. @Service
  10. public class IndexServiceImpl implements IndexService{
  11.  
  12. @Autowired
  13. private IndexMapper mapper;
  14.  
  15. @Override
  16. public String getName(String id) {
  17. return mapper.getName(id+"%");
  18. }
  19.  
  20. @Override
  21. public int deleteById(Integer id) {
  22. return mapper.deleteById(id);
  23. }
  24.  
  25. }
  • dao层接口,在dao层,使用的是注解的方式进行mapper的自动实现。
  1. package site.wangxin520.springboot.dao;
  2.  
  3. import org.apache.ibatis.annotations.Mapper;
  4. import org.apache.ibatis.annotations.Select;
  5.  
  6. @Mapper
  7. public interface IndexMapper {
  8.  
  9. // @Select("SELECT username FROM `user` WHERE id=#{id};")
  10. @Select("SELECT username from user where username LIKE #{id};")
  11. public String getName(String id);
  12.  
  13. @Select("DELETE FROM `user` where id =#{id};")
  14. public Integer deleteById(int id);
  15. }
  • 源数据库记录

  • 启动项目,使用httprequest调用controller调用网络接口

结果却令我大跌眼镜,竟然报服务器异常,并且空指针了

  • 查看数据库

没想到数据库竟然成功的删除了id为3的这条记录。

  • 查看控制台

在控制台上打印出了空指针,根据错误信息,定位到了site.wangxin520.springboot.dao.IndexMapper.deleteById(int)这个方法,因为返回的是null。

这时候我就有疑惑了,理论上删除返回的并不是null啊,而是影响的行数,这次这是什么情况。后来我自习的查看了一下,发现了错误的信息原来真的是很狗血的!

  1. @Select("DELETE FROM `user` where id =#{id};")

错误就错在这个注解上,删除应该是使用注解@Delete,而不是Select。

顿时我就无语了,把这个dao接口重新修改以后,再次运行。

  • dao接口
  1. package site.wangxin520.springboot.dao;
  2.  
  3. import org.apache.ibatis.annotations.Delete;
  4. import org.apache.ibatis.annotations.Mapper;
  5. import org.apache.ibatis.annotations.Select;
  6.  
  7. @Mapper
  8. public interface IndexMapper {
  9.  
  10. // @Select("SELECT username FROM `user` WHERE id=#{id};")
  11. @Select("SELECT username from user where username LIKE #{id};")
  12. public String getName(String id);
  13.  
  14. // @Select("DELETE FROM `user` where id =#{id};")
  15. @Delete("DELETE FROM `user` where id =#{id};")
  16. public Integer deleteById(int id);
  17. }
  • 启动项目,使用httprequest调用接口

  • 查看数据库

id为2的记录成功删除,并且返回一个1,即所影响的记录数!

一切正常,这个小错误真的可以说是人为的,以后得多注意!

spring boot继承web和mybatis时,调用接口删除记录出现的空指针以及解决办法的更多相关文章

  1. Spring Boot 嵌入式Web容器

    目录 前言 1.起源 2.容器启动流程解析 2.1.获取应用类型 2.2.容器启动流程 3.加载 Web 容器工厂 4.总结 前言         最近在学习Spring Boot相关的课程,过程中以 ...

  2. 07.深入浅出 Spring Boot - 数据访问之Mybatis(附代码下载)

    MyBatis 在Spring Boot应用非常广,非常强大的一个半自动的ORM框架. 代码下载:https://github.com/Jackson0714/study-spring-boot.gi ...

  3. 使用Spring Boot开发Web项目(二)之添加HTTPS支持

    上篇博客使用Spring Boot开发Web项目我们简单介绍了使用如何使用Spring Boot创建一个使用了Thymeleaf模板引擎的Web项目,当然这还远远不够.今天我们再来看看如何给我们的We ...

  4. Spring Boot 框架下使用MyBatis访问数据库之基于XML配置的方式

    MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单的 XML ...

  5. Spring Boot开发Web应用之Thymeleaf篇

    前言 Web开发是我们平时开发中至关重要的,这里就来介绍一下Spring Boot对Web开发的支持. 正文 Spring Boot提供了spring-boot-starter-web为Web开发予以 ...

  6. Spring Boot + JPA(hibernate 5) 开发时,数据库表名大小写问题

      (转载)Spring Boot + JPA(hibernate 5) 开发时,数据库表名大小写问题   这几天在用spring boot开发项目, 在开发的过程中遇到一个问题hibernate在执 ...

  7. spring boot中使用@Async实现异步调用任务

    本篇文章主要介绍了spring boot中使用@Async实现异步调用任务,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 什么是“异步调用”? “异步调用”对应的是“同步 ...

  8. spring boot 2.0.0 + mybatis 报:Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

    spring boot 2.0.0 + mybatis 报:Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required 无法启动 ...

  9. 【spring boot】5.spring boot 创建web项目并使用jsp作前台页面

    贼烦的是,使用spring boot 创建web项目,然后我再idea下创建的,but 仅仅启动spring boot的启动类,就算整个项目都是好着的,就算是能够进入controller中,也不能成功 ...

随机推荐

  1. Android开发-- The content of the adapter has changed but ListView did not receive a notification - With AsyncTask

    最近在联系开发DaysMatter时遇到一个问题: app中使用ListView来展示所有事件,每次添加完事件后使用下面代码来更新ListView. toDoListView.refreshDrawa ...

  2. Django SimpleCMDB 使用序列化

    如下,前面我们是使用 urllib 方法来转换并传递数据的: [root@localhost ~]$ tail /data/script/getHostInfo.py if __name__ == ' ...

  3. 文件名过滤器FilenameFilter的用法

    Java.io.FilenameFilter是文件名过滤器,用来过滤不符合规格的文件名,并返回合格的文件: 实例1,匹配指定字符结尾的文件 package cn.test; import java.i ...

  4. PHP关于=>和->以及::的用法

    1.=>的用法 在php中数组默认键名是整数,也可以自己定义任意字符键名(最好是有实际意义),如: $css=array('style'=>'0',‘color’=>‘green‘) ...

  5. django进阶-小实例

    前言: 这篇博客对上篇博客django进阶作下补充. 一.效果图 前端界面较简单(丑),有两个功能: 从数据库中取出书名 eg: 新书A 在form表单输入书名,选择出版社,选择作者(多选),输入完毕 ...

  6. 【python3】urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)>

    在玩爬虫的时候,针对https ,需要单独处理.不然就会报错: 解决办法:引入 ssl 模块即可 核心代码 imort ssl ssl._create_default_https_context = ...

  7. 【Laravel5.5】 laravel5 数据库配置(MySQL)

    1   进入laravel根目录.      在config目录下找到database.php文件.      显而易见这个文件是数据库相关的配置文件. 2  修改 .env 配置完database. ...

  8. R语言(入门小练习篇)

    问题: 一组学生参加了数学.科学和英语考试.为了给所有的学生确定一个单一的成绩衡量指标,需要将这些科目的成绩组合起来.另外,还想将前20%的学生评定为A,接下来20%的学生评定为B,以此类推.最后,希 ...

  9. 蓝凌OA常用表整理

    SELECT * FROM V_FI_ORG_EMP  --用户表视图(关联单位)SELECT * FROM FI_ORG_EMP  --用户表 SELECT * FROM FI_ORG_INFO   ...

  10. window下node更新

    打开cmd查看你之前node版本安装的路径,where node: 直接去官网下载与你电脑系统(32位还是64位)对应的最新的mis版本,安装在上述路径中覆盖即可. 注意:windows上并不支持n模 ...