- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Mybatis多表关联查询的实现(DEMO)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
概要 。
本节要实现的是多表关联查询的简单demo。场景是根据id查询某商品分类信息,并展示该分类下的商品列表.
1、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核心技术'
);
|
2、配置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,自动生成相关的类.
3、自定义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:
完整参考官网: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>
|
4、测试 。
在上一节测试基础上新增测试方法:
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 。
最后此篇关于Mybatis多表关联查询的实现(DEMO)的文章就讲到这里了,如果你想了解更多关于Mybatis多表关联查询的实现(DEMO)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
就在我放松下来以为我对数组上下文中的指针有一个公平的理解时,我对下面的程序再次感到沮丧。我已经理解了数组 arr,arr 和 &arr 大小相同,但类型不同,但我无法牢牢掌握以下程序的输出。我尝试将其
这个问题在这里已经有了答案: Why whole structure can not be compared in C, yet it can be copied? (6 个答案) 关闭 9 年前。
今天我试图在我的代码中使用系统属性。当我输入 ./gradlew -Dorg.gradle.project.env=demo test 时,虽然我在脚本中成功打印了 env ,但发生了 NullPoi
概要 本节要实现的是多表关联查询的简单demo。场景是根据id查询某商品分类信息,并展示该分类下的商品列表。 1、Mysql测试数据 新建表Category(商品分类)和Product(商品
SpringCloud + Docker 的便利和强大真的超乎想象,我已经入坑了…好了,不说废话,记录一个简单的 Demo 供其他同学排坑。 前言 惯例不能丢,先上源代码:docker-demo
在没介绍正文之前先给大家介绍下数据更新方法支持的连贯操作方法有: 在上一篇文章中我们实现了数据的删除和批量删除,这一篇文章我们将实现数据的更新。 首先依然是预期效果图: 点击修
过滤html标签在php中可以有内置的函数了,但它过滤的太干净了,我们就整理了一下些利用正则来过滤指定html标签的例子,具体如下所示。 采集的时候有时候需要过滤掉多余的标签属性,比如 img标签
UIPickerView平常用的地方好像也不是很多,顶多就是一些需要选择的地方,这次项目需要这一个功能,我就单独写了一个简单的demo,效果图如下: 新增主页面弹出view,在主页面添
项目里面有一个需求,要对sql进行简单的语法分析 为了避免sql里面的字符串和注释对语法分析做干扰,我写了一个java函数,对sql进行修剪,删除里面字符串和注释,用空格代替 周末闲着没事,我用g
今天公司里的实习生跑过来问我一般App上广告的无限滚动是怎么实现的,刚好很久没写博客了,就决定写下了,尽量帮助那些处于刚学iOS的程序猿. 做一个小demo,大概实现效果如下图所示: 基本实
最近项目组同事跟我说遇到一个SQL性能问题,他说全表只有69条记录,客户端执行耗费了两分多钟,很不科学。我帮了分析出了原因并得到解决。下面小编安装类似表结构,构造了一个案例,测试截图如下所示:
新的一年又要到来了,各个大的公司又在这年末为大家送上了新春的祝福,支付宝还是延续了这几年的传统,在年末为大家送上了集五福的活动,为了大家能更快更好的扫出来大大的“福”,今天就带领大家利用python
我已经安装了http://download.jboss.org/jbpm/release/6.4.0.Final/jbpm-6.4.0.Final-installer-full.zip在这里找到htt
1、准备环境 服务器:linux(debian)+nginx+mysql+php 环境 框架:laravel 5.5 (确认能跑通) 需求:每天晚上1点执行定时操作 2、定时任务的步骤
下面通过一个小例子来说明cmake编译一个c++项目,生成可执行文件: 整理目录结构: CMake Lists.txt addlib build main.cpp 电脑上没有tree命
本文实例讲述了php实现基于微信公众平台开发SDK(demo)扩展的方法。分享给大家供大家参考。具体分析如下: 该扩展基于官方的微信公众平台SDK,这里只做了简单地封装,实现了一些基本的功能(如天
验证码不用多说,在 WEB-APP 中一般应用于:登录、注册、买某票、秒杀等场景。大家都接触过~可以说是千奇百怪,各式各样。 DEMO 目标功能 验证码页面输入。 页面更换验证码(异
下拉刷新在越来越多的app中使用,已经形成一种默认的用户习惯,遇到列表显示的内容时,用户已经开始习惯性的拉拉。在交互习惯上已经形成定性。之前在我的文章《ios学习笔记34—egotableviewp
一切尽在代码中,代码附有注释,欢迎大家参考。 ?
我试图找到答案,但没有任何结果。bin/console的一些命令和 ./psh.phar没有描述,所以很难理解它们的用途。 Shopware 6 中是否有从数据库中删除所有演示数据的命令? 最佳答案
我是一名优秀的程序员,十分优秀!