当前位置: 首页 > news >正文

天津网站建设揭秘登录百度

天津网站建设揭秘,登录百度,wordpress 二级导航制作,iis .net 网站架设概要 本节要实现的是多表关联查询的简单demo。场景是根据id查询某商品分类信息,并展示该分类下的商品列表。 一、Mysql测试数据 新建表Category(商品分类)和Product(商品),并插入几条测试数据。 create table Category ( Id int n…

概要

本节要实现的是多表关联查询的简单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);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,自动生成相关的类。

Image(36)

三、自定义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 ProductNameFROM Category,ProductWHERE Category.Id=Product.CategoryId AND Category.Id=#{id}</select>

接下来定义resultMap节点id为CategoryResult,type为CategoryDto。

关于resultMap:

  • id – 一个 ID 结果;标记结果作为 ID 可以帮助提高整体效能
  • result – 注入到字段或 JavaBean 属性的普通结果
  • association – 一个复杂的类型关联;许多结果将包成这种类型
    • 嵌入结果映射 – 结果映射自身的关联,或者参考一个
  • collection – 复杂类型的集
    • 嵌入结果映射 – 结果映射自身的集,或者参考一个

完整参考官网: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>

 

四、测试

在上一节测试基础上新增测试方法:

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

    |_膜法世家

    |_御泥坊

    |_雅诗兰黛

    |_欧莱雅

    |_韩后

    |_相宜本草

源码地址:http://pan.baidu.com/s/1eScI7z8

http://www.lbrq.cn/news/2491435.html

相关文章:

  • 网站怎样做https百度笔记排名优化
  • 吕梁网站建设seo和sem
  • 郑州的网站建设公司哪家好百度关键词点击价格查询
  • 南宁网站排名优化电话免费网站注册免费创建网站
  • 网站建设在线商城seo优化人员
  • 龙华网站制作公司如何开展网络营销活动
  • 请列出页面上影响网站排名的因素网站开发建站
  • 中国建设网银seo做得比较好的企业案例
  • 成都旅游网站建设规划方案找客户资源的软件
  • 开网站的宣传图片怎么做sem和seo哪个工作好
  • 怎样做网站管理与维护广州seo优化公司排名
  • 企业网站建设实训建议网站产品推广
  • 网站架构 seo网络广告策划书
  • 租服务器做网站简述影响关键词优化的因素
  • 漕泾网站建设培训机构seo
  • 湘潭网站建设建站海外独立站
  • 设计网站免费的今日冯站长之家
  • wordpress 插件制作教程全网营销与seo
  • 做网站 想做成宽屏的自己建网站流程
  • 专做定制网站建设seo营销专员
  • 无需登录网页小游戏网站seo推广主要做什么
  • 临海网站建设关键字优化
  • 网站建设公司怎么发展seo关键词推广渠道
  • 企业网站建设的过程企业网站推广外包
  • 哪个网站可以做申论真题html家乡网站设计
  • 安庆商城网站开发微信管理系统软件
  • wordpress 采集 公众号seo是什么软件
  • 做刀模线网站谷歌浏览器在线入口
  • 做网站的网络非要专线吗汕头网站建设平台
  • 网站添加新闻栏怎么做seo推广方式是什么呢
  • git stash 命令详解
  • 中级全栈工程师笔试题
  • mac配置多版本jdk
  • 影刀RPA_初级课程_玩转影刀自动化_网页操作自动化
  • LeetCode 1074:元素和为目标值的子矩阵数量
  • sqli-labs通关笔记-第21关 字符型Header-Cookie SQL注入(单引号括号闭合 base64编码绕过 手工注入+脚本注入两种方法)