MyBatis details not to be ignored

Source: Internet
Author: User

Self-summary, welcome to shoot Bricks!

Objective: In case of the need to return basic type data such as Int,long, try to use the basic type of wrapper class in MyBatis mapper.

Cause: When the queried field is NULL, MyBatis returns NULL, and an exception is received with the underlying type, but the wrapper class is used to circumvent the problem.

I. Reading Data Environment preparation:

Create a student table (no primary key):

CREATE TABLE student (ID int,name varchar (), idcard bigint,classno varchar (10));

INSERT into student set id=1,name= ' 007 ';

INSERT INTO STUDETN set name = ' 911911 ';

Two. Code:

Studentmapper.xml

 <?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" >< Mapper namespace= "Com.core.mapper.StudentMapper" > <select id= "getstudent" resulttype= " Com.core.entity.Student "> select Id,name,idcard,classno from Student where ID = #{id}  </select> <select id=" Getidcard "resulttype="   Long   "> select Idcard from student where ID  = #{id}  </select> <select id= "getId" resulttype= "  integer< /strong>  "> SELECT ID from student where name  = #{name}  </select></mapper> 


Studentmapper.java

 PackageCom.core.mapper;ImportOrg.apache.ibatis.annotations.Param;Importcom.core.entity.Student; Public InterfaceStudentmapper { PublicStudent getstudent (@Param ("id")intID);  Public  Long Getidcard (@Param ("id")intID);  Public  Integer GetId (@Param ("name") String name);}

Test class Studenttest

 Packagecom.core.test;Importorg.apache.ibatis.session.SqlSession;Importorg.junit.Test;Importcom.core.common.SessionFactory;Importcom.core.entity.Student;ImportCom.core.mapper.StudentMapper; Public classStudenttest { Public Static voidMain (string[] args) {sqlsession session=sessionfactory.getsqlsession (); Try{studentmapper Mapper= Session.getmapper (studentmapper.class); Student Blog= Mapper.getstudent (1);        System.out.println (Blog.getname ()); } finally{session.close (); }            }            //get Idcard by ID@Test Public voidTestgetidcard () {sqlsession session=sessionfactory.getsqlsession (); Try{studentmapper Mapper= Session.getmapper (studentmapper.class); Long Idcard= Mapper.getidcard (1); } finally{session.close (); }    }            //get ID by name@Test Public voidTestgetidbyname () {sqlsession session=sessionfactory.getsqlsession (); Try{studentmapper Mapper= Session.getmapper (studentmapper.class); Integer ID= Mapper.getid ("911911"); } finally{session.close (); }    }    }

3. Analysis:

1. If the return value type of Getidcard in Studentmapper.java and Studentmapper.xml is type long, the parameter ID passed is 1, and the record for the query Idcard is Null,long The type cannot accept null values, so the exception is reported, and if all are changed to wrapper class long in the code above, a null value can be accepted.

2. If the return value type of GetID in Studentmapper.java and studentmapper.xml is type int, the parameter passed with name is ' 911911 ' and the record for the query ID is null,int type cannot accept null value , so the exception will be reported, if the above code is changed to the wrapper class integer, you can accept the null value.

Summary: If you need to return basic types of data such as Int,long, try to use the underlying type of wrapper class in MyBatis mapper.

PS: In Mapper.java as far as possible with @param this parameter transfer mode and mapper.xml to correspond. So you don't have to configure attribute ParameterType in Mapper.xml.

Problem Description:

MyBatis details not to be ignored

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.