Mybatis 的输出结果封装

resultType 配置结果类型

resultType 属性可以指定结果集的类型,它支持基本类型和实体类类型。
我们在前面的 CRUD 案例中已经对此属性进行过应用了。
需要注意的是,它和 parameterType 一样,如果注册过类型别名的,可以直接使用别名。没有注册过的必须使用全限定类名。例如:我们的实体类此时必须是全限定类名
同时,当是实体类名称时,还有一个要求,实体类中的属性名称必须和查询语句中的列名保持一致,否则无法实现封装。

基本类型示例

1 Dao 接口

/**
* 查询总记录条数
* @return
*/
int findTotal();

2 映射配置

<!-- 查询总记录条数 -->
<select id="findTotal" resultType="int">
select count(*) from user;
</select>

实体类类型示例

1 Dao 接口

/**
* 查询所有用户
* @return
*/
List<User> findAll();

2 映射配置

<!-- 配置查询所有操作 -->
<select id="findAll" resultType="com.sunyan.domain.User">
select * from user
</select>

特殊情况示例

1 修改实体类
实体类代码如下: (此时的实体类属性和数据库表的列名已经不一致了)

package com.sunyan.domain;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {

    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 Dao 接口

/**
* 查询所有用户
* @return
*/
List<User> findAll();

3 映射配置

<!-- 配置查询所有操作 -->
<select id="findAll" resultType="com.sunyan.domain.User">
select * from user
</select>

4 测试查询结果

    @Test
    public void testFindAll() {
        //5.执行查询所有方法
        List<User> users = userDao.findAll();
        for (User user : users) {
            System.out.println(user);
        }
    }

测试结果只有名称有值
为什么名称会有值呢?
因为: mysql 在 windows 系统中不区分大小写!

5 修改映射配置
使用别名查询

<!-- 配置查询所有操作 -->
    <select id="findAll" resultType="com.sunyan.domain.User">
         select id as userId,username as userName, address as userAddress, sex as userSex, birthday as userBirthday from user;
    </select>

运行结果:查询到所有用户
思考:
如果我们的查询很多,都使用别名的话写起来岂不是很麻烦,有没有别的解决办法呢?
即下文介绍。

resultMap 结果类型

resultMap 标签可以建立查询的列名和实体类的属性名称不一致时建立对应关系。从而实现封装。
在 select 标签中使用 resultMap 属性指定引用即可。同时 resultMap 可以实现将查询结果映射为复杂类型的 pojo,
比如在查询结果映射对象中包括 pojo 和 list 实现一对一查询和一对多查询。

1 定义 resultMap

    <resultMap id="userMap" type="com.sunyan.domain.User">
        <!--主键字段的对应-->
        <id property="userId" column="id"></id>
        <!--非主键字段的对应-->
        <result property="userName" column="username"></result>
        <result property="userAddress" column="address"></result>
        <result property="userSex" column="sex"></result>
        <result property="userBirthday" column="birthday"></result>
    </resultMap>
id 标签:用于指定主键字段
result 标签:用于指定非主键字段
column 属性:用于指定数据库列名
property 属性:用于指定实体类属性名称

2 映射配置

<!-- 配置查询所有操作 -->
<select id="findAll" resultMap="userMap">
select * from user
</select>

3 测试结果

@Test
public void testFindAll() {
    List<User> users = userDao.findAll();
    for(User user : users) {
      System.out.println(user);
    }
}
追求执行效率,选择使用别名查询或实体类属性和数据库表的列名对应
追求开发效率,选择resultMap这种方式或实体类属性和数据库表的列名对应

猜你喜欢

转载自www.cnblogs.com/sunyanblog/p/12404464.html