基于mybatis高级映射多对多查询的实现

2025-05-29 0 74

1.同以前一样,首先给一个使用多对多的需求,

查询用户以及用户所购买的商品信息,经过分析用户和商品数据库级别没有任何关系,用户和商品需要建立关系,要通过订单,订单明细建立关系。根据这个需求,可以分析出需要查询的主表为:

查询主表:用户表

查询关联表:由于商品和用户没有关系,通过订单和订单明细进行关联,所以得出关联表是:orders订单表,orderdetail订单明细表,items商品表。这样的话,sql该如何去写?这样写:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19
select

orders.*,

t_user.id user_id,

t_user.address,

t_user.name,

t_user.brithday,

orderdetail.id orderdetail_id,

orderdetail.orderid,

orderdetail.itemsid,

items.id items_id,

items.name items_name,

items.price items_price

from

orders,

t_user,

orderdetail,

items

where

orders.userid=t_user.id and orderdetail.orderid=orders.id and orderdetail.itemsid = items.id

为了方便映射,适当加上别名,上面就是写好的大sql,这sql语句是越来越大了,但不要害怕,待会做映射一个个来做,只要方法得提是很简单的。

2.编写映射文件分析:

现在的主表是user,所以将用户 的信息映射到user中,一个用户可以创建多个订单,所以在user属性中添加list<orders> orderlist 属性,这和hibernate越来越像了哈,然后将用户创建的订单映射到orderlist,中一个订单可以包括多个订单明细,所以在orders中添加订单明细列表属性:list<orderdetail> orderdetails ,一个订单明细只包括一个商品信息,所以在ordersdetail中添加商品items 属性。

user.java代码如下:

?

1

2

3

4

5

6

7

8
public class user {

private int id;

private string name;

private string pwd;

private string address;

private date brithday;

private list<orders> orderslist;//看这里 用户创建的订单列表

}

orders.java代码如下:

?

1

2

3

4

5

6

7

8
public class orders {

private int id;

private string note;

private date datetime;

private string number;

private int userid;

private user user;

private list<ordersdetail> ordersdetails;//一个订单包括多个订单明细

ordersdetail.java代码如下:

?

1

2

3

4

5
public class ordersdetail {

private int id;

private int orderid;

private int itemsid;

private items items;//一条订单明细包括一条商品信息

到这里,pojo类之间的关系就搭建好了,接下来写mapper.xml

代码如下(注意看代码里的注释):

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50
<!--查询用户所购买的商品信息resultmap-->

<resultmap id="useranditemsresultmap" type="com.djp.pojo.user">

<!--配置用户信息-->

<id column="user_id" property="id"/>

<result column="address" property="address"/>

<result column="name" property="name"/>

<result column="brithday" property="brithday"/>

<!--配置用户创建的订单信息,一个用户创建了多个订单-->

<collection property="orderslist" oftype="com.djp.pojo.orders">

<id column="id" property="id"/>

<result column="note" property="note"/>

<result column="datetime" property="datetime"/>

<result column="userid" property="userid"/>

<result column="number" property="number"/>

<!--配置订单明细信息 一个订单包含多个订单明细-->

<collection property="ordersdetails" oftype="com.djp.pojo.ordersdetail">

<id column="orderdetail_id" property="id"/>

<result column="orderid" property="orderid"/>

<result column="itemsid" property="itemsid"/>

<!--配置商品信息 一个明细包括一个商品-->

<association property="items" javatype="com.djp.pojo.items">

<id column="items_id" property="id"/>

<result column="items_name" property="name"/>

<result column="items_price" property="price"/>

</association>

</collection>

</collection>

</resultmap>

<!--查询用户所购买的商品信息-->

<select id="finduseranditemsresultmap" resultmap="useranditemsresultmap">

select

orders.*,

t_user.id user_id,

t_user.address,

t_user.name,

t_user.brithday,

orderdetail.id orderdetail_id,

orderdetail.orderid,

orderdetail.itemsid,

items.id items_id,

items.name items_name,

items.price items_price

from

orders,

t_user,

orderdetail,

items

where

orders.userid=t_user.id and orderdetail.orderid=orders.id and orderdetail.itemsid = items.id

</select>

有了之前一对多的映射,类比写这个就比较简单了;这里不能使用继承,因为你发现使用继承没法套用了。所以只能老实的写完。

接下来在接口中添加一个方法,为查询用户所购买商品信息

?

1

2

3

4

5

6

7
public interface orderscustommapper {

/**

* 查询用户所购买的商品

* @return

* @throws exception

*/

list<user> finduseranditemsresultmap() throws exception;

最后一步,写测试类:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17
/**finduseranditemsresultmap

* 查询用户所购买的商品信息

*/

@test

public void testfinduseranditemsresultmap() {

try {

system.out.println("start.................");

//通过得到的sqlsessionfactory打开回话sqlsession

sqlsession sqlsession = sqlsessionfactory.opensession();

//通过会话得到用户的代理

orderscustommapper orderscustommapper = sqlsession.getmapper(orderscustommapper.class);

list<user> list = orderscustommapper.finduseranditemsresultmap();

system.out.println("end.................");

} catch (exception e) {

e.printstacktrace();

}

}

运行,结果如下:

基于mybatis高级映射多对多查询的实现

用户商品.png可以看到我展开的列表,是不是想要的数据都出来了;

总结:

到这里可能有人会觉得这个怎么比hibernate中的多对多还麻烦,那么,现在如果有这么一个需求,查询用户所购买的商品信息明细清单(用户名,用户地址,购买商品名称,购买商品时间,购买商品数量),这个时候应该使用resultmap还是resultype,这个时候会发现,针对上面的需求,就是用resulttype将查询到的信息映射到一个扩展的pojo中,很好用,加属性就行了,这样的话就可以很简单地实现明细清单的功能,比如话费账单,张三几点几分给谁打电话,张三几点几分打电话给谁,没必要去重复记录。所以,要根据需求来,并非所有的一对多都是上面的resultmap那种查询。使用resultmap是针对那些查询结果有特殊要求的功能,比如映射成list中还包括多个list。

一对多是多对多的特咧:查询用户购买商品信息,用户和商品是多对多关系。

需求1:查询字段:用户账号,用户名称,用户性别,商品名称,商品价格(最常见)。企业开发中常见的明细表,用户购买商品明细表等。

方法:使用resulttype将上面商品输出

需求2:查询字段:用户账号,用户名称,购买商品数量,商品明细(鼠标移动到上面显示明细)

方法:使用resultmap将用户所购买的商品明细映射到user中。

对resultmap的大总结:

resulttype:

作用:将查询结果按照sql列名和pojo属性名一致映射到pojo中

场合:常见的一些明细记录的展示,比如用户购买商品的明细,将关联查询的信息全部展示在页面时,此时用resulttype将每一条记录映射到pojo中,在前端页面遍历list即可。

resultmap:

使用association和collection完成一对一和一对多的高级映射(对结果有特殊要求)。

association:

作用:将关联查询的信息映射到一个pojo中

场合:为了方便查询关联信息可以使用association将关联订单信息映射为用户对象的pojo属性中,比如:查询订单关联查询用户信息。

collection:

作用:将关联查询用户信息映射到一个list集合中

场合:为了方便查询遍历关联信息可以使用collection将关联信息映射到list集合中,比如:查询用户权限范围模块以及模块下的菜单,可以使用collection将模块映射到list中,将菜单列表遍历即可!

以上这篇基于mybatis高级映射多对多查询的实现就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

原文链接:http://www.jianshu.com/p/58b92011130b

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 基于mybatis高级映射多对多查询的实现 https://www.kuaiidc.com/114030.html

相关文章

发表评论
暂无评论