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

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

香港云服务器
服務器之家 - 編程語言 - Java教程 - Mybatis多表關聯查詢的實現(DEMO)

Mybatis多表關聯查詢的實現(DEMO)

2020-08-19 11:13陳敬(Cathy) Java教程

本節要實現的是多表關聯查詢的簡單demo。場景是根據id查詢某商品分類信息,并展示該分類下的商品列表,需要的朋友可以參考下

概要

本節要實現的是多表關聯查詢的簡單demo。場景是根據id查詢某商品分類信息,并展示該分類下的商品列表。

一、Mysql測試數據

新建表Category(商品分類)和Product(商品),并插入幾條測試數據。

?
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
create table Category (
Id int not null auto_increment,
Name varchar(80) null,
constraint pk_category primary key (Id)
);
INSERT INTO category(Name) VALUES ('女裝');
INSERT INTO category(Name) VALUES ('美妝');
INSERT INTO category(Name) VALUES ('書籍');
create table product (
Id int not null auto_increment,
categoryId int not null,
Name varchar(80) null,
constraint pk_product primary key (Id),
constraint fk_product_2 foreign key (categoryId)
references category (Id)
);
create index productCat on product (categoryId);
create index productName on product (Name);
INSERT INTO product(CategoryId,Name) VALUES (1, '裂帛');
INSERT INTO product(CategoryId,Name) VALUES (1, '雅鹿');
INSERT INTO product(CategoryId,Name) VALUES (2,'膜法世家');
INSERT INTO product(CategoryId,Name) VALUES (2,'御泥坊');
INSERT INTO product(CategoryId,Name) VALUES (2, '雅詩蘭黛');
INSERT INTO product(CategoryId,Name) VALUES (2, '歐萊雅');
INSERT INTO product(CategoryId,Name) VALUES (2, '韓后');
INSERT INTO product(CategoryId,Name) VALUES (2, '相宜本草');
INSERT INTO product(CategoryId,Name) VALUES (3,'瘋狂JAVA');
INSERT INTO product(CategoryId,Name) VALUES (3,'JAVA核心技術');

二、配置mybatis-generator-config.xml

配置mybatis-generator-config.xml的方法見 JAVA入門[7]-Mybatis generator(MBG)自動生成mybatis代碼 ,這里主要改動的是table節點。

?
1
2
3
4
5
6
<table tableName="category" enableCountByExample="true" enableDeleteByExample="true" enableSelectByExample="true" enableUpdateByExample="true">
 <generatedKey column="Id" sqlStatement="mysql" identity="true"/>
</table>
<table tableName="product" enableCountByExample="true" enableSelectByExample="true" enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true" enableInsert="true">
 <generatedKey column="Id" sqlStatement="mysql" identity="true"></generatedKey>
</table>

配置好xml文件后,在Maven面板運行mybatis-generator:generate,自動生成相關的類。

Mybatis多表關聯查詢的實現(DEMO)

三、自定義mybatis關聯查詢

1.封裝實體dto

我們新定義CategoryDto,封裝商品分類信息及其商品列表。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class CategoryDto {
 private Category category;
 private List<Product> products;
 private int id;
 public int getId() {
 return id;
 }
 public void setId(int id) {
 this.id = id;
 }
 public Category getCategory() {
 return category;
 }
 public void setCategory(Category category) {
 this.category = category;
 }
 public List<Product> getProducts() {
 return products;
 }
 public void setProducts(List<Product> products) {
 this.products = products;
 }
}

2.為CategoryMapper.java接口新增方法getById()

CategoryDto getById(int id);

3.配置CategoryMapper.xml

首先定義select節點,id對應上面的方法名getById;parameterType參數類型為Integer;resultMap為自定義resultMap的id。

?
1
2
3
4
5
<select id="getById" parameterType="java.lang.Integer" resultMap="CategoryResult">
SELECT Category.Id AS CateId,Category.Name AS CateName,Product.Id AS ProductId,Product.Name AS ProductName
FROM Category,Product
WHERE Category.Id=Product.CategoryId AND Category.Id=#{id}
</select>

接下來定義resultMap節點id為CategoryResult,type為CategoryDto。

關于resultMap:

  • id – 一個 ID 結果;標記結果作為 ID 可以幫助提高整體效能
  • result – 注入到字段或 JavaBean 屬性的普通結果
  • association – 一個復雜的類型關聯;許多結果將包成這種類型
  • 嵌入結果映射 – 結果映射自身的關聯,或者參考一個
  • collection – 復雜類型的集
  • 嵌入結果映射 – 結果映射自身的集,或者參考一個

完整參考官網:http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#Result_Maps

用association對應category,collection對應products,然后用result對應到每個具體字段。

?
1
2
3
4
5
6
7
8
9
10
<resultMap id="CategoryResult" type="com.data.dto.CategoryDto">
 <association property="category" javaType="com.data.pojo.Category">
 <result property="id" column="CateId"></result>
 <result property="name" column="CateName"></result>
 </association>
 <collection property="products" ofType="com.data.pojo.Product">
 <result property="id" column="ProductId"></result>
 <result property="name" column="ProductName"></result>
 </collection>
 </resultMap>

四、測試

在上一節測試基礎上新增測試方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Test
 public void test_getById(){
 int id=2;
 CategoryDto dto= categoryMapper.getById(id);
 if(dto==null){
 System.out.println("不存在");
 }else {
 System.out.println("商品id="+dto.getId()+" name="+dto.getCategory().getName());
 System.out.println("Products:"+dto.getProducts().size());
 for(Product product:dto.getProducts()){
 System.out.println(" |_"+product.getName());
 }
 }
 }

運行之后居然報錯了

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 6

后來找到了解決方案,修改resultMap,添加id節點就可以了。

?
1
2
3
4
<resultMap id="CategoryResult" type="com.data.dto.CategoryDto">
 <id property="id" column="CateId"></id>
……
</resultMap>

運行結果:

商品id=2 name=美妝

Products:6

    |_膜法世家

    |_御泥坊

    |_雅詩蘭黛

    |_歐萊雅

    |_韓后

    |_相宜本草

原文鏈接:http://www.cnblogs.com/janes/archive/2017/02/24/6437351.html

延伸 · 閱讀

精彩推薦
1866
主站蜘蛛池模板: 欧美成人鲁丝片在线观看 | 亚洲天堂中文字幕在线观看 | 最新se94se在线欧美 | 亚洲一级片免费观看 | 欧美一级片免费在线观看 | 精品小视频 | 国产正在播放 | av一二三四区 | 狠狠干最新网址 | 欧美性生活免费视频 | 91成人一区 | 欧美视频国产精品 | 91成人一区二区三区 | 欧美一级片一区 | 男女一边摸一边做羞羞视频免费 | 好看的91视频 | 欧美成人精品 | av资源在线天堂 | 国产女同玩人妖 | 黄色免费大片 | 免费一级欧美在线观看视频 | 99精品视频在线导航 | 色淫湿视频 | 毛片av网| 中文字幕 在线观看 | chinese中国真实乱对白 | 色屁屁xxxxⅹ在线视频 | 欧美一级黄色影院 | 男女污视频在线观看 | 成人综合在线观看 | 精品久久久久久久久久中出 | 国产精品免费看 | 91精品国产91久久久久久丝袜 | 久久精品亚洲一区二区三区观看模式 | 精品小视频 | 在线视频 欧美日韩 | 一级毛片在线免费播放 | 二区三区四区 | 免费一区二区三区 | 在线观看av国产一区二区 | 一区二区三区视频在线播放 |