Mybatis中输出结果的封装
封装输出结果: Mybatis执行sql语句,得到ResultSet,转为Java对象。
一、resultType
resultType属性:在执行select时使用,作为标签是属性。
resultType:表示结果类型,mysql执行sql语句,得到java对象的类型。它的值有两种:
1、java类型的全限定名称(建议使用,可阅读性强)
A) resultType表示JAVA自定义对象
/**
* 查询学生
* */
Student selectStudentById(Integer id);
<select id="selectStudentById" parameterType="integer" resultType="com.itjuzi.entity.Student">
select id,name,email,age from Student where id = #{id}
</select>
这里使用全限定名称,表示的意思 mybatis执行sql,把ResultSet中的数据转为Student 类型的对象。
即Mybatis做一下操作:
1.调用com.itjuzi.entity.Student的无参构造方法,创建对象。
Student student = new Student(); //使用反射创建对象
2.同名的列赋值给同名的属性
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
...
3.得到Java对象,如果dao接口返回值是List集合,Mybatis会把student对象梵高List集合。
所以执行Student myStudent = mapper.selectStudentById(1001);得到数据库中的 id=1001 这行数据,
这行数据的行值赋给了myStudent对象的属性。能得到myStudent对象就相当于是 id=1001 这行数据。
B) resultType表示简单类型
dao方法
/**
* 学生表数据数
* */
int studentCount();
mapper文件
<!--学生表数据数 注意count() 间不要有空格-->
<select id="studentCount" resultType="java.lang.Integer">
select count(*) from Student
</select>
C) resultType表示一个map结构
/**
* 查询结果为Map
* */
Map<Object,Object> selectMap(@Param("studentId") String id);
<!--查询结果为Map
执行sql得到一个Map结构数据,mybatis执行sql,把resultSet转为Map
sql执行结果,列名做map作为key,列值作为value
sql执行得到的是一行记录,转为map结构是正确的。即
dao接口返回是一个map,sql语句最多能获取的是一天记录,多余一行则会错误
-->
<select id="selectMap" resultType="java.util.Map">
select id,name from Student where id = #{studentId}
</select>
2、使用Mybatis中的别名(自定义别名)
① mybatis提供的对java类型定义的名称;
② 自定义别名步骤:
a)在mybatis主配置文件中,用typeAliases标签声明别名;
<!--声明别名-->
<typeAliases>
<!--第一种语法格式
type:java类型的全限定名称(自定义类型)
alias:自定义别名
-->
<typeAlias type="com.itjuzi.entity.Student" alias="student"/>
</typeAliases>
优点: 可以自定义别名
缺点: 每个类型必须单独自定义
<!--声明别名-->
<typeAliases>
<!--第一种语法格式
type:java类型的全限定名称(自定义类型)
alias:自定义别名
-->
<!-- <typeAlias type="com.itjuzi.entity.Student" alias="student"/>-->
<!--第二种方式
name:包名,mybatis会把这个包中的所有类型作为别名(不用区分大小写)
-->
<package name="com.itjuzi.entity"/>
</typeAliases>
优点: 使用方便,一次给多个类定义别名
缺点: 别名不能自定义,必须是类名。别名冲突不唯一,不同包里会有同类名冲突。
b)在mapper文件中,resultType=“别名”;
<select id="selectStudentById" parameterType="integer" resultType="student">
select id,name,email,age from Student where id = #{id}
</select>
二、resultMap
resultMap: 结果映射,自定义列名和Java对象属性的关系, 常用在列名和属性名不同的情况。
用法:
1.先定义一个resultMap标签,指定列名和属性名称对应的关系;
2.在select标签使用resultMap属性,指定上面定义的resultMap的id;
<!--
定义resultMap
id:给resultMap映射关系起个名称,唯一值
type:java类型的全限定名称
-->
<resultMap id="customMap" type="com.itjuzi.vo.CustomObject">
<!--定义列名和属性名的对应-->
<!--主键类型使用id标签-->
<id column="id" property="cid"/>
<!--非主键类型使用result标签-->
<result column="name" property="cname"/>
<!--下面列名和属性名相同可以不用定义-->
<result column="email" property="email"/>
<result column="age" property="age"/>
</resultMap>
<!--使用resultMap属性,指定映射关系的id-->
<select id="selectResultMap" resultMap="customMap">
select id,name,email,age from student where id=#{id}
</select>
三、实体类属性名和列名不同的处理方式
1)使用resultMap(如上述)
2)使用列别名和resultType(as可以省略)
<select id="selectResultMap" resultType="com.itjuzi.vo.CustomObject">
select id as cid,name as cname,email,age from student where id=#{id}
</select>