When spring boot inherits the Web and MyBatis, the calling interface deletes the null pointer that appears on the record and the workaround

Source: Internet
Author: User

When I was learning Spring boot two days ago, there was a very strange mistake, because it was the first time to use spring boot, so I did not expect to encounter this inexplicable bug, that is, when the calling interface deletes a record in the database, the record in the database is actually deleted. But it returns a null, which makes me baffled, in theory, if deleted, returns the number of affected records.

The final sweep of a lap, the result is very much so that I tumbled glasses, really simple! The following code is written:

    • Controller class, here because later database SQL changed, in order to test like the search function, so the previous index method parameters are not modified in time, because this article does not involve the method, so please ignore!
 PackageSite.wangxin520.springboot.web;ImportJava.util.Map;ImportJava.util.Map.Entry;ImportJava.util.Set;Importjavax.servlet.http.HttpServletRequest;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.web.bind.annotation.PathVariable;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;ImportOrg.springframework.web.bind.annotation.RestController;ImportSite.wangxin520.springboot.service.IndexService; @RequestMapping ("/") @RestController Public classIndex {@AutowiredPrivateIndexservice Indexservice; /*** resful style, get ID from path, read database display data *@paramRequest *@paramID *@return     */@RequestMapping ("/{id}")     PublicString 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); returnname; }        /*** by ID, go to delete data *@paramRequest *@paramID *@return     */@RequestMapping (Value= "/", method={requestmethod.delete}) PublicString Deletebyid (httpservletrequest request,integer id) {intDeletebyid =Indexservice.deletebyid (ID); returnDeletebyid+ ""; }    }
    • Service Interface
 PackageSite.wangxin520.springboot.service; Public InterfaceIndexservice {/*** by ID, get name *@paramID *@return     */     Publicstring GetName (string id); /*** by ID, delete element *@paramID *@return     */     Public intDeletebyid (Integer ID); }
    • Service Implementation Class
 PackageSite.wangxin520.springboot.service.impl;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Service;ImportSite.wangxin520.springboot.dao.IndexMapper;ImportSite.wangxin520.springboot.service.IndexService; @Service Public classIndexserviceimplImplementsindexservice{@AutowiredPrivateIndexmapper Mapper; @Override Publicstring GetName (string id) {returnMapper.getname (id+ "%"); } @Override Public intDeletebyid (Integer id) {returnMapper.deletebyid (ID); }}
    • The DAO layer interface, in the DAO layer, uses annotations to automate the implementation of mapper.
 PackageSite.wangxin520.springboot.dao;ImportOrg.apache.ibatis.annotations.Mapper;Importorg.apache.ibatis.annotations.Select; @Mapper Public InterfaceIndexmapper {//@Select ("Select username from ' user ' WHERE id=#{id};")@Select ("Select username from user where username like #{id};")     Publicstring GetName (string id); @Select ("DELETE from ' user ' where ID =#{id};")     PublicInteger Deletebyid (intID);}
    • Source database records

    • Start the project, call the controller using HttpRequest to call the network interface

The result makes me fall into glasses, unexpectedly reported server exception, and empty pointer

    • View Database

Unexpectedly the database successfully deleted the ID 3 of this record.

    • View the console

A null pointer is printed on the console, which, based on the error message, locates the site.wangxin520.springboot.dao.IndexMapper.deleteById (int) method because NULL is returned.

This time I have doubts, theoretically deleted returned is not null Ah, but the number of rows affected, this is what the case. Later I looked at the self-study, found the wrong information is really very dog blood!

@Select ("DELETE from ' user ' where ID =#{id};")
The error is wrong on this annotation, the deletion should be using the annotation @delete instead of the select.

Suddenly I have no words, the DAO interface to re-modify, run again.

    • DAO interface
 PackageSite.wangxin520.springboot.dao;ImportOrg.apache.ibatis.annotations.Delete;ImportOrg.apache.ibatis.annotations.Mapper;Importorg.apache.ibatis.annotations.Select; @Mapper Public InterfaceIndexmapper {//@Select ("Select username from ' user ' WHERE id=#{id};")@Select ("Select username from user where username like #{id};")     Publicstring GetName (string id); //@Select ("DELETE from ' user ' where ID =#{id};")@Delete ("Delete from ' user ' where ID =#{id};")     PublicInteger Deletebyid (intID);}
    • Starting the project, using the HttpRequest call interface

    • View Database

The record with ID 2 was successfully deleted and returned a 1, which is the number of records affected!

Everything is normal, this small mistake really can be said to be artificial, after much attention!

When spring boot inherits the Web and MyBatis, the calling interface deletes the null pointer that appears on the record and the workaround

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.