六、封装 MyBatis 输出结果resultType、resultMap 以及 数据库表中列名和返回对象属性名不一致的2种解决方案(详解)

1.1 resultType

resultType: 执行 sql 得到 ResultSet 转换的类型,使用类型的完全限定名或别名。
注意:如果返回的是集合,那应该设置为集合包含的类型,而不是集合本身。resultType 和 resultMap,不能同时使用。
在这里插入图片描述

A、简单类型

接口方法:

int countStudent();

mapper 文件:

 <!--resultType简单类型-->
    <select id="countStudent" resultType="int">
        select count(*) from student
    </select>

或者:

 <!--resultType简单类型-->
    <select id="countStudent" resultType="java.lang.Integer">
        select count(*) from student
    </select>

注意:resultType结果类型的它的值

  1. 类型的全限定名称
  2. 类型的别名, 例如 java.lang.Integer别名是int

测试文件:

@Test
    public void testReturnInt(){
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class); // 这句代码可以自动创建dao接口的实现类对象
        int count = dao.countStudent();
        System.out.println("学生总人数:"+ count);
    }

在这里插入图片描述

B、 对象类型

接口方法:

public Student selectStudentById(@Param("studentId") Integer id);

mapper 文件:

<select id="selectStudentById" resultType="com.zep.domain.Student">
        select id,name,email,age from student where id=#{studentId}
</select>

测试文件:

@Test
    public void testSelectStudentById() {
    
    
        /**
         * 使用mybatis的动态代理机制,使用SqlSession.getMapper(dao接口)
         * getMapper能够获取dao接口对应的实现类对象。
         */
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class); // 这句代码可以自动创建dao接口的实现类对象
        //调用dao的方法,执行数据库的操作
        Student student = dao.selectStudentById(1001);
        System.out.println("学生=" + student);

    }

在这里插入图片描述
在这里插入图片描述
注意:Dao 接口方法返回是集合类型,需要指定集合中的类型,不是集合本身。
在这里插入图片描述
mybatis中也可以对我们自定义的类型起别名:
方法:
1)在mybatis主配置文件中定义,使<typeAlias>定义别名
在这里插入图片描述

<!--定义别名-->
    <typeAliases>
        <!--可以指定一个类型一个自定义别名
            type:自定义类型的全限定名称
            alis:别名(短小,容易记忆)
        -->
        <typeAlias type="com.zep.domain.Student" alias="stu"/>
        <typeAlias type="com.zep.vo.ViewStudent" alias="vstu"/>
    </typeAliases>

2)可以在resultType中使用自定义别名
接口方法:

public Student selectStudentById(@Param("studentId") Integer id);

mapper 文件:

<select id="selectStudentById" resultType="stu">
        select id,name,email,age from student where id=#{studentId}
</select>

测试文件:

@Test
    public void testSelectStudentById() {
    
    
        /**
         * 使用mybatis的动态代理机制,使用SqlSession.getMapper(dao接口)
         * getMapper能够获取dao接口对应的实现类对象。
         */
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class); // 这句代码可以自动创建dao接口的实现类对象
        //调用dao的方法,执行数据库的操作
        Student student = dao.selectStudentById(1001);
        System.out.println("学生=" + student);

    }

在这里插入图片描述
或者采用包扫描<package>:
1)在mybatis主配置文件中定义,使<package>定义别名
在这里插入图片描述

<typeAliases>
        <!--
            第一种方式:
            可以指定一个类型一个自定义别名
            type:自定义类型的全限定名称
            alis:别名(短小,容易记忆)
        -->
        <!--<typeAlias type="com.zep.domain.Student" alias="stu"/>
        <typeAlias type="com.zep.vo.ViewStudent" alias="vstu"/>-->
        <!--
        第二种方式
        <package> name是包名,这个包中的所有类,类名就是别名(类名不区分大小写)
        -->
        <package name="com.zep.domain"/>
        <package name="com.zep.vo"/>
    </typeAliases>

2)可以在resultType的值中可以省略前面在<package>标签name属性中已经写过的内容,只需要写类名即可,类名就是别名

mapper文件:

<select id="selectStudentById" resultType="Student">
        select id,name,email,age from student where id=#{studentId}
</select>

测试文件:

@Test
    public void testSelectStudentById() {
    
    
        /**
         * 使用mybatis的动态代理机制,使用SqlSession.getMapper(dao接口)
         * getMapper能够获取dao接口对应的实现类对象。
         */
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class); // 这句代码可以自动创建dao接口的实现类对象
        //调用dao的方法,执行数据库的操作
        Student student = dao.selectStudentById(1001);
        System.out.println("学生=" + student);

    }

在这里插入图片描述

C、 Map

sql 的查询结果作为 Map 的 key 和 value。推荐使用 Map<Object,Object>。

注意:Map 作为接口返回值,sql 语句的查询结果最多只能有一条记录。大于一条记录是错误。
在这里插入图片描述

接口方法:

//定义方法返回Map
    Map<Object,Object> selectMapById(Integer id);

mapper 文件:

    <!--
        返回Map
        1)列名是map的key,列值是map的value
        2)只能最多返回一行记录。多于一行会报错
    -->
 <select id="selectMapById" resultType="map">
        select id,name from student where id=#{stuid}
    </select>

或者:

<select id="selectMapById" resultType="java.util.HashMap">
        select id,name from student where id=#{stuid}
</select>

测试文件:

   // 返回Map
    @Test
    public void testSelectMap() {
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class); // 这句代码可以自动创建dao接口的实现类对象
        //调用dao的方法,执行数据库的操作
        Map<Object, Object> map = dao.selectMapById(1001);
        System.out.println("m=" + map);

    }

在这里插入图片描述

1.2 resultMap

resultMap 可以自定义 sql 的结果和 java 对象属性的映射关系。更灵活的把列值赋值给指定属性。
常用在列名和 java 对象属性名不一样的情况。

使用方式:

  1. 先定义 resultMap,指定列名和属性的对应关系。
  2. <select>中把 resultType 替换为 resultMap。

注意:
resultMap和resultType不要一起用,二选一

接口方法:

 /**
     * 使用resultMap定义映射关系
     */
    List<Student> selectAllStudents();

mapper文件:
在这里插入图片描述

 <!--使用resultMap
        1)先定义resultMap
        2)在select标签中,使用resultMap来引用1中定义的。

    -->
    <!--定义resultMap
        id:自定义名称,表示你定义的这个resultMap
        type:java类型的全限定名称
    -->
    <resultMap id="studentMap" type="com.zep.domain.Student">
        <!--定义列名和java属性的关系-->
        <!--主键列,使用id标签
            column:列名
            property:java类型的属性名
        -->
        <id column="id" property="id"/>
        <!--非主键列,使用result标签-->
        <result column="name" property="name" />
        <result column="email" property="email" />
        <result column="age" property="age" />
    </resultMap>
    <select id="selectAllStudents" resultMap="studentMap">
        select id,name,email,age from student
    </select>

测试文件:

/**/
    @Test
    public void testSelectAllStudents() {
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);

        List<Student> students = dao.selectAllStudents();
        for (Student student : students) {
    
    
            System.out.println("学生=" + student);
        }
        sqlSession.close();
    }

在这里插入图片描述
列名和属性名不同的第一种解决方案:
在这里插入图片描述
在这里插入图片描述

在domain文件夹下新建MyStudent.java:

package com.zep.domain;

public class MyStudent {
    
    
    private Integer stuid;
    private String stuname;
    private String stuemail;
    private Integer stuage;

    @Override
    public String toString() {
    
    
        return "MyStudent{" +
                "stuid=" + stuid +
                ", stuname='" + stuname + '\'' +
                ", stuemail='" + stuemail + '\'' +
                ", stuage=" + stuage +
                '}';
    }

    public Integer getStuid() {
    
    
        return stuid;
    }

    public void setStuid(Integer stuid) {
    
    
        this.stuid = stuid;
    }

    public String getStuname() {
    
    
        return stuname;
    }

    public void setStuname(String stuname) {
    
    
        this.stuname = stuname;
    }

    public String getStuemail() {
    
    
        return stuemail;
    }

    public void setStuemail(String stuemail) {
    
    
        this.stuemail = stuemail;
    }

    public Integer getStuage() {
    
    
        return stuage;
    }

    public void setStuage(Integer stuage) {
    
    
        this.stuage = stuage;
    }
}

接口方法:

List<MyStudent> selectMyStudent();

mapper文件:

 <resultMap id="myStudentMap" type="com.zep.domain.MyStudent">
        <id column="id" property="stuid"/>
        <!--非主键列,使用result标签-->
        <result column="name" property="stuname" />
        <result column="email" property="stuemail" />
        <result column="age" property="stuage" />
    </resultMap>
    <!--列名和属性名不一样-->
    <select id="selectMyStudent" resultMap="myStudentMap">
        select id,name,email,age from student
    </select>

测试文件:

 /**/
    @Test
    public void testSelectMyStudent() {
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        List<MyStudent> students = dao.selectMyStudent();
        for (MyStudent student : students) {
    
    
            System.out.println("学生=" + student);
        }
        sqlSession.close();
    }

在这里插入图片描述
列名和属性名不同的第二种解决方案:
采用列别名的方式,给数据库的列名起一个别名,让这个别名和属性名相同即可!
在这里插入图片描述
接口方法:

 List<MyStudent> selectDiffColProperty();

mapper文件:

<!--列名和属性名不一样:第二种方式
        resultType的默认原则是 同名的列的值赋值给同名的属性,
        所以使用列别名(java对象的属性名)即可解决
    -->
    <select id="selectDiffColProperty" resultType="com.zep.domain.MyStudent">
        select id as stuid,name as stuname,email as stuemail ,age as stuage from student
    </select>

测试文件:

@Test
    public void testSelectDiffColProperty() {
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        List<MyStudent> students = dao.selectDiffColProperty();
        for (MyStudent student : students) {
    
    
            System.out.println("###学生=" + student);
        }
        sqlSession.close();
    }

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44827418/article/details/111468931