Mybatis使用resultType自动映射、使用别名和ResultMap标签进行结果映射

今天继续完善一下mybatis系列相关博客,以便查阅,同时也希望能帮助到有需要的小伙伴,各位看到此博客的小伙伴,如有不对的地方请及时通过私信我或者评论此博客的方式指出,以免误人子弟。多谢!

上一篇通过指定resultType为map将所有的列映射到HashMap 的键上,虽然在大部分情况下都够用,但是 HashMap 并不是一个很好的领域模型。通常我们使用javabean作为领域模型。MyBatis 对两者都提供了支持。

比如之前我们的测试例子中:

<select id="selectByMultiParam" resultType="com.example.mybatis.domain.User">
     select *from t_user where id = #{id} and name = #{name}
</select>

我们使用resultType指定一个返回对象的类型,在这些情况下,MyBatis 会在幕后自动创建一个 ResultMap,再根据属性名来映射列到 JavaBean 的属性上。如果列名和属性名不能匹配上,可以在 SELECT 语句中设置列别名来完成匹配。

resultType自动映射

比如,我们在查询用户信息的时候想把用户wife的名字查询出来,或者把wife的详细信息都查询出来,可以像下面这样:

@Data
public class UserWifeDto {
    private Integer id;
    private String name;
    private Integer age;
    private String wifeName;
}

Mapper接口中:

UserWifeDto testAutomappering(int id);

Mapper.xml中:

<select id="testAutomappering" resultType="com.example.mybatis.dto.UserWifeDto">
    select u.*, w.wife_name from t_user u
       left join t_wife w on u.id = w.user_id
       where u.id = #{id}
</select>

使用别名进行结果映射

比如上面我们UserWifeDto中的wifeName为wfName,那属性名和列名就不能匹配上,可以在查询时使用别名的方式完成匹配,如:

select u.*,w.wife_name as wfName from t_user u 
   left join t_wife w on u.id = w.user_id 
   where u.id = #{id}

这只是依赖了wife对象的一个属性,如果UserWifeDt中依赖wife对象 private Wife wife; 这时就没法自动映射wife属性了 结果如下:

这时候就可以使用强大的ResultMap标签了。看下官方文档中对此标签的表述:

resultMap 元素是 MyBatis 中最重要最强大的元素。它可以让你从 90% 的 JDBC ResultSets 数据提取代码中解放出来,并在一些情形下允许你进行一些 JDBC 不支持的操作。实际上,在为一些比如连接的复杂语句编写映射代码的时候,一份 resultMap 能够代替实现同等功能的数千行代码。ResultMap 的设计思想是,对简单的语句做到零配置,对于复杂一点的语句,只需要描述语句之间的关系就行了。

ResultMap标签进行结果映射

使用resultMap级联属性封装结果集

@Data
public class UserWifeDto {
    private Integer id;
    private String name;
    private Integer age;
    private String wifeName;
    private Wife wife;
}

Mapper接口中:

UserWifeDto testCascadeResultMap(int id);

Mapper.xml中:

<select id="testCascadeResultMap" resultMap="cascadeResultMap">
    select u.*, w.* from t_user u
        left join t_wife w on u.id = w.user_id
        where u.id = #{id}
</select>

<resultMap id="cascadeResultMap" type="com.example.mybatis.dto.UserWifeDto">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="sex" column="sex"/>
    <result property="age" column="age"/>
    <result property="province" column="province"/>
    <result property="city" column="city"/>
    <result property="createdTime" column="created_time"/>
    <result property="createdBy" column="created_by"/>
    <result property="updatedTime" column="updated_time"/>
    <result property="updatedBy" column="updated_by"/>
    <result property="wife.wifeId" column="wife_id"/>
    <result property="wife.userId" column="user_id"/>
    <result property="wife.wifeName" column="wife_name"/>
</resultMap>

执行结果如下:

resultMap中使用association关联查询(一对一)

springboot整合mybatis使用association做关联查询 一对一查询

resultMap中使用collection关联查询(一对多)

springboot整合mybatis使用collection查询 一对多 多对一 多对多查询

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis中,ResultTypeResultMap都是用于指定查询结果的映射方式,但它们的使用方式和作用有所不同。 1. ResultType ResultType是指定查询结果的类型,通常情况下可以指定为JavaBean的全限定名,或者是Java基本数据类型的别名Mybatis会根据查询结果的列名和JavaBean的属性名进行自动映射,将查询结果封装为JavaBean对象。 使用方式如下: ```xml <select id="selectUser" resultType="com.example.User"> select id, name, age from user where id=#{id} </select> ``` 在上述例子中,查询结果会被自动映射为com.example.User对象。 2. ResultMap ResultMap是指定查询结果的映射规则,可以自定义映射规则,包括列名和JavaBean属性名的对应关系,以及一些高级映射规则,如关联查询、嵌套查询等。 使用方式如下: ```xml <resultMap id="userMap" type="com.example.User"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="age" property="age"/> </resultMap> <select id="selectUser" resultMap="userMap"> select id, name, age from user where id=#{id} </select> ``` 在上述例子中,自定义了一个id为userMapResultMap,指定了列名和JavaBean属性名的对应关系,然后在select语句中使用了这个ResultMap。查询结果会按照ResultMap的规则进行映射,将查询结果封装为com.example.User对象。 总之,ResultTypeResultMap都是用于指定查询结果的映射方式,ResultType适用于简单的映射ResultMap适用于复杂的映射。开发人员可以根据具体情况选择合适的方式来进行结果映射
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值