目录
1. resultType结果类型, 指sql语句执行完毕后, 数据转为的java对象, java类型是任意的
4. resultMap : 结果映射, 指定列名和java对象的属性对应关系
MyBatis中SQL语句执行完毕, 对结果的处理
MyBatis执行了sql语句, 得到的java对象
1. resultType结果类型, 指sql语句执行完毕后, 数据转为的java对象, java类型是任意的
resultType结果类型的值
1. 类型的全限定名称
2. 类型的别名, 例如 java.lang.Integer别名是int, int类型的别名是_int
处理方式
1. mybatis执行sql语句, 然后mybatis调用类的无参数构造方法,创建对象。
2. mybatis把ResultSet指定列值付给返回值的同名属性。
<select id="selectMultiPosition" resultType="com.bjpowernode.domain.Student">
select id,name, email,age from student
</select>
对等的jdbc
ResultSet rs = executeQuery("select id,name, email,age from student" )
while(rs.next()){
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"))
}
看个例子
例1
/*
返回值为单个对象Student,
resultType="org.example.domain.Student"
*/
public Student selectStudentById(Integer id);
对应mapper文件
<!--
返回值默认赋值规则是, 将查询结果的列值赋予对应的对象的属性
-->
<select id="selectStudentById" resultType="org.example.domain.Student" >
select * from t_student where id=#{id}
</select>
例2
/*
返回值为List, List中是Student,
resultType="org.example.domain.Student"
*/
List<Student> selectMultiParam(@Param("myName") String name, @Param("myAge") Integer age);
对应的mapper文件
<!--
方法的返回值是List, 但是resultType的值是Student,
因为List是由一个个Student组成,
-->
<select id="selectMultiParam" resultType="org.example.domain.Student">
select * from t_student where name=#{myName} or age=#{myAge}
</select>
例3
/*
返回值是简单类型
resultType="int", 这里是MyBatis给的别名,
也可以使用类型的全限定名称, 如resultType="java.lang.Integer"
*/
Integer countStudent();
对应的mapper文件
<select id="countStudent" resultType="int">
select count(*) from t_student
</select>
2. 为返回结果resultType起别名
上面例3有MyBatis为简单类型起别名, 那么自定义类型可不可以使用别名
也就是说resultType="org.example.domain.Student"可不可以使用resultType="Student类别名"代替
答案是可以的
定义别名的语法为在主配置文件中
给出例子, 在主配置文件中配置如下
<!--
1. 直接起别名
可以为一个类型其别名, 方便使用
type : 自定义类型的全限定名
alias : 别名(短小的, 容易记忆的)
一个<typeAliases>中包含多个<typeAlias>标签, 可以定义多个别名
-->
<!--
2. 包内的索引类就是全限定名的别名, 不区分大小写
使用<package>, name是包名
注意package标签也是用在<typeAliases>中的
-->
<typeAliases>
<typeAlias type="org.example.domain.Student" alias="Student" />
<package name="org.example.domain"/>
</typeAliases>
public Student selectStudentById1(Integer id);
mapper文件如下
<select id="selectStudentById1" resultType="Student">
select * from t_student where id=#{id}
</select>
3. 将查询结果存入Map
/*
查询结果返回Map
查询的SQL语句为select id, name from t_student where id = #{id}
结果为{name=lisi, id=2}, 这个map中有两个键值对
如果语句为select id, name, email from t_student where id = #{id}
查询结果就为{name=lisi, id=2, email=124@123.com}
规则为
1. 列名为key, 列值为value
2. 查询结果只能是一行记录, 多行会报错
*/
Map<Object, Object> selectMapById(Integer id);
对应的mapper文件为
<!--返回map-->
<select id="selectMapById" resultType="java.util.HashMap">
select id, name from t_student where id = #{id}
</select>
4. resultMap : 结果映射, 指定列名和java对象的属性对应关系
前面说了, resultType根据查询结果的列名, 赋值给java对象中和列名同名的属性
如果现在我列名和属性名不相同, 就得使用这个
例如Student的属性为s_id, s_name
查询结果列名为id, name
这种时刻就要使用resultMap指定列名和java对象的属性对应关系
给出例子
/*
使用ResultMap定义列名和java对象的属性间的映射关系
*/
List<NewStu> selectAllStudents();
其中NewStu的属性是s_id, s_name, s_email, s_age
对应的mapper为
<!--
使用resultMap
1. 先定义resultMap(映射关系)
2. 在select标签中使用resultMap来引用定义好的
-->
<!--
定义resultMap
id : 自定义名称, 是这个映射关系的标识符
type : java类型的权限定名称
-->
<resultMap id="stuMap" type="org.example.domain.NewStu">
<!--id为主键, result是非主键-->
<id column="id" property="s_id"/>
<result column="name" property="s_name"/>
<result column="email" property="s_email"/>
<result column="age" property="s_age"/>
</resultMap>
<!--
定义完上面的resultMap后, 当select中使用resultMap="stuMap"后
就会将查询结果的id值付给NewStu对象的s_id属性...
-->
<select id="selectAllStudents" resultMap="stuMap">
select * from t_student
</select>
注意, ResultMap和ResultType在sql语句标签中二选一
除了ResultMap能解决java对象属性名和列名不一致的情况, 还有一种方法, 就是sql语句中的列别名
<!--
使用列别名的方式可以达到和resultMap一样的效果
但是缺点是不能重复使用
-->
<select id="selectAllStudents" resultType="org.example.domain.NewStu" >
select id as s_id, name as s_name, email as s_email, age as s_age from t_student
</select>