最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis多表關(guān)聯(lián)查詢(xún)的實(shí)現(xiàn)(DEMO)

 更新時(shí)間:2017年02月24日 11:37:06   作者:陳敬(Cathy)  
本節(jié)要實(shí)現(xiàn)的是多表關(guān)聯(lián)查詢(xún)的簡(jiǎn)單demo。場(chǎng)景是根據(jù)id查詢(xún)某商品分類(lèi)信息,并展示該分類(lèi)下的商品列表,需要的朋友可以參考下

概要

本節(jié)要實(shí)現(xiàn)的是多表關(guān)聯(lián)查詢(xún)的簡(jiǎn)單demo。場(chǎng)景是根據(jù)id查詢(xún)某商品分類(lèi)信息,并展示該分類(lèi)下的商品列表。

一、Mysql測(cè)試數(shù)據(jù)

新建表Category(商品分類(lèi))和Product(商品),并插入幾條測(cè)試數(shù)據(jù)。

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 ('書(shū)籍');
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, '雅詩(shī)蘭黛');
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核心技術(shù)');

二、配置mybatis-generator-config.xml

配置mybatis-generator-config.xml的方法見(jiàn) JAVA入門(mén)[7]-Mybatis generator(MBG)自動(dòng)生成mybatis代碼 ,這里主要改動(dòng)的是table節(jié)點(diǎn)。

<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面板運(yùn)行mybatis-generator:generate,自動(dòng)生成相關(guān)的類(lèi)。

Image(36)

三、自定義mybatis關(guān)聯(lián)查詢(xún)

1.封裝實(shí)體dto

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

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節(jié)點(diǎn),id對(duì)應(yīng)上面的方法名getById;parameterType參數(shù)類(lèi)型為Integer;resultMap為自定義resultMap的id。

 <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>

接下來(lái)定義resultMap節(jié)點(diǎn)id為CategoryResult,type為CategoryDto。

關(guān)于resultMap:

  • id – 一個(gè) ID 結(jié)果;標(biāo)記結(jié)果作為 ID 可以幫助提高整體效能
  • result – 注入到字段或 JavaBean 屬性的普通結(jié)果
  • association – 一個(gè)復(fù)雜的類(lèi)型關(guān)聯(lián);許多結(jié)果將包成這種類(lèi)型
  • 嵌入結(jié)果映射 – 結(jié)果映射自身的關(guān)聯(lián),或者參考一個(gè)
  • collection – 復(fù)雜類(lèi)型的集
  • 嵌入結(jié)果映射 – 結(jié)果映射自身的集,或者參考一個(gè)

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

用association對(duì)應(yīng)category,collection對(duì)應(yīng)products,然后用result對(duì)應(yīng)到每個(gè)具體字段。

<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> 

四、測(cè)試

在上一節(jié)測(cè)試基礎(chǔ)上新增測(cè)試方法:

@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());
 }
 }
 }

運(yùn)行之后居然報(bào)錯(cuò)了

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

后來(lái)找到了解決方案,修改resultMap,添加id節(jié)點(diǎn)就可以了。

<resultMap id="CategoryResult" type="com.data.dto.CategoryDto">
 <id property="id" column="CateId"></id>
……
</resultMap>

運(yùn)行結(jié)果:

商品id=2 name=美妝

Products:6

    |_膜法世家

    |_御泥坊

    |_雅詩(shī)蘭黛

    |_歐萊雅

    |_韓后

    |_相宜本草

相關(guān)文章

  • Java中的幾種關(guān)鍵字的使用小結(jié)

    Java中的幾種關(guān)鍵字的使用小結(jié)

    本文主要介紹了Java中的幾種關(guān)鍵字的使用小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • 淺談mybatis中的#和$的區(qū)別

    淺談mybatis中的#和$的區(qū)別

    下面小編就為大家?guī)?lái)一篇淺談mybatis中的#和$的區(qū)別。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-06-06
  • JAVA中的deflate壓縮實(shí)現(xiàn)方法

    JAVA中的deflate壓縮實(shí)現(xiàn)方法

    下面小編就為大家?guī)?lái)一篇JAVA中的deflate壓縮實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-09-09
  • Spring.Net控制反轉(zhuǎn)IoC入門(mén)使用

    Spring.Net控制反轉(zhuǎn)IoC入門(mén)使用

    這篇文章主要為大家詳細(xì)介紹了Spring.Net控制反轉(zhuǎn)IoC入門(mén)使用的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Java實(shí)現(xiàn)常見(jiàn)的排序算法代碼實(shí)例

    Java實(shí)現(xiàn)常見(jiàn)的排序算法代碼實(shí)例

    這篇文章主要介紹了Java實(shí)現(xiàn)常見(jiàn)的排序算法代碼實(shí)例,按照思路實(shí)現(xiàn)了以下幾個(gè)排序算法(冒泡排序、直接插入排序、直接選擇排序、快速排序),方便日后用到,特此記錄一下,需要的朋友可以參考下
    2023-11-11
  • Java數(shù)據(jù)結(jié)構(gòu)與算法之循環(huán)隊(duì)列的實(shí)現(xiàn)

    Java數(shù)據(jù)結(jié)構(gòu)與算法之循環(huán)隊(duì)列的實(shí)現(xiàn)

    循環(huán)隊(duì)列 (Circular Queue) 是一種特殊的隊(duì)列。循環(huán)隊(duì)列解決了隊(duì)列出隊(duì)時(shí)需要將所有數(shù)據(jù)前移一位的問(wèn)題。本文將帶大家詳細(xì)了解循環(huán)隊(duì)列如何實(shí)現(xiàn),需要的朋友可以參考一下
    2021-12-12
  • Spring?this調(diào)用當(dāng)前類(lèi)方法無(wú)法攔截的示例代碼

    Spring?this調(diào)用當(dāng)前類(lèi)方法無(wú)法攔截的示例代碼

    這篇文章主要介紹了Spring?this調(diào)用當(dāng)前類(lèi)方法無(wú)法攔截,通過(guò)debug 查看這個(gè)proxyService1 和this的區(qū)別,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • RabbitMq的5種模式及實(shí)例解讀

    RabbitMq的5種模式及實(shí)例解讀

    這篇文章主要介紹了RabbitMq的5種模式及實(shí)例解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Kafka?日志存儲(chǔ)實(shí)現(xiàn)過(guò)程

    Kafka?日志存儲(chǔ)實(shí)現(xiàn)過(guò)程

    這篇文章主要為大家介紹了Kafka?日志存儲(chǔ)的實(shí)現(xiàn)過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • 深入解析Apache Kafka實(shí)時(shí)流處理平臺(tái)

    深入解析Apache Kafka實(shí)時(shí)流處理平臺(tái)

    這篇文章主要為大家介紹了Apache Kafka實(shí)時(shí)流處理平臺(tái)深入解析,從基本概念到實(shí)戰(zhàn)操作詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01

最新評(píng)論

佛学| 郯城县| 齐齐哈尔市| 岫岩| 革吉县| 稷山县| 唐山市| 黄骅市| 江山市| 兰州市| 敦煌市| 金坛市| 吉木萨尔县| 崇明县| 新津县| 凤凰县| 龙江县| 宜君县| 门源| 南丰县| 安福县| 山阳县| 沂源县| 两当县| 富裕县| 沂水县| 曲麻莱县| 台中市| 河北省| 枞阳县| 合江县| 峨眉山市| 如皋市| 岳西县| 临澧县| 甘孜县| 疏勒县| 通州市| 汶上县| 韶关市| 稷山县|