概要

本节要实现的是多表关联查询的简单demo。场景是根据id查询某商品分类信息,并展示该分类下的商品列表。
一、Mysql测试数据
新建表Category(商品分类)和Product(商品),并插入几条测试数据。
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节点。
<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关联查询
1.封装实体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节点,id对应上面的方法名getById;parameterType参数类型为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>
接下来定义resultMap节点id为CategoryResult,type为CategoryDto。
关于resultMap:
完整参考官网:http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#Result_Maps
用association对应category,collection对应products,然后用result对应到每个具体字段。
<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>
四、测试
在上一节测试基础上新增测试方法:
@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节点就可以了。
<resultMap id="CategoryResult" type="com.data.dto.CategoryDto"> <id property="id" column="CateId"></id> …… </resultMap>
运行结果:
商品id=2 name=美妆
Products:6
|_膜法世家
|_御泥坊
|_雅诗兰黛
|_欧莱雅
|_韩后
|_相宜本草
# mybatis
# 多表关联查询
# ybatis
# 关联查询
# 关于QueryWrapper
# 实现MybatisPlus多表关联查询方式
# Mybatis 一对多和多对一关联查询问题
# mybatis-plus多表关联查询功能的实现
# mybatis如何使用注解实现一对多关联查询
# MyBatis实践之动态SQL及关联查询
# MyBatis 三表外关联查询的实现(用户、角色、权限)
# Mybatis关联查询之一对多和多对一XML配置详解
# Mybatis实现一对一、一对多关联查询的方法(示例详解)
# Mybatis-Plus多表关联查询的使用案例解析
# MyBatis的关联查询实现(一对一、一对多、多对多)
# 本草
# 的是
# 自定义
# 分类信息
# 自动生成
# 雅诗兰黛
# 测试数据
# 基础上
# 欧莱
# 错了
# 不存在
# 在上
# 可以帮助
# 几条
# 核心技术
# 商品分类
# 官网
# 就可以
# 本节
# 应到
相关文章:
建站之星安装后界面空白如何解决?
如何正确下载安装西数主机建站助手?
建站之星如何快速生成多端适配网站?
小捣蛋自助建站系统:数据分析与安全设置双核驱动网站优化
如何在搬瓦工VPS快速搭建网站?
制作网站的软件下载免费,今日头条开宝箱老是需要下载怎么回事?
代购小票制作网站有哪些,购物小票的简要说明?
重庆网站制作公司哪家好,重庆中考招生办官方网站?
如何快速生成高效建站系统源代码?
如何做网站制作流程,*游戏网站怎么搭建?
如何配置WinSCP新建站点的密钥验证步骤?
如何在Windows服务器上快速搭建网站?
高端智能建站公司优选:品牌定制与SEO优化一站式服务
如何快速搭建虚拟主机网站?新手必看指南
专业网站设计制作公司,如何制作一个企业网站,建设网站的基本步骤有哪些?
定制建站流程解析:需求评估与SEO优化功能开发指南
如何撰写建站申请书?关键要点有哪些?
建站之星如何助力企业快速打造五合一网站?
c# Task.Yield 的作用是什么 它和Task.Delay(1)有区别吗
广东企业建站网站优化与SEO营销核心策略指南
建站主机选哪种环境更利于SEO优化?
宿州网站制作公司兴策,安徽省低保查询网站?
太平洋网站制作公司,网络用语太平洋是什么意思?
公司门户网站制作流程,华为官网怎么做?
建站之星IIS配置教程:代码生成技巧与站点搭建指南
无锡营销型网站制作公司,无锡网选车牌流程?
攀枝花网站建设,攀枝花营业执照网上怎么年审?
建站之星导航菜单设置与功能模块配置全攻略
青浦网站制作公司有哪些,苹果官网发货地是哪里?
常州企业建站如何选择最佳模板?
东莞专业制作网站的公司,东莞大学生网的网址是什么?
寿县云建站:智能SEO优化与多行业模板快速上线指南
建站之星2.7模板:企业网站建设与h5定制设计专题
佛山网站制作系统,佛山企业变更地址网上办理步骤?
建站之星下载版如何获取与安装?
平台云上自主建站:模板化设计与智能工具打造高效网站
如何在自有机房高效搭建专业网站?
如何通过IIS搭建网站并配置访问权限?
Swift中swift中的switch 语句
家具网站制作软件,家具厂怎么跑业务?
如何用y主机助手快速搭建网站?
如何选择长沙网站建站模板?H5响应式与品牌定制哪个更优?
如何快速查询网址的建站时间与历史轨迹?
如何在建站主机中优化服务器配置?
网站制作费用多少钱,一个网站的运营,需要哪些费用?
建站主机如何选?性能与价格怎样平衡?
香港网站服务器数量如何影响SEO优化效果?
如何快速辨别茅台真假?关键步骤解析
如何通过虚拟主机空间快速建站?
宝塔Windows建站如何避免显示默认IIS页面?
*请认真填写需求信息,我们会在24小时内与您取得联系。