spring boot继承web和mybatis时,调用接口删除记录出现的空指针以及解决办法
前两天在学spring boot的时候,出现了一个很奇怪的错误,因为是第一次使用spring boot,所以没想到会遇到这种莫名其妙的bug,即调用接口删除数据库中一条记录的时候,数据库中记录事实上以及被删除了,但是却返回一个null,这就令我百思不得其解了,理论上,删除的话,会返回受影响的记录的条数。
最后排查了一圈,结果却十分令我大跌眼镜,真的很简单!下面写的代码:
- controller类,这里由于后来数据库sql改了,为了测试like的搜索功能,所以前面的index方法参数并未进行及时修改,由于本文不涉及该方法,所以请忽略!
package site.wangxin520.springboot.web; import java.util.Map;
import java.util.Map.Entry;
import java.util.Set; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import site.wangxin520.springboot.service.IndexService; @RequestMapping("/")
@RestController
public class Index { @Autowired
private IndexService indexService; /**
* resful的风格,从path里面获取到id,读取数据库展示数据
* @param request
* @param id
* @return
*/
@RequestMapping("/{id}")
public String index(HttpServletRequest request,@PathVariable("id") String id){ Map<String, String[]> parameterMap = request.getParameterMap();
Set<Entry<String, String[]>> entrySet = parameterMap.entrySet();
for (Entry<String, String[]> entry : entrySet) {
System.out.println(entry.getKey()+"\t:\t"+entry.getValue());
}
String name = indexService.getName(id);
return name; } /**
* 通过id,去删除数据
* @param request
* @param id
* @return
*/
@RequestMapping(value="/",method={RequestMethod.DELETE})
public String deleteById(HttpServletRequest request,Integer id){ int deleteById = indexService.deleteById(id); return deleteById+""; } }
- service接口
package site.wangxin520.springboot.service;
public interface IndexService {
/**
* 通过id,获取名字
* @param id
* @return
*/
public String getName(String id);
/**
* 通过id,删除元素
* @param id
* @return
*/
public int deleteById(Integer id);
}
- service实现类
package site.wangxin520.springboot.service.impl; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import site.wangxin520.springboot.dao.IndexMapper;
import site.wangxin520.springboot.service.IndexService; @Service
public class IndexServiceImpl implements IndexService{ @Autowired
private IndexMapper mapper; @Override
public String getName(String id) {
return mapper.getName(id+"%");
} @Override
public int deleteById(Integer id) {
return mapper.deleteById(id);
} }
- dao层接口,在dao层,使用的是注解的方式进行mapper的自动实现。
package site.wangxin520.springboot.dao; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select; @Mapper
public interface IndexMapper { // @Select("SELECT username FROM `user` WHERE id=#{id};")
@Select("SELECT username from user where username LIKE #{id};")
public String getName(String id); @Select("DELETE FROM `user` where id =#{id};")
public Integer deleteById(int id);
}
- 源数据库记录

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

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

没想到数据库竟然成功的删除了id为3的这条记录。
- 查看控制台
在控制台上打印出了空指针,根据错误信息,定位到了site.wangxin520.springboot.dao.IndexMapper.deleteById(int)这个方法,因为返回的是null。
这时候我就有疑惑了,理论上删除返回的并不是null啊,而是影响的行数,这次这是什么情况。后来我自习的查看了一下,发现了错误的信息原来真的是很狗血的!
@Select("DELETE FROM `user` where id =#{id};")
错误就错在这个注解上,删除应该是使用注解@Delete,而不是Select。
顿时我就无语了,把这个dao接口重新修改以后,再次运行。
- dao接口
package site.wangxin520.springboot.dao; import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select; @Mapper
public interface IndexMapper { // @Select("SELECT username FROM `user` WHERE id=#{id};")
@Select("SELECT username from user where username LIKE #{id};")
public String getName(String id); // @Select("DELETE FROM `user` where id =#{id};")
@Delete("DELETE FROM `user` where id =#{id};")
public Integer deleteById(int id);
}
- 启动项目,使用httprequest调用接口

- 查看数据库

id为2的记录成功删除,并且返回一个1,即所影响的记录数!
一切正常,这个小错误真的可以说是人为的,以后得多注意!
spring boot继承web和mybatis时,调用接口删除记录出现的空指针以及解决办法的更多相关文章
- Spring Boot 嵌入式Web容器
目录 前言 1.起源 2.容器启动流程解析 2.1.获取应用类型 2.2.容器启动流程 3.加载 Web 容器工厂 4.总结 前言 最近在学习Spring Boot相关的课程,过程中以 ...
- 07.深入浅出 Spring Boot - 数据访问之Mybatis(附代码下载)
MyBatis 在Spring Boot应用非常广,非常强大的一个半自动的ORM框架. 代码下载:https://github.com/Jackson0714/study-spring-boot.gi ...
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
上篇博客使用Spring Boot开发Web项目我们简单介绍了使用如何使用Spring Boot创建一个使用了Thymeleaf模板引擎的Web项目,当然这还远远不够.今天我们再来看看如何给我们的We ...
- Spring Boot 框架下使用MyBatis访问数据库之基于XML配置的方式
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单的 XML ...
- Spring Boot开发Web应用之Thymeleaf篇
前言 Web开发是我们平时开发中至关重要的,这里就来介绍一下Spring Boot对Web开发的支持. 正文 Spring Boot提供了spring-boot-starter-web为Web开发予以 ...
- Spring Boot + JPA(hibernate 5) 开发时,数据库表名大小写问题
(转载)Spring Boot + JPA(hibernate 5) 开发时,数据库表名大小写问题 这几天在用spring boot开发项目, 在开发的过程中遇到一个问题hibernate在执 ...
- spring boot中使用@Async实现异步调用任务
本篇文章主要介绍了spring boot中使用@Async实现异步调用任务,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 什么是“异步调用”? “异步调用”对应的是“同步 ...
- spring boot 2.0.0 + mybatis 报:Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
spring boot 2.0.0 + mybatis 报:Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required 无法启动 ...
- 【spring boot】5.spring boot 创建web项目并使用jsp作前台页面
贼烦的是,使用spring boot 创建web项目,然后我再idea下创建的,but 仅仅启动spring boot的启动类,就算整个项目都是好着的,就算是能够进入controller中,也不能成功 ...
随机推荐
- Scala单例对象和伴生对象
1.Scala单例对象 Scala单例对象是十分重要的,没有像在Java一样,有静态类.静态成员.静态方法,但是Scala提供了object对象,这个object对象类似于Java的静态类,它的成员. ...
- Python学习(26):Python函数式编程
转自 http://www.cnblogs.com/BeginMan/p/3509985.html 前言 <core python programming 2>说: Python不大可能 ...
- IOS设计模式第二篇之单例设计模式
现在我们的组件已经有组织了.你需要从其他的地方得到数据,你也可以创建一个API类管理数据这个下个设计模式单例里面介绍. 这个单例设计模式确保这个类仅仅拥有一个实例,并且为这个实例提供一个全局的访问点. ...
- 【Spring源码分析系列】搭建Spring实现容器的基本实现
前言 bean是Spring中最核心的东西,因为Spring就像一个大水桶,而bean就像是容器中的水,先新建一个小例子来看一下: 一.使用eclipse构建项目,项目结构如下 二.类文件内容 < ...
- java(2) 面向对象
1.类的封装 *在定义一个类时,将类中的属性私有化,即使用prviate关键字来修饰,私有属性只能在它所在的类中被访问.为了能让外界访问私有属性,需要提供一些使用public修饰的公有方法,其中包括用 ...
- ActiveMQ 消息持久化到数据库(Mysql、SQL Server、Oracle、DB2等)
ActiveMQ具体就不介绍了,直接介绍如何讲ActiveMQ持久化到本地数据库,以SQL Server 2008 R2为例1.下载ActiveMQ后直接解压,我下载的是apache-activemq ...
- LeetCode 81 Search in Rotated Sorted Array II(循环有序数组中的查找问题)
题目链接:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/#/description 姊妹篇:http://www. ...
- sencha touch Button Select(点击按钮进行选择)扩展
此扩展基于官方selectfield控件修改而来,变动并不大,使用方法类似. 代码如下: Ext.define('ux.SelectBtn', { extend: 'Ext.Button', xtyp ...
- Debian的自动化安装(DEBIAN_FRONTEND)
Debian 安装程序的参数 安装系统确认一些附加的引导参数 debconf/priority 这些参数设置将设置显示的信息为为最低的级别. 缺省安装使用 debconf/priority=high ...
- SOA架构商城一
SOA架构: SOA是Service-Oriented Architecture的首字母简称,它是一种支持面向服务的架构样式.从服务.基于服务开发和服务的结果来看,面向服务是一种思考方式.其实SOA架 ...