Mybatis
1. MyBatis 输出结果封装
1.1 resultType 配置结果类型
resultType
属性可以指定结果集的类型,它支持基本类型和实体类类型。它和 parameterType
一样,如果注册过类型别名的,可以直接使用别名。没有注册过的必须使用全限定类名。
1.1.1 结果类型为基本类型
(1)持久层接口
/**
* 查询总记录条数
* @return
*/
int findTotal();
(2)映射配置文件
<!-- 查询总记录条数 -->
<select id="findTotal" resultType="int">
select count(*) from user;
</select>
1.1.2 结果类型为实体类
(1)持久层接口
/**
* 查询所有用户
* @return
*/
List<User> findAll();
(2)映射配置文件
<!-- 配置查询所有操作 -->
<select id="findAll" resultType="com.spg.entity.User">
select * from user
</select>
1.1.3 其他情况
(1)修改实体类
package com.spg.entity;
import java.util.Date;
public class User {
private Integer userId;
private String userName;
private String userAddress;
private String userSex;
private Date userBirthday;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserAddress() {
return userAddress;
}
public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}
public String getUserSex() {
return userSex;
}
public void setUserSex(String userSex) {
this.userSex = userSex;
}
public Date getUserBirthday() {
return userBirthday;
}
public void setUserBirthday(Date userBirthday) {
this.userBirthday = userBirthday;
}
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", userName='" + userName + '\'' +
", userAddress='" + userAddress + '\'' +
", userSex='" + userSex + '\'' +
", userBirthday=" + userBirthday +
'}';
}
}
(2) 在持久层接口中添加方法
/**
* 查询所有用户
* @return
*/
List<User> findAll();
(3)在用户的映射配置文件中配置
<!-- 配置查询所有操作 -->
<select id="findAll" resultType="com.spg.entity.User">
select * from user
</select>
(4)测试查询结果
@Test
public void testFindAll() {
List<User> users = userDao.findAll();
for(User user : users) {
System.out.println(user);
}
}
运行结果:
(5)修改映射配置
使用别名查询:
<!-- 配置查询所有操作 -->
<select id="findAll" resultType="com.spg.entity.User">
select id as userId,username as userName,birthday as userBirthday,
sex as userSex,address as userAddress from user
</select>
运行结果:
1.2 resultMap 结果类型
resultMap
标签可以建立查询的列名和实体类的属性名称不一致时建立对应关系。从而实现封装。
在 select
标签中使用 resultMap
属性指定引用即可。同时 resultMap
可以实现将查询结果映射为复杂类型的 pojo
,比如在查询结果映射对象中包括 pojo
和 list
实现一对一查询和一对多查询。
1.2.1 定义 resultMap
<!-- 配置查询结果的列名和实体类的属性名的对应关系
type 属性:指定实体类的全限定类名
id 属性:给定一个唯一标识,是给查询 select 标签引用用的。
-->
<resultMap id="userMap" type="com.spg.entity.User">
<!--
id 标签:用于指定主键字段
result 标签:用于指定非主键字段
column 属性:用于指定数据库列名
property 属性:用于指定实体类属性名称
-->
<!-- 主键字段的对应 -->
<id property="userId" column="id"/>
<!--非主键字段的对应-->
<result property="userName" column="userName"/>
<result property="userSex" column="sex"/>
<result property="userBirthday" column="birthday"/>
<result property="userAddress" column="address"/>
</resultMap>
1.2.2 在用户的映射配置文件中配置
<!-- 配置查询所有操作 -->
<select id="findAll" resultMap="userMap">
select * from user
</select>
1.2.3 测试查询结果
@Test
public void testFindAll() {
List<User> users = userDao.findAll();
for(User user : users) {
System.out.println(user);
}
}
运行结果: