下面要给大家带来的是和mybatis多对多相关的内容,那么你对于mybatis多对多映射关系有多了解呢?下面一起来看看详细介绍吧!
一、Mybatis多对多映射关系
最首先的就是要创建数据库
user表属性和orders表属性
之后,进行主外键关联
之后创建创建ordersdemo表
之后,进行主外键关联
接着创建item表
这样的话整个数据库就已经设计完成了,下面来进行测试
需求:
查询用户以及购买商品的信息,因为用户没有和商品没有直接的关系,关系是建立在orders以及orderdemo当中
创建bean类User
package com.hp.bean; import java.util.List; public class User { //用户表 private Integer id; private String name; private int ses; //商品订单 private List < Orders > orders; public List < Orders > getOrders() { return orders; } public void setOrders(List < Orders > orders) { this.orders = orders; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSes() { return ses; } public void setSes(int ses) { this.ses = ses; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", ses=" + ses + "]"; } }
Orders
package com.hp.bean; import java.util.List; public class Orders { //商品订单表 private Integer o_id; private String o_name; private Integer u_id; //用户表 private User user; //订单明细表 private List < OrdersDemo > ordersDemos; public List < OrdersDemo > getOrdersDemos() { return ordersDemos; } public void setOrdersDemos(List < OrdersDemo > ordersDemos) { this.ordersDemos = ordersDemos; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public Integer getO_id() { return o_id; } public void setO_id(Integer o_id) { this.o_id = o_id; } public String getO_name() { return o_name; } public void setO_name(String o_name) { this.o_name = o_name; } public Integer getU_id() { return u_id; } public void setU_id(Integer u_id) { this.u_id = u_id; } @Override public String toString() { return "Orders [o_id=" + o_id + ", o_name=" + o_name + ", u_id=" + u_id + ", user=" + user + ", ordersDemos=" + ordersDemos + "]"; } }
OrdersDemo
package com.hp.bean; public class OrdersDemo { private Integer d_id; private Integer d_num; private double d_price; private Integer orders_id; //商品信息 private Items items; public Items getItems() { return items; } public void setItems(Items items) { this.items = items; } public Integer getD_id() { return d_id; } public void setD_id(Integer d_id) { this.d_id = d_id; } public Integer getD_num() { return d_num; } public void setD_num(Integer d_num) { this.d_num = d_num; } public double getD_price() { return d_price; } public void setD_price(double d_price) { this.d_price = d_price; } public Integer getOrders_id() { return orders_id; } public void setOrders_id(Integer orders_id) { this.orders_id = orders_id; } @Override public String toString() { return "OrdersDemo [d_id=" + d_id + ", d_num=" + d_num + ", d_price=" + d_price + ", orders_id=" + orders_id + "]"; } }
Items
package com.hp.bean; public class Items { //商品信息表 private Integer i_id; private String i_text; private double i_price; private String i_desc; public Integer getI_id() { return i_id; } public void setI_id(Integer i_id) { this.i_id = i_id; } public String getI_text() { return i_text; } public void setI_text(String i_text) { this.i_text = i_text; } public double getI_price() { return i_price; } public void setI_price(double i_price) { this.i_price = i_price; } public String getI_desc() { return i_desc; } public void setI_desc(String i_desc) { this.i_desc = i_desc; } }
mapper.xml配置
<!-- 查询用户和商品信息的resultmap --> <resultMap type="com.hp.bean.User" id="UserItemFinadAllResultMap"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="ses" property="ses"/> <!-- 一个用户对应有多个订单信息 --> <collection property="orders" ofType="com.hp.bean.Orders"> <id column="o_id" property="o_id"/> <result column="o_name" property="o_name"/> <result column="u_id" property="u_id"/> <!-- 一个商品订单有多个商品信息 --> <collection property="ordersDemos" ofType="com.hp.bean.OrdersDemo"> <id column="d_id" property="d_id"/> <result column="d_num" property="d_num"/> <result column="d_price" property="d_price"/> <result column="orders_id" property="orders_id"/> <!-- 一个订单详细 有一个商品信息 --> <association property="items" javaType="com.hp.bean.Items"> <id column="i_id" property="i_id"/> <result column="i_text" property="i_text" /> <result column="i_price" property="i_price" /> <result column="i_desc" property="i_desc" /> </association> </collection> </collection> </resultMap> <!--查询用户 和商品信息 多对多查询 由于用户 和商品信息没有直接关系所以 --> <select id="UserItemFinadAll" resultMap="UserItemFinadAllResultMap"> select id,name,ses,o_id,o_name,u_id,d_id,d_num,d_price,orders_id,i_text,i_desc,i_price,i_id from orders,USER,ordersdemo,item WHERE orders.u_id=user.id AND ordersdemo.orders_id=orders.o_id AND ordersdemo.item_id=item.i_id </select>
mapper
//查询用户和商品信息 public List < User > UserItemFinadAll();
最终的测试结果如下:
下面来一个小总结:
使用ResultMap是针对于对查询结果映射有特殊的要求
例:
list里面包括list集合
使用association和collection来完成多对多的查询,使用场景对结果有特殊的要求。
使用ReslutType将结果按照sql列名pojo属性名一致映射到pojo中,使用场景清单明细。
以上就是对于mybatis多对多映射关系的详细讲解了,你都了解了吧!你想成为一名java架构师吗?更多关于java架构师的知识可以继续关注本站了解哦。
推荐阅读: