mybatis 多对多映射关系
2015-08-05 21:03
471 查看
xml映射文件:一个用户对应多个订单,一个订单对应多个订单明细,一个订单明细对应一个商品 可以推断出用户和商品是多对多的关系用户和商品的关系分析
可以发现一层套一层...
public List<User> findUserAndItemsResultMap();
测试:
可以发现一层套一层...
<!-- #######################################多对多的关系映射####################################################### --> <resultMap type="cn.itcast.domain.User" id="UserAndItemsResultMap"> <!-- 用户表信息 --> <id column="id" property="id"/> <result column="username" property="username"/> <result column="sex" property="sex"/> <result column="address" property="address"/> <!-- 订单表信息 一个用户多个订单--> <collection property="ordersList" ofType="cn.itcast.domain.Orders"> <id column="id" property="id"/> <result column="number" property="number"/> <result column="createtime" property="createtime"/> <result column="note" property="note"/> <result column="user_id" property="userId"/> <!-- 一个 订单多个明细 --> <collection property="orderdetails" ofType="cn.itcast.domain.Orderdetail" > <id column="orderdetail_id" property="id"/> <result column="items_num" property="itemsNum"/> <result column="items_id" property="itemsId"/> <result column="orders_id" property="ordersId"/> <!-- 一个订单一个商品 --> <association property="items" javaType="cn.itcast.domain.Items"> <id column="items_id" property="id"/> <result column="items_name" property="name"/> <result column="items_price" property="price"/> <result column="items_detail" property="detail"/> </association> </collection> </collection> </resultMap> <select id="findUserAndItemsResultMap" resultMap="UserAndItemsResultMap"> select orders.*, user.username,user.sex,user.address, orderdetail.items_id,orderdetail.items_num,orderdetail.orders_id,orderdetail.id orderdetail_id, items.name items_name,items.price items_price,items.detail items_detail from orders,user,orderdetail,items where orders.user_id=user.id and orders.id=orderdetail.orders_id and orderdetail.items_id=items.id </select>接口中定义方法
public List<User> findUserAndItemsResultMap();
测试:
@Test public void testfindUserAndItemsResultMap(){ SqlSession sqlSession = sqlSessionFactory.openSession(); OrdersMapperUser ordersMapperUser = sqlSession.getMapper(OrdersMapperUser.class); List<User> list = ordersMapperUser.findUserAndItemsResultMap(); System.out.println(list); }运行结果分析:
相关文章推荐
- LeetCode(12)Integer to Roman
- java中的包以及内部类的介绍
- Rescue
- Python文档中if __name__ == '__main__'
- Search in Rotated Sorted Array(leetcode)
- myeclipse中的SpringMVC项目创建(引入velocity框架)
- property 中的strong 与weak
- 我知道点redis-数据结构与对象(链表)
- 虚拟机字节码执行引擎
- HDOJ 5352 MZL's City 匈牙利匹配
- iOS开发---轮播图模块(连续循环滚动版)
- JAVA基础篇二(Java,C++中的面向对象)
- android学习连接网页
- 【ci框架】ci框架授课思路
- C/C++经典算法精华整理(3)-实现栈的数据结构
- TF-IDF自动提取关键词
- Struts1.x自定义转化器
- hdu5348 MZL's endless loop(欧拉回路)
- shell 中数学计算总结
- socket 实现多线程聊天