MyBatis学习笔记(4)-resultMap多对多映射使用总结

SQL语句

  • 查询的主表:用户表user
  • 关联的关联表:由于用户和商品没有直接关联,通过订单和订单明细进行关联,所以关联表是orders、orderdetail、items

SELECT语句如下

SELECT
        orders.*,
        USER.username,
        USER.sex,
        USER.address,
        orderdetail.id orderdetail_id,
        orderdetail.items_id,
        orderdetail.items_num,
        orderdetail.orders_id,
        items.name items_name,
        items.detail items_detail,
        items.price items_price
        FROM
        orders,
        USER,
        orderdetail,
        items
        WHERE orders.user_id = user.id AND orderdetail.orders_id=orders.id AND orderdetail.items_id = items.id

映射思路

  • 将用户信息映射到user中。
  • User类中添加订单列表属性List<Orders> orderslist,将用户创建的订单映射到orderslist
  • Orders类中添加订单明细列表属性List<OrderDetail>orderdetials,将订单的明细映射到orderdetails
  • OrderDetail类中添加Items属性,将订单明细所对应的商品映射到Items

使用resultMap具体实现多对多映射

OrdersCustomMapper.xml

定义resultMap

 <!-- 查询用户及购买的商品 -->
    <resultMap type="com.crow.po.User" id="UserAndItemsResultMap">
        <!-- 用户信息 -->
        <id column="user_id" property="id"/>
        <result column="username" property="username"/>
        <result column="sex" property="sex"/>
        <result column="address" property="address"/>

        <!-- 订单信息 一个用户对应多个订单,使用collection映射 -->
        <collection property="ordersList" ofType="com.crow.po.Orders">
            <id column="id" property="id"/>
            <result column="user_id" property="userId"/>
            <result column="number" property="number"/>
            <result column="createtime" property="createtime"/>
            <result column="note" property="note"/>

            <!-- 订单明细 一个订单包括 多个明细 collection里嵌套collection -->
            <collection property="orderDetails" ofType="com.crow.po.OrderDetail">
                <id column="orderdetail_id" property="id"/>
                <result column="items_id" property="itemsId"/>
                <result column="items_num" property="itemsNum"/>
                <result column="orders_id" property="ordersId"/>

                <!-- 商品信息 一个订单明细对应一个商品 -->
                <association property="items" javaType="com.crow.po.Items">
                    <id column="items_id" property="id"/>
                    <result column="items_name" property="name"/>
                    <result column="items_detail" property="detail"/>
                    <result column="items_price" property="price"/>
                </association>

            </collection>

        </collection>

    </resultMap>

定义statement

<!-- 查询用户及购买的商品信息,使用resultmap -->
    <select id="findUserAndItemsResultMap" resultMap="UserAndItemsResultMap">
        SELECT
        orders.*,
        USER.username,
        USER.sex,
        USER.address,
        orderdetail.id orderdetail_id,
        orderdetail.items_id,
        orderdetail.items_num,
        orderdetail.orders_id,
        items.name items_name,
        items.detail items_detail,
        items.price items_price
        FROM
        orders,
        USER,
        orderdetail,
        items
        WHERE orders.user_id = user.id AND orderdetail.orders_id=orders.id AND orderdetail.items_id = items.id
    </select>

OrderCustomMapper.java

//查询用户购买商品信息
public List<User>  findUserAndItemsResultMap()throws Exception;

测试代码

@Before
    public void setUp() throws Exception {
        // 创建sqlSessionFactory

        // mybatis配置文件
        String resource = "SqlMapConfig.xml";
        // 得到配置文件流
        InputStream inputStream = Resources.getResourceAsStream(resource);

        // 创建会话工厂,传入mybatis的配置文件信息
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }
    
@Test
    public void testfindUserAndItemsResultMap() throws Exception{
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrdersCustomMapper ordersCustomMapper = sqlSession.getMapper(OrdersCustomMapper.class);
        List<User> userList = ordersCustomMapper.findUserAndItemsResultMap();
        System.out.println(userList);
        sqlSession.close();
    }

resultMap总结

  • resultType 作用:将查询结果按照sql列名pojo属性名一致性映射到pojo中。 应用场合:用于展示明细记录,比如用户购买商品明细,将关联查询信息全部展示在页面时,此时可直接使用resultType将每一条记录映射到pojo中,在前端页面遍历list(list中是pojo)即可。
  • resultMap:使用association和collection完成一对一、一对多和多对多高级映射(对结果有特殊的映射要求)。
  • association: 作用:将关联查询信息映射到一个pojo对象中。 场合:为了方便查询关联信息,可以使用association将关联订单信息映射到用户对象的pojo属性中,比如:查询订单及关联用户信息。使用resultType无法将查询结果映射到pojo对象的pojo属性中。
  • collection 作用:将关联查询信息映射到一个list集合中。 场合:为了方便查询遍历关联信息,可以使用collection将关联信息映射到list集合中,比如:查询用户权限范围模块及模块下的菜单,可使用collection将模块映射到模块list中,将菜单列表映射到模块对象的菜单list属性中,这样的作的目的也是方便对查询结果集进行遍历查询。如果使用resultType无法将查询结果映射到list集合中。
发布了248 篇原创文章 · 获赞 32 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/tianshan2010/article/details/103987529