激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - Mybatis中的高級映射一對一、一對多、多對多

Mybatis中的高級映射一對一、一對多、多對多

2020-06-08 12:31丁國華 JAVA教程

這篇文章主要介紹了Mybatis中的高級映射一對一、一對多、多對多的相關(guān)資料,需要的朋友可以參考下

學習hibernate的時候,小編已經(jīng)接觸多各種映射,mybatis中映射有到底是如何運轉(zhuǎn)的,今天這篇博文,小編主要來簡單的介紹一下mybatis中的高級映射,包括一對一、一對多、多對多,希望多有需要的小伙伴有幫助,小編主要從四個方面進行介紹,訂單商品數(shù)據(jù)模型、一對一查詢、一對多查詢、多對多查詢。

一、訂單商品數(shù)據(jù)模型

1、數(shù)據(jù)庫執(zhí)行腳本,如下所示:

?
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
<span style="font-family:Comic Sans MS;font-size:18px;">CREATE TABLE items (
id INT NOT NULL AUTO_INCREMENT,
itemsname VARCHAR(32) NOT NULL COMMENT '商品名稱',
price FLOAT(10,1) NOT NULL COMMENT '商品定價',
detail TEXT COMMENT '商品描述',
pic VARCHAR(64) DEFAULT NULL COMMENT '商品圖片',
createtime DATETIME NOT NULL COMMENT '生產(chǎn)日期',
PRIMARY KEY (id)
) DEFAULT CHARSET=utf8;
/*Table structure for table `orderdetail` */
CREATE TABLE orderdetail (
id INT NOT NULL AUTO_INCREMENT,
orders_id INT NOT NULL COMMENT '訂單id',
items_id INT NOT NULL COMMENT '商品id',
items_num INT DEFAULT NULL COMMENT '商品購買數(shù)量',
PRIMARY KEY (id),
KEY `FK_orderdetail_1` (`orders_id`),
KEY `FK_orderdetail_2` (`items_id`),
CONSTRAINT `FK_orderdetail_1` FOREIGN KEY (`orders_id`) REFERENCES `orders` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `FK_orderdetail_2` FOREIGN KEY (`items_id`) REFERENCES `items` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) DEFAULT CHARSET=utf8;
/*Table structure for table `orders` */
CREATE TABLE orders (
id INT NOT NULL AUTO_INCREMENT,
user_id INT NOT NULL COMMENT '下單用戶id',
number VARCHAR(30) NOT NULL COMMENT '訂單號',
createtime DATETIME NOT NULL COMMENT '創(chuàng)建訂單時間',
note VARCHAR(100) DEFAULT NULL COMMENT '備注',
PRIMARY KEY (`id`),
KEY `FK_orders_1` (`user_id`),
CONSTRAINT `FK_orders_id` FOREIGN KEY (`user_id`) REFERENCES `t_user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) DEFAULT CHARSET=utf8;
/*Table structure for table `t_user` */
CREATE TABLE t_user (
id INT NOT NULL AUTO_INCREMENT,
username VARCHAR(32) NOT NULL COMMENT '用戶名稱',
birthday DATE DEFAULT NULL COMMENT '生日',
sex CHAR(1) DEFAULT NULL COMMENT '性別',
address VARCHAR(256) DEFAULT NULL COMMENT '地址',
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
</span>

測試數(shù)據(jù)代碼

?
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
<span style="font-family:Comic Sans MS;font-size:18px;">/*Data for the table `items` */
INSERT INTO items(itemsname,price,detail,pic,createtime) VALUES
('臺式機',3000.0,'該電腦質(zhì)量非常好!',NULL,'2015-07-07 13:28:53'),
('筆記本',6000.0,'筆記本性能好,質(zhì)量好!',NULL,'2015-07-08 13:22:57'),
('背包',200.0,'名牌背包,容量大質(zhì)量好!',NULL,'2015-07-010 13:25:02');
/*Data for the table `orderdetail` */
INSERT INTO `orderdetail`(`orders_id`,`items_id`,`items_num`) VALUES
(1,1,1),
(1,2,3),
(2,3,4),
(3,2,3);
/*Data for the table `orders` */
INSERT INTO `orders`(`user_id`,`number`,`createtime`,`note`) VALUES
(1,'1000010','2015-06-04 13:22:35',NULL),
(1,'1000011','2015-07-08 13:22:41',NULL),
(2,'1000012','2015-07-17 14:13:23',NULL),
(3,'1000012','2015-07-16 18:13:23',NULL),
(4,'1000012','2015-07-15 19:13:23',NULL),
(5,'1000012','2015-07-14 17:13:23',NULL),
(6,'1000012','2015-07-13 16:13:23',NULL);
/*Data for the table `user` */
INSERT INTO `t_user`(`username`,`birthday`,`sex`,`address`) VALUES
('王五',NULL,'2',NULL),
('張三','2014-07-10','1','北京市'),
('張小明',NULL,'1','河南鄭州'),
('陳小明',NULL,'1','河南鄭州'),
('張三豐',NULL,'1','河南鄭州'),
('陳小明',NULL,'1','河南鄭州'),
('王五',NULL,NULL,NULL),
('小A','2015-06-27','2','北京'),
('小B','2015-06-27','2','北京'),
('小C','2015-06-27','1','北京'),
('小D','2015-06-27','2','北京');
</span>

2、數(shù)據(jù)模型分析思路

(1).每張表記錄的數(shù)據(jù)內(nèi)容:分模塊對每張表記錄的內(nèi)容進行熟悉,相當 于你學習系統(tǒng) 需求(功能)的過程;

(2).每張表重要的字段設(shè)置:非空字段、外鍵字段;

(3).數(shù)據(jù)庫級別表與表之間的關(guān)系:外鍵關(guān)系;

(4).表與表之間的業(yè)務(wù)關(guān)系:在分析表與表之間的業(yè)務(wù)關(guān)系時一定要建立在某個業(yè)務(wù)意義基礎(chǔ)上去分析。

3、針對訂單商品模型的數(shù)據(jù)庫思路分析,如下圖所示:

Mybatis中的高級映射一對一、一對多、多對多

二、一對一查詢

2.1、需求:查詢訂單信息,關(guān)聯(lián)查詢用戶信息

2.2、resultType實現(xiàn)

2.2.1sql語句

確定查詢的主表:訂單表,確定查詢的關(guān)聯(lián)表,用戶表,代碼如下所示:

?
1
2
3
4
5
6
7
8
9
<span style="font-family:Comic Sans MS;font-size:18px;">SELECT t1.*,
t2.username,
t2.sex,
t2.address
FROM
orders t1,
t_user t2
WHERE t1.user_id=t2.id
</span>

2.2.2創(chuàng)建entity實體

用戶實體User.java,代碼如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<span style="font-family:Comic Sans MS;font-size:18px;">package com.mybatis.entity;
import java.util.Date;
import java.util.List;
/**
* @ClassName: User
* @Description: TODO(用戶實體)
* @author 阿赫瓦里
*/
public class User {
private Integer id;
// 姓名
private String username;
// 性別
private String sex;
// 地址
private String address;
// 生日
private Date birthday;
// 用戶創(chuàng)建的訂單列表
private List<Orders> ordersList;
// getter and setter ......
}
</span>

訂單實體orders.java

?
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
<span style="font-family:Comic Sans MS;font-size:18px;">package com.mybatis.entity;
import java.util.Date;
import java.util.List;
/**
* @ClassName: Orders
* @Description: TODO(訂單實體)
* @author 阿赫瓦里
*/
public class Orders {
/** 主鍵訂單Id */
private Integer id;
/** 下單用戶id */
private Integer userid;
/** 訂單號 */
private String number;
/** 創(chuàng)建訂單時間 */
private Date createTime;
/** 備注 */
private String note;
// 用戶信息
private User user;
// 訂單明細
private List<OrderDetail> orderdetails;
// getter and setter ......
}
</span>

商品實體:items.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<span style="font-family:Comic Sans MS;font-size:18px;">package com.mybatis.entity;
import java.util.Date;
/**
* @ClassName: Items
* @Description: TODO(商品實體類)
* @author 丁國華
*/
public class Items {
/** 商品表主鍵Id */
private Integer id;
/** 商品名稱 */
private String itemsName;
/** 商品定價 */
private float price;
/** 商品描述 */
private String detail;
/** 商品圖片 */
private String picture;
/** 生產(chǎn)日期 */
private Date createTime;
// getter and setter ......
}
</span>

訂單明細實體orderDetail.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<span style="font-family:Comic Sans MS;font-size:18px;">package com.mybatis.entity;
/**
* @ClassName: OrderDetail
* @Description: TODO(訂單明細實體)
* @author 丁國華
*/
public class OrderDetail {
/** 主鍵,訂單明細表Id */
private Integer id;
/** 訂單Id */
private Integer ordersId;
/** 商品id */
private Integer itemsId;
/** 商品購買數(shù)量 */
private Integer itemsNum;
// 明細對應(yīng)的商品信息
private Items items;
// getter and setter ......
}
</span>

創(chuàng)建一個包裝類,將查詢到的信息全部映射到此類OrdersCustom.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<span style="font-family:Comic Sans MS;font-size:18px;">/**
* @ClassName: OrdersCustom
* @Description: TODO(訂單的擴展類,通過此類映射訂單和用戶的查詢結(jié)果,讓此類繼承字段較多的實體類)
* @author: 丁國華
*/
public class OrdersCustom extends Orders {
// 添加用戶的屬性
private String username;
private String sex;
private String address;
// getter and setter......
}
</span>

2.2.3創(chuàng)建OrderscCustomMapper.java,代碼如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<span style="font-family:Comic Sans MS;font-size:18px;">package com.mybatis.Mapper;
import java.util.List;
import com.mybatis.entity.OrdersCustom;
/**
* @ClassName: OrdersMapperCustom
* @Description: TODO(OrdersMapperCustom的mapper)
* @author 丁國華
*/
public interface OrdersCustomMapper {
/** 查詢訂單,關(guān)聯(lián)查詢用戶信息 */
public List<OrdersCustom> findOrdersUser();
}
</span>

2.2.4創(chuàng)建OrdersCustomMapper.xml和上面對應(yīng)的接口名稱一致,一邊通過mapper接口加載配置文件,代碼如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<span style="font-family:Comic Sans MS;font-size:18px;"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace命名空間,作用就是對sql進行分類化的管理,理解為sql隔離
注意:使用mapper代理開發(fā)時,namespace有特殊作用,namespace等于mapper接口地址
-->
<mapper namespace="com.mybatis.mapper.OrdersCustomMapper">
<!-- 查詢訂單,關(guān)聯(lián)查詢用戶信息 -->
<select id="findOrdersUser" resultType="com.mybatis.entity.OrdersCustom">
SELECT t1.*,
t2.username,
t2.sex,
t2.address
FROM
orders t1,
t_user t2
WHERE t1.user_id=t2.id
</select>
</mapper>
</span>

2.3resultMap實現(xiàn)

2.3.1sql語句同上

2.3.2resultMap映射思路:

使用resultMap將查詢結(jié)果中的訂單信息映射到Orders對象中,在orders類中添加User屬性,將關(guān)聯(lián)查詢出來的用戶信息映射到orders對象中的user屬性中(上面orders實體中已經(jīng)添加)。

2.3.3 ordersCustomMapper.xml

1、定義resultMap,代碼如下所示:

?
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
<span style="font-family:Comic Sans MS;font-size:18px;"><!-- 定義查詢訂單關(guān)聯(lián)用戶的 resultMap,將整個的查詢結(jié)果映射到com.mybatis.entity.Orders中 -->
<resultMap type="com.mybatis.entity.Orders" id="OrdersUserResultMap">
<!-- 配置映射的訂單信息 -->
<!-- id:查詢列中的唯一標識,訂單信息中的唯一標識,如果多列組成唯一標識(如:一般數(shù)據(jù)庫設(shè)計中的字典表 使用聯(lián)合主鍵),就需要配置多個id
column:訂單信息的唯一標識 列
property:訂單信息的唯一標識列所映射到orders中的那個屬性(假如:數(shù)據(jù)庫中orders表中的主鍵為orders_id,而實體屬性名稱為ordersId,
則這個配置應(yīng)為<id column="orders_id" property="ordersId"/>,類似hibernate實體映射文件配置)。
-->
<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"/>
<!-- 配置映射的關(guān)聯(lián)用戶信息 -->
<!--association:用于映射關(guān)聯(lián)查詢單個對象的信息
property:要將關(guān)聯(lián)查詢的用戶信息映射到Orders中那個屬性
-->
<association property="user" javaType="com.mybatis.entity.User">
<!-- id:關(guān)聯(lián)查詢用戶的唯一標識
column:指定唯一標識用戶信息的列
property:映射到user的那個屬性
-->
<id column="user_id" property="id"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/>
<result column="address" property="address"/>
</association>
</resultMap>
</span>

2、statement定義,代碼如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
<span style="font-family:Comic Sans MS;font-size:18px;"><!-- 查詢訂單,關(guān)聯(lián)查詢用戶信息,使用resultMap實現(xiàn) -->
<select id="findOrdersUserResultMap" resultMap="OrdersUserResultMap">
SELECT t1.*,
t2.username,
t2.sex,
t2.address
FROM
orders t1,
t_user t2
WHERE t1.user_id=t2.id
</select></span>

3、OrderCustomMapper.java接口中添加下面的方法:

?
1
2
3
<span style="font-family:Comic Sans MS;font-size:18px;">/** 查詢訂單關(guān)聯(lián)查詢用戶信息,使用reslutMap實現(xiàn)*/
public List<Orders>findOrdersUserResultMap();
</span>

4、對resultType和resultMap實現(xiàn)的junit測試,代碼如下所示:

?
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
<span style="font-family:Comic Sans MS;font-size:18px;">package com.mybatis.test;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import com.mybatis.entity.Orders;
import com.mybatis.entity.OrdersCustom;
import com.mybatis.mapper.OrdersCustomMapper;
public class OrdersCustomMapperTest {
private SqlSessionFactory sqlSessionFactory;
// 此方法是在執(zhí)行findUserByIdTest之前執(zhí)行
@Before
public void setUp() throws Exception {
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 創(chuàng)建SqlSessionFcatory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
// 查詢訂單,關(guān)聯(lián)查詢用戶信息,使用resultType實現(xiàn)的測試
@Test
public void TestFindOrdersUser() {
SqlSession sqlSession = sqlSessionFactory.openSession();
// 創(chuàng)建代理對象
OrdersCustomMapper oc = sqlSession.getMapper(OrdersCustomMapper.class);
// 調(diào)用mapper的方法
List<OrdersCustom> list = oc.findOrdersUser();
System.out.println(list);
sqlSession.close();
}
// 查詢訂單,關(guān)聯(lián)查詢用戶信息,使用resultMap實現(xiàn)的測試
@Test
public void TestFindOrdersUserResultMap() {
SqlSession sqlSession = sqlSessionFactory.openSession();
// 創(chuàng)建代理對象
OrdersCustomMapper oc = sqlSession.getMapper(OrdersCustomMapper.class);
// 調(diào)用mapper的方法
List<Orders> list = oc.findOrdersUserResultMap();
System.out.println(list);
sqlSession.close();
}
}
</span>

5、resultType和resultMap實現(xiàn)一對一查詢小結(jié)

實現(xiàn)一對一查詢:

a.resultType:使用resultType實現(xiàn)較為簡單,如果pojo中沒有包括查詢出來的列名,需要增加列名對應(yīng)的屬性,即可完成映射。

b.如果沒有查詢結(jié)果的特殊要求建議使用resultType。

c.resultMap:需要單獨定義resultMap,實現(xiàn)有點麻煩,如果對查詢結(jié)果有特殊的要求,使用resultMap可以完成將關(guān)聯(lián)查詢映射pojo的屬性中。

d.resultMap可以實現(xiàn)延遲加載,resultType無法實現(xiàn)延遲加載。

三、一對多查詢

3.1 需求:查詢訂單(關(guān)聯(lián)用戶)及訂單明細;

3.2 在orders.java類中添加List<orderDetail> orderDetails屬性(上面實體已添加)。最終會將訂單信息映射到orders中,訂單所對應(yīng)的訂單明細映射到orders中的orderDetails屬性中。

3.3 在ordersCustomMapper.xml中添加如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<span style="font-family:Comic Sans MS;font-size:18px;"><!-- 查詢訂單關(guān)聯(lián)查詢用戶及訂單明細 -->
<select id="findOrdersAndOrderDetailResultMap" resultMap="ordersAndOrderDetailResultMap">
SELECT
t1.*,
t2.username,
t2.sex,
t2.address,
t3.id orderdetail_id,
t3.items_id,
t3.items_num,
t3.orders_id
FROM
orders t1,
t_user t2,
orderdetail t3
WHERE t1.user_id = t2.id AND t3.orders_id=t1.id
</select>
</span>

resultMap的定義同樣添加到ordersCustomMapper.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<span style="font-family:Comic Sans MS;font-size:18px;"><!-- 查詢訂單(關(guān)聯(lián)用戶)及訂單明細的resultMap -->
<resultMap type="com.mybatis.entity.Orders" id="ordersAndOrderDetailResultMap" extends="OrdersUserResultMap">
<!-- 訂單信息 -->
<!-- 關(guān)聯(lián)用戶信息 -->
<!-- 使用extends繼承,不用在中配置訂單信息和用戶信息的映射-->
 
<!-- 關(guān)聯(lián)訂單明細信息
一個訂單關(guān)聯(lián)查詢出了多條訂單明細,要使用collection映射
collection:對關(guān)聯(lián)查詢到的多條記錄映射到集合中
property:將關(guān)聯(lián)查詢到的多條記錄映射到orders類的那個屬性
ofType:指定映射的集合屬性中pojo的類型
-->
<collection property="orderdetails" ofType="com.mybatis.entity.OrderDetail">
<!-- id:唯一標識
property:要將訂單明細的唯一標識映射到com.mybatis.entity.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"/>
</collection>
</resultMap>
</span>

3.4 在OrderCustomeMapper.java接口類中添加一個方法,代碼如下所示:

?
1
2
3
<span style="font-family:Comic Sans MS;font-size:18px;">/**查詢訂單(關(guān)聯(lián)用戶)以及訂單明細*/
public List<OrderDetail>findOrdersAndOrderDetailResultMap();
</span>

3.5 在Junit測試類中添加測試方法,代碼如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
<span style="font-family:Comic Sans MS;font-size:18px;">// 查詢訂單(關(guān)聯(lián)用戶)以及訂單明細的測試
@Test
public void TestFindOrdersAndOrderDetailResultMap() {
SqlSession sqlSession = sqlSessionFactory.openSession();
// 創(chuàng)建代理對象
OrdersCustomMapper oc = sqlSession.getMapper(OrdersCustomMapper.class);
// 調(diào)用mapper的方法
List<OrderDetail> list = oc.findOrdersAndOrderDetailResultMap();
System.out.println(list);
sqlSession.close();
}
</span>

3.6 小結(jié)

mybatis使用resultMap的collection對關(guān)聯(lián)查詢的多條記錄映射到一個list集合屬性中。使用resultType實現(xiàn):將訂單明細映射到orders中的orderdetails中,需要自己處理,使用雙重循環(huán)遍歷,去掉重復(fù)記錄,將訂單明細放在orderdetails中。

四、多對多查詢

4.1 需求:查詢用戶以及用戶購買的商品信息

4.2 映射思路

將用戶信息映射到user中,在user類中添加訂單列表屬性List<Orders>orderslist,將用戶創(chuàng)建的訂單映射到orderslist;在Orders中添加訂單明細列表屬性List<OrderDetail>orderdetials,將訂單的明細映射到orderdetials;在OrderDetail中添加Items屬性,將訂單明細所對應(yīng)的商品映射到Item。

4.3 OrdersCustomMapper.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
51
52
53
54
55
56
57
<span style="font-family:Comic Sans MS;font-size:18px;"><!-- 查詢用戶即購買的商品信息的ResultMap -->
<resultMap type="com.mybatis.entity.User" id="userAndItemsResultMap">
<!-- 用戶信息 -->
<id column="user_id" property="id"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/>
<result column="address" property="address"/>
<!-- 訂單信息
一個用戶對應(yīng)多個訂單,使用collection映射 -->
<collection property="ordersList" ofType="com.mybatis.entity.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 property="orderdetails" ofType="com.mybatis.entity.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"/>
<!-- 商品信息
一個訂單明細對應(yīng)一個商品
-->
<association property="items" javaType="com.mybatis.entity.Items">
<id column="items_id" property="id"/>
<result column="items_name" property="itemsName"/>
<result column="items_detail" property="detail"/>
<result column="items_price" property="price"/>
</association>
</collection>
</collection>
</resultMap>
<!-- 查詢用戶及用戶購買的商品信息,使用resulaMap-->
<select id="findUserAndItemsResultMap" resultMap="userAndItemsResultMap">
SELECT
t1.*,
t2.username,
t2.sex,
t2.address,
t3.id orderdetail_id,
t3.items_id,
t3.items_num,
t3.orders_id,
t4.itemsname items_name,
t4.detail items_detail,
t4.price items_price
FROM
orders t1,
t_user t2,
orderdetail t3,
items t4
WHERE t1.user_id = t2.id AND t3.orders_id=t1.id AND t3.items_id = t4.id
</select>
</span>

4.4 在OrderCustomMapper.java中添加如下方法:

?
1
2
3
<span style="font-family:Comic Sans MS;font-size:18px;"> /** 查詢用戶及用戶所購買的商品信息 */
public List<User> findUserAndItemsResultMap();
</span>

4.5 在Junit測試中添加測試方法,代碼如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
<span style="font-family:Comic Sans MS;font-size:18px;">// 查詢用戶及用戶購買的商品的信息
@Test
public void TestFindUserAndItemsResultMap() {
SqlSession sqlSession = sqlSessionFactory.openSession();
// 創(chuàng)建代理對象
OrdersCustomMapper oc = sqlSession.getMapper(OrdersCustomMapper.class);
// 調(diào)用mapper的方法
List<User> list = oc.findUserAndItemsResultMap();
System.out.println(list);
sqlSession.close();
}
</span>

4.6 resultMap總結(jié)

resultType:

作用:將查詢結(jié)果按照sql列名pojo屬性名一致性映射到pojo中。

場合:常見一些明細記錄的展示,比如用戶購買商品明細,將關(guān)聯(lián)查詢信息全部展示在頁面時,此時可直接使用resultType將每一條記錄映射到pojo中,在前端頁面遍歷list(list中是pojo)即可。

resultMap:

使用:association和collection完成一對一和一對多高級映射(對結(jié)果有特殊的映射要求)。

association:

作用:將關(guān)聯(lián)查詢信息映射到一個pojo對象中。

場合:為了方便查詢關(guān)聯(lián)信息可以使用association將關(guān)聯(lián)訂單信息映射為用戶對象的pojo屬性中,比如:查詢訂單及關(guān)聯(lián)用戶信息。

使用resultType無法將查詢結(jié)果映射到pojo對象的pojo屬性中,根據(jù)對結(jié)果集查詢遍歷的需要選擇使用resultType還是resultMap。

collection:

作用:將關(guān)聯(lián)查詢信息映射到一個list集合中。

場合:為了方便查詢遍歷關(guān)聯(lián)信息可以使用collection將關(guān)聯(lián)信息映射到list集合中,比如:查詢用戶權(quán)限范圍模塊及模塊下的菜單,可使用collection將模塊映射到模塊list中,將菜單列表映射到模塊對象的菜單list屬性中,這樣的作的目的也是方便對查詢結(jié)果集進行遍歷查詢。

如果使用resultType無法將查詢結(jié)果映射到list集合中。

以上所述是小編給大家介紹的Mybatis中的高級映射一對一、一對多、多對多,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!

原文鏈接:http://blog.csdn.net/u010850027/article/details/52289513

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 九色中文字幕 | 黄色av片在线观看 | 99精品视频在线看 | 一区二区网 | 国产在线一级片 | 国产毛毛片一区二区三区四区 | 日本xxxx视频 | 亚洲操比视频 | 久久99精品国产99久久6男男 | 欧美国产一区二区三区 | 久久久久国产一区二区三区不卡 | 中文字幕在线视频日本 | 国产毛片在线高清视频 | 亚洲午夜免费 | 久久久久久久久日本理论电影 | 国产精品热 | 日本不卡二区 | 干一夜综合 | 日日操夜夜透 | 色综合网在线观看 | 久久亚洲激情 | 国产在线精品一区二区 | 日韩精品中文字幕在线播放 | 日韩中文字幕一区二区三区 | 国产精品久久久久影院老司 | 久久精品一区二区三区四区五区 | 在线成人www免费观看视频 | 欧美另类综合 | 夜夜夜操操操 | 中文字幕一区二区三区久久 | 看免费一级毛片 | 色综合久久久久久 | 日韩在线观看视频网站 | 澳门一级淫片免费视频 | 久久999精品 | 91超视频 | 天堂成人一区二区三区 | 免费久久精品 | 福利在线免费视频 | videos高潮| 亚洲最大中文字幕 |