mybatis不可忽略的细节

时间:2021-04-17 15:41:58

自我总结,欢迎拍砖!

目的:在需要返回int,long等基础类型数据的情况下,尽量在mybatis的Mapper中用基础类型的包装类。

原因:当查询的字段为空值时,mybatis会返回null,用基础类型接收则会出现异常,但是用包装类就把这个问题规避了。

 

一.读数据环境准备:

创建student表(无主键):

  create table student(id int,name varchar(20),idCard bigint,classNo varchar(10));

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

  insert into studetn set name ='911911';

 

二.代码:

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">
        select id from student where name = #{name}
    </select>
</mapper>


StudentMapper.java

package com.core.mapper;

import org.apache.ibatis.annotations.Param;

import com.core.entity.Student;

public interface StudentMapper {
    public Student getStudent(@Param("id") int id);
    public Long getIdCard(@Param("id") int id);
    public Integer getId(@Param("name") String name);

}

 

测试类StudentTest

package com.core.test;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import com.core.common.SessionFactory;
import com.core.entity.Student;
import com.core.mapper.StudentMapper;

public class StudentTest {
    
    public static void main(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();
        }
        
    }
    
    
    //通过ID获取IdCard
    @Test
    public void TestGetIdCard(){
        SqlSession session =SessionFactory.getSqlSession();
        try {
          StudentMapper mapper = session.getMapper(StudentMapper.class);
          Long idCard = mapper.getIdCard(1);
        } finally {
          session.close();
        }
    }
    
    
    //通过name获取Id
    @Test
    public void TestGetIdByName(){
        SqlSession session =SessionFactory.getSqlSession();
        try {
          StudentMapper mapper = session.getMapper(StudentMapper.class);
          Integer id = mapper.getId("911911");
        } finally {
          session.close();
        }
    }
    

}

 

3.分析:

1.若StudentMapper.java和StudentMapper.xml中getIdCard的返回值类型为long类型,此时传递的参数id为1,查询idCard的记录为null,long 类型无法接受null值,所以会报异常,若均如上方的代码中改成包装类Long,则可以接受null值。

2.若StudentMapper.java和StudentMapper.xml中getId的返回值类型为int类型,此时传递的参数name为'911911',查询id的记录为null,int 类型无法接受null值,所以会报异常,若均如上方的代码中改成包装类Integer,则可以接受null值。

 

总结:在需要返回int,long等基础类型数据的情况下,尽量在mybatis的Mapper中用基础类型的包装类。

 

 PS:在Mapper.java中尽量用@Param这种参数传递方式和Mapper.xml对相对应。这样就不用在Mapper.xml中配置属性parameterType了