mybatis的resultMap自定义结果映射规则

574 篇文章 4 订阅
272 篇文章 1 订阅

第一步:
在这里插入图片描述
第二步:
在这里插入图片描述
第二种方法:

<!-- 自定义结果集,使用级联属性封装第2种方式-->
    <resultMap id="myMapJiLian" type="com.yunqing.mybatis.bean.User">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="age" property="age"/>
        <result column="did" property="dep.id"/>
        <result column="dep_name" property="dep.departmentName"/>
    </resultMap>

<!--使用级联属性-->
    <select id="getUserAndDepById" resultMap="myMapJiLian">
        SELECT u.id id,u.name name,u.age age,u.dep_id dep_id,dep.id did,dep.department dep_name FROM t_user u,t_dep dep WHERE
        u.dep_id=dep.id AND u.id=#{id}
    </select>

本文参考

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis 中,我们可以使用 ResultMap 标签来自定义查询结果的封装规则ResultMap 标签可以将查询结果集中的列与 Java 对象的属性进行映射,从而实现自定义的结果封装规则。 下面是一个简单的 ResultMap 的示例: ``` <resultMap id="userResultMap" type="User"> <id column="id" property="id"/> <result column="username" property="username"/> <result column="password" property="password"/> <result column="email" property="email"/> </resultMap> ``` 在这个示例中,我们定义了一个名为 userResultMapResultMap,它将查询结果集中的 id、username、password 和 email 列映射到 User 对象的 id、username、password 和 email 属性上。 在实际应用中,我们可以根据需要进行更加复杂的结果封装规则的定义,比如使用 association 和 collection 标签来定义关联对象和集合对象的映射关系等。例如: ``` <resultMap id="blogResultMap" type="Blog"> <id column="id" property="id"/> <result column="title" property="title"/> <result column="content" property="content"/> <association property="author" javaType="Author"> <id column="author_id" property="id"/> <result column="author_username" property="username"/> <result column="author_password" property="password"/> <result column="author_email" property="email"/> </association> <collection property="comments" ofType="Comment"> <id column="comment_id" property="id"/> <result column="comment_content" property="content"/> <result column="comment_author" property="author"/> </collection> </resultMap> ``` 在这个示例中,我们定义了一个名为 blogResultMapResultMap,它将查询结果集中的 id、title 和 content 列映射到 Blog 对象的 id、title 和 content 属性上;将查询结果集中的 author_id、author_username、author_password 和 author_email 列映射到 Blog 对象的 author 属性上;将查询结果集中的 comment_id、comment_content 和 comment_author 列映射到 Blog 对象的 comments 属性上。其中,author 属性是一个关联对象,comments 属性是一个集合对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值