MyBatis关联查询_一对一查询_一对多查询_多对多查询

准备案例数据模型

  • 用户表
  • 订单表
  • 订单详情表
  • 商品表
    对应的实体类如下;
    用户表:
public class Users implements Serializable{
	
	private Integer id;
	private String username;
	private Date birthday;
	private String address;
	//用户对象的订单列表属性中
	private List<Orders> orders;
}

商品表:

public class Items implements Serializable{
	
	private Integer id;
	private String name;
	private Double price;
	private Date createtime;
	private String detail;
}

订单表:

public class Orders implements Serializable{

	private Integer id;
	private Users user;
	private String orderId;
	private Date createtime;
	private String note;
	private List<OrderDetail> orderDetails;
}

订单明细表:

public class OrderDetail implements Serializable{

	private Integer id;
	private Integer itemsNum;
	// 关联商品信息
	private Items items;
}

一、一对一查询

MyBatis中使用association标签来解决一对一的关联查询,association标签可用的属性如下:

  1. property : 对象属性的名称
  2. javaType : 对象属性的类型
  3. column : 所对应的外键字段名称
  4. select : 使用另一个查询封装的结果

实现案例:查询所有订单和用户信息。

  • 一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询。如果从用户信息出发查询用户下的订单信息则为一对多查询,因为一个用户可以下多个订单。
  • MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接
    表示返回类型的,而resultMap则是对外部ResultMap的引用

实现方法:使用resultMap

配置OrdersMapper.xml中ResultMap映射

<!-- //定义查询订单的方法关联查询用户信息 public List<Orders> queryOrderAndUser(); 一对一-->
	<select id="queryOrderAndUser" resultMap="orderAndUser">
		select
		o.id oids,o.orderid,o.createtime,o.note,
		u.id uids,u.username,u.birthday,u.address
		from orders o,t_user u
		where
		o.userid=u.id
	</select>
	<resultMap type="orders" id="orderAndUser">
		<id column="oids" property="id"></id>
		<result column="orderid" property="orderId"></result>
		<result column="createtime" property="createtime"></result>
		<result column="note" property="note"></result>
		<!-- 
			表示进行关联查询单条记录
			property:表示关联的结果存储在Orders类中的user属性
			JavaType:表示关联查询的结果类型
		 -->
		<association property="user" javaType="users">
		<!-- 
			查询结果中的userID列关联的user对象的id属性。
		 -->
			<id column="uids" property="id"></id>
			<result column="username" property="username"></result>
			<result column="birthday" property="birthday"></result>
			<result column="address" property="address"></result>
		</association>
	</resultMap>

定义接口:

public List<Orders> queryOrderAndUser();

测试:

public class TestOne2One {

	public static void main(String[] args)throws Exception {
		InputStream is=Resources.getResourceAsStream("mybatis/mybatis-config.xml");
		SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
		SqlSession session=factory.openSession();
		OrdersMapper mapper=session.getMapper(net.neuedu.mybatis.mapper.OrdersMapper.class);	
		List<Orders> orders=mapper.queryOrderAndUser();	
		System.out.println(orders.toString());
	}
}

二、一对多查询

实现案例:查询所有订单信息及订单下的订单明细信息,订单信息与订单明细为一对多关系。

实现方法:使用collection完成关联查询,将关联查询信息映射到集合对象。

配置OrdersMapper.xml中ResultMap映射

	<!-- 
		public List<Orders> queryOrderAndOrderDetail(); 一对多 
		查所有订单信息及订单下的订单明细信息,将相关联的查询映射到集合中
	-->
	<resultMap type="orders" id="orderAndOrderDetail">
		<id property="id" column="oids"></id>
		<result property="orderId" column="orderid"></result>
		<result property="createtime" column="createtime" />
		<result property="note" column="note" />
		<!-- 表示关联查询结果集 -->
		<collection property="orderDetails" ofType="OrderDetail">
			<id property="id" column="odid"></id>
			<result property="itemsNum" column="itemsnum"></result>
			<association property="items" javaType="items">
				<id property="id" column="iid"></id>
				<result property="name" column="name"></result>
				<result property="createtime" column="icreatetime"></result>
				<result property="price" column="price"></result>
				<result property="detail" column="detail"></result>
			</association>
		</collection>
	</resultMap>
	<select id="queryOrderAndOrderDetail" resultMap="orderAndOrderDetail">
		select
		o.id oids,o.userid,o.createtime,o.note,o.orderid,
		od.id odid,od.itemsid,od.itemsnum,
		i.id iid,i.name,i.price,i.createtime icreatetime,i.detail
		from orders o,orderdetail od,items i
		where o.id=od.ordersid and od.itemsid=i.id
	</select>

接口方法:

public List<Orders> queryOrderAndOrderDetail();

三、多对多查询

实现案例:查询所有用户信息,关联查询订单及订单明细信息,订单明细信息中关联查询商品信息

配置OrdersMapper.xml中ResultMap映射

  • 订单:一个用户对应多个订单,使用collection映射到用户对象的订单列表属性中
  • 订单明细:一个订单对应多个明细,使用collection映射到订单对象中的明细属性中
  • 商品信息:一个订单明细对应一个商品,使用association映射到订单明细对象的商品属性中
<resultMap type="users" id="UserAdnAll">
		<id column="uids" property="id"/>
		<result column="username" property="username"/>
		<result column="birthday" property="birthday"/>
		<result column="address" property="address"/>
		<collection property="orders" ofType="orders">
			<id column="oids" property="id"/>
			<result column="orderid" property="orderId"/>
			<result column="createtime" property="createtime"/>
			<result column="note" property="note"/>
			
			<collection property="orderDetails" ofType="OrderDetail">
				<id column="odid" property="id"/>
				<result column="itemsnum" property="itemsNum"/>
				<association property="items" javaType="items">
					<id column="iid" property="id"/>
					<result column="name" property="name"/>
					<result column="price" property="price"/>
					<result column="icreatetime" property="createtime"/>
					<result column="detail" property="detail"/>
					
				</association>
			</collection>
		</collection>
	</resultMap>
	 <!-- 实现案例:查询所有用户信息,关联查询订单及订单明细信息,订单明细信息中关联查询商品信息 -->
	public List<Users> queryAllUserAndOrderAndOrderdetailAndItem();
	<select id="queryAllUserAndOrderAndOrderdetailAndItem" resultMap="UserAdnAll">
		select 
          u.id uids,u.username,u.birthday,u.address,
          o.id oids,o.createtime,o.note,o.orderid,
          od.id odid,od.itemsnum,
          i.id iid,i.name,i.price,i.createtime icreatetime,i.detail
		from t_user u,orders o,orderdetail od,items i
		where u.id=o.userid and o.id=od.ordersid and od.itemsid=i.id
	</select>
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code_mo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值