概要
專業(yè)成都網(wǎng)站建設(shè)公司,做排名好的好網(wǎng)站,排在同行前面,為您帶來(lái)客戶和效益!成都創(chuàng)新互聯(lián)為您提供成都網(wǎng)站建設(shè),五站合一網(wǎng)站設(shè)計(jì)制作,服務(wù)好的網(wǎng)站設(shè)計(jì)公司,成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)負(fù)責(zé)任的成都網(wǎng)站制作公司!
本節(jié)要實(shí)現(xiàn)的是多表關(guān)聯(lián)查詢的簡(jiǎn)單demo。場(chǎng)景是根據(jù)id查詢某商品分類信息,并展示該分類下的商品列表。
一、MySQL測(cè)試數(shù)據(jù)
新建表Category(商品分類)和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 ('書籍');
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的方法見 JAVA入門[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)的類。

三、自定義mybatis關(guān)聯(lián)查詢
1.封裝實(shí)體dto
我們新定義CategoryDto,封裝商品分類信息及其商品列表。
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ù)類型為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:
完整參考官網(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ī)蘭黛
|_歐萊雅
|_韓后
|_相宜本草
分享標(biāo)題:Mybatis多表關(guān)聯(lián)查詢的實(shí)現(xiàn)(DEMO)
文章地址:http://www.jbt999.com/article10/psiogo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、響應(yīng)式網(wǎng)站、全網(wǎng)營(yíng)銷推廣、、移動(dòng)網(wǎng)站建設(shè)、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)