概要
本节要实现的是多表关联查询的简单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自关联查询一对多查询的实现示例,包括了Mybatis自关联查询一对多查询的实现示例的使用技巧和注意事项,需要的朋友参考一下 注:代码已托管在GitHub上,地址是:https://github.com/Damaer/Mybatis-Learning ,项目是mybatis-13-oneself-one2many,需要自取,需要配置maven环境以及mysql环境(sql
主要内容:示例实际应用中,由于多对多的关系比较复杂,会增加理解和关联的复杂度,所以应用较少。MyBatis 没有实现多对多级联,推荐通过两个一对多级联替换多对多级联,以降低关系的复杂度,简化程序。 例如,一个订单可以有多种商品,一种商品可以对应多个订单,订单与商品就是多对多的级联关系。可以使用一个中间表(订单记录表)将多对多级联转换成两个一对多的关系。 示例 下面以订单和商品(实现“查询所有订单以及每个订单对应
主要内容:示例,分步查询,单步查询在《 MyBatis一对一关联查询》一节中介绍了 MyBatis 如何处理一对一级联关系。但在实际生活中也有许多一对多级联关系,例如一个用户可以有多个订单,而一个订单只属于一个用户。同样,国家和城市也属于一对多级联关系。 在 MyBatis 中,通过 <resultMap> 元素的子元素 <collection> 处理一对多级联关系,collection 可以将关联查询的多条记录映射到一个 lis
级联关系是一个数据库实体的概念,有 3 种级联关系,分别是一对一级联、一对多级联以及多对多级联。例如,一个角色可以分配给多个用户,也可以只分配给一个用户。大部分场景下,我们都需要获取角色信息和用户信息,所以会经常遇见以下 SQL。 在级联中存在 3 种对应关系。 一对多的关系,如角色和用户的关系。通俗的理解就是,一家软件公司会存在许多软件工程师,公司和软件工程师就是一对多的关系。 一对一的关系。每
本文向大家介绍Django自关联实现多级联动查询实例,包括了Django自关联实现多级联动查询实例的使用技巧和注意事项,需要的朋友参考一下 1 问题引出 我们在开发网站的时候可能会遇到这种情况,多个字段之间有一定的关联性,比如省市县,选择省,之后下一个选择框的值则为该省的市集合,选择市之后下一个选择框的值为该市的县集合。 一种实现方式是,建立三个模型表,用外键一对多方式,显然这样是不太合理的。Dj
主要内容:示例,分步查询,单步查询一对一级联关系在现实生活中是十分常见的,例如一个大学生只有一个学号,一个学号只属于一个学生。同样,人与身份证也是一对一的级联关系。 在 MyBatis 中,通过 <resultMap> 元素的子元素 <association> 处理一对一级联关系。示例代码如下。 在 <association> 元素中通常使用以下属性。 property:指定映射到实体类的对象属性。 column:指定表中对应的字
本文向大家介绍MyBatis实践之动态SQL及关联查询,包括了MyBatis实践之动态SQL及关联查询的使用技巧和注意事项,需要的朋友参考一下 序言 MyBatis,大家都知道,半自动的ORM框架,原来叫ibatis,后来好像是10年apache软件基金组织把它托管给了goole code,就重新命名了MyBatis,功能相对以前更强大了。它相对全自动的持久层框架Hibernate,更加灵活,更轻
本文向大家介绍Yii2 ActiveRecord多表关联及多表关联搜索的实现,包括了Yii2 ActiveRecord多表关联及多表关联搜索的实现的使用技巧和注意事项,需要的朋友参考一下 Yii的ActiveRecord是与数据库打交道的类,也即MVC中的M(模型层),也是ORM的O(Object)。 一个老生常谈的问题。最近通过群里的反馈,觉得很多人还是没有去理解这个问题。今天把这个问题讲明白了