Mybatis 复杂对象映射配置ResultMap的association

association: 映射到POJO的某个复杂类型属性,比如订单order对象里面包含 user对象

代码:

VideoMapper.java

public interface VideoOrderMapper {
    /**
     * 查询全部订单,关联用户
     * @return
     */
    List<VideoOrder> queryVideoOrderList();
}

VideoMapper.xml

<mapper namespace="work.yspan.online_class.dao.VideoOrderMapper">

    <resultMap id="VideoOrderResultMap" type="VideoOrder">
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="out_trade_no" property="outTradeNo"/>
        <result column="create_time" property="createTime"/>
        <result column="total_fee" property="totalFee"/>
        <result column="video_id" property="videoId"/>
        <result column="video_title" property="videoTitle"/>
        <result column="video_img" property="videoImg"/>
		<!--
 		association 配置属性一对一
 		property 对应videoOrder⾥⾯的user属性名
 		javaType 这个属性的类型
 		-->
        <association property="user" javaType="User">
            <id column="user_id" property="id"/>
            <result column="name" property="name"/>
            <result column="head_img" property="headImg"/>
            <result column="phone" property="phone"/>
            <result column="create_timeu" property="createTime"/>
        </association>
    </resultMap>
	<!--一对一管理查询订单, 订单内部包含用户属性-->
    <select id="queryVideoOrderList" resultMap="VideoOrderResultMap">
        select
        o.id ,
        o.user_id,
        o.out_trade_no,
        o.create_time,
        o.total_fee,
        o.video_id,
        o.video_title,
        o.video_img,
        u.name,
        u.head_img,
        u.phone,
        u.create_time create_timeu
        from video_order o left join user u on o.user_id = u.id
    </select>

</mapper>

运行代码:

public class SqlSessionDemo {
    public static void main(String [] args) throws IOException {

        //读取配置文件
        String resouce="config/mybatis-config.xml";
        InputStream inputStream= Resources.getResourceAsStream(resouce);

        //构建Session工厂
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        //获取Session
        try(SqlSession sqlSession=sqlSessionFactory.openSession()){
            VideoOrderMapper videoOrderMapper=sqlSession.getMapper(VideoOrderMapper.class);
 			/**
             * resultMap association关联查询
             */
            List<VideoOrder> list=videoOrderMapper.queryVideoOrderList();
            System.out.println(list.toString());

效果截图:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值