天天看点

【MyBatis】MyBatis 中映射文件执行 select 输出结果剖析(resultType、resultMap、自定义别名)

文章目录

      • MyBatis的输出结果
        • 1)resultType
          • 返回对象类型
          • 返回简单类型
          • 返回Map类型
        • 2) 定义自定义类型的别名
        • 3)resultMap

MyBatis的输出结果

MyBatis执行了sql语句,得到的 java 对象。

1)resultType

resultType结果类型:指执行 sql 得到 ResultSet 转换的类型,这个类型可以是任意的(不一定非要是实体类),使用类型的完全限定名(java.lang.Integer)或别名(int)。 注意:如果返回的是集合,那应该设置为集合包含的类型,而不是集合本身。

返回对象类型
处理方式:
	1. mybatis执行sql语句, 然后mybatis调用类的无参数构造方法,创建对象。
	2. mybatis把ResultSet指定列值付给同名的属性。
           
mapper.xml:
	<select id="selectMultiPosition" resultType="com.kaho.domain.Student">
      	select id,name, email,age from student
    </select>

	MyBatis底层对等的jdbc:
	  ResultSet rs = executeQuery(" select id,name,email,age from student" )
	  while(rs.next()){
          Student student = new Student();
          //使用构造方法创建对象。调用 setXXX 给属性赋值。
		  student.setId(rs.getInt("id"));
		  student.setName(rs.getString("name"));
		  student.setEmail(rs.getstring("email"));
		  student.setAge(rs.getInt("age"));
		  ...
	  }
           

Student student = new Student();

调用列名对应的 set 方法

id 列 — setId()

name 列 — setName()

sql 语句列 java 对象方法
id setId( rs.getInt(“id”) )
name setName( rs.getString(“name”) )
email setEmail( rs.getString(“email”) )
age setAge( rs.getInt(“age”) )
注意:Dao 接口方法返回是集合类型,需要指定集合中的类型,不是集合本身。
返回简单类型

一般用于执行 sql 语句的返回结果是一行一列的数据。

例:接口方法:

mapper.xml:

<select id="countStudent" resultType="int">
 	select count(*) from student
</select>
           

测试方法:

@Test
public void testRetunInt(){
     int count = studentDao.countStudent();
     System.out.println("学生总人数:"+ count);
}
           
返回Map类型

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

注意:Map 作为接口返回值,sql 语句的查询结果最多只能有一行记录。大于一条记录是错误。 如果要用Map返回多条记录,需要把它装载List集合中,也就是返回结果 List<Map<Object,Object>>,然后resultType="java.util.HashMap"不变,而dao接口中的方法返回值要改成List<Map<Object,Object>>

接口方法:

mapper.xml:

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

测试方法:

@Test
public void testReturnMap(){
     Map<Object,Object> retMap = studentDao.selectReturnMap(1002);
     System.out.println("查询结果是 Map:"+retMap);
}
           

2) 定义自定义类型的别名

​ 1)在mybatis主配置文件中定义,使用 **<typeAlias>**定义别名

​ 2)可以在resultType中使用自定义别名

建议少用别名,多用全限定名称,这样更安全,更有可读性
<settings>
		......
	</settings>   

<!--建议写在settings下面-->
	<typeAliases>
        <!--
			方式一:
			可以指定一个类型一个自定义别名
            type:自定义类型的全限定名称
            alias:别名(短小易记)
        -->
        <typeAlias type="com.kaho.domain.Student" alias="stu" />
        <typeAlias type="com.kaho.vo.ViewStudent" alias="vstu" />
        
        <!--
			方式二:(推荐使用)
			<package> name是包名,这个包中的所有类的类名就是其对应的别名(类名不区分大小写)
		-->
        <package name="com.kaho.domain" />
        <package name="com.kaho.vo" />
    </typeAliases>
           

定义完成后就可以在resultType中使用别名替代全限定名称。

3)resultMap

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

使用方式:
1.先定义 resultMap,自定义列值赋值给哪个属性,指定列名和属性的对应关系。
2.在<select>中把 resultType 替换为 resultMap。
           
<!--定义resultMap(这个resultMap是可复用的,只需要引用其id就行)
	id:自定义名称,表示你定义的这个resultMap
	type:java类型的全限定名称
-->
<resultMap id="studentMap" type="com.kaho.domain.Student">
	<!--列名和java属性的关系-->
    <!--主键列,使用id标签
		column:列名
		property:java类型的属性名
	-->
    <id column="id" property="id" />
    <!--非主键列-->
    <result column="name" property="name" />
    <result column="email" property="email" />
    <result column="age" property="age" />
</resultMap>
<!--如果引用resultMap的位置和resultMap的定义 在同一个mapper.xml,直接使用resultMap的id;如果不在同一个mapper.xml,要在resultMap的id前面加上"namespace."-->
<select id="selectAllStudents" resultMap="studentMap">
	select id,name,email,age from student
</select>
           

注意:

resultMap和resultType不能同时使用,二选一
           

如何在列名和java对象属性名不一致的情况下使用resultType?

给列起别名,别名和java对象的属性名取一致。

MyStudent类:

package com.kaho.domain;

public class QueryParam {
    private int stuId;
    private String stuName;
    private String stuEmail;
    private int stuAge;
    //get、set、toString方法
}
           

mapper.xml:

<select id="selectDiffColProperty" resultType="com.kaho.domain.MyStudent">
	select id as stuId,name as stuName,email as stuEmail,age as stuAge from student
</select>
           

继续阅读