mybatis的一堆多映射使用配置
自己仿站jeep官网在制作商城时,商品详情页面需要带着一个商品的信息,商品的配置,配置对应的颜色,商品的图片 如图
首先设计业务bean
一辆车的信息
业务一对多的大业务bean,继承Car.java
几个list的类型不做赘述,本博客主要讲映射
对应的marrp.xml中设置
创建
<resultMap type="cn.jeep.CarBean.detailsCar" id="oneCar">
<id column="carid" jdbcType="VARCHAR" property="carid" />
<result column="carimg" jdbcType="VARCHAR" property="carimg" />
<result column="carname" jdbcType="VARCHAR" property="carname" />
<result column="cartext" jdbcType="VARCHAR" property="cartext" />
<result column="carzt" jdbcType="INTEGER" property="carzt" />
<!-- 多表 -->
<!-- 配置信息 -->
<collection property="peizhi" ofType="cn.jeep.CarBean.carPeizhi" javaType="java.util.ArrayList" column="carid" select="selectPerizhi">
<id column="zys_carpeizhi_id" jdbcType="VARCHAR" property="carid" />
<result column="pid" jdbcType="VARCHAR" property="pid" />
<result column="pname" jdbcType="VARCHAR" property="pname" />
<result column="pzt" jdbcType="INTEGER" property="pzt" />
</collection>
<!-- 配置图片 -->
<collection property="liimg" ofType="cn.jeep.CarBean.carImg" javaType="java.util.ArrayList" column="carid" select="selectPimg">
<id column="carid" jdbcType="VARCHAR" property="carid" />
<result column="carimg" jdbcType="VARCHAR" property="carimg" />
</collection>
<!-- 详细配置图片 -->
<collection property="liximg" ofType="cn.jeep.CarBean.xCarImg" javaType="java.util.ArrayList" column="carid" select="selectPximg">
<id column="xcarid" jdbcType="VARCHAR" property="xcarid" />
<result column="xcarimg" jdbcType="VARCHAR" property="xcarimg" />
</collection>
</resultMap>
<!-- 多表 -->
<!-- 配置信息 -->
<resultMap id="perizhiMap" type="cn.jeep.CarBean.carPeizhi">
<id column="zys_carpeizhi_id" jdbcType="VARCHAR" property="carid" />
<result column="pid" jdbcType="VARCHAR" property="pid" />
<result column="pname" jdbcType="VARCHAR" property="pname" />
<result column="pzt" jdbcType="INTEGER" property="pzt" />
</resultMap>
<!-- 配置图片 -->
<resultMap id="carImg" type="cn.jeep.CarBean.carImg">
<id column="carid" jdbcType="VARCHAR" property="carid" />
<result column="carimg" jdbcType="VARCHAR" property="carimg" />
</resultMap>
<!-- 详细配置图片 -->
<resultMap id="xcarImg" type="cn.jeep.CarBean.xCarImg">
<id column="xcarid" jdbcType="VARCHAR" property="xcarid" />
<result column="xcarimg" jdbcType="VARCHAR" property="xcarimg" />
</resultMap>
<!-- 第一个配置颜色 -->
<resultMap id="selectPcolor" type="cn.jeep.CarBean.peizhiColor">
<id column="colorid" jdbcType="VARCHAR" property="colorid" />
<result column="colorname" jdbcType="VARCHAR" property="colorname" />
</resultMap>
多次尝试,常规使用的一对多映射不需要写每个bean独立的 resultMap 但是映射过程中打断点发现所有list只能取到每个的第一行数据。
<!-- 封装大型产品详情页的业务bean --> <!-- 测试多字段映射=================================================== -->
<select id="detailsCars" resultMap="oneCar">
SELECT
zys_cark.`carid`,
zys_cark.`carimg`,
zys_cark.`carname`,
zys_cark.`cartext`,
zys_cark.`carzt`
from zys_cark
WHERE zys_cark.`carid`=#{carid}
</select>
<!-- 配置图片 -->
<select id="selectPerizhi" resultMap="perizhiMap">
select * from zys_carpeizhi where zys_carpeizhi.`carid` =#{carid}
</select>
<!-- 详细配置 -->
<select id="selectPimg" resultMap="carImg">
SELECT * FROM zys_carimg WHERE carid=#{#carid}
</select>
<!-- 详细图片 -->
<select id="selectPximg" resultMap="xcarImg">
select * from zys_xcarimg where xcarid=#{carid}
</select> <!-- 查一个汽车配置的颜色 -->
<select id="selectOneColor" resultMap="selectPcolor">
SELECT zys_pcolor.`colorid`,zys_color.`colorname`,zys_pcolor.`czt` FROM zys_carpeizhi JOIN zys_pcolor JOIN zys_color
ON zys_carpeizhi.`pid`=zys_pcolor.`pid` AND zys_color.`colorid`=zys_pcolor.`colorid` WHERE zys_carpeizhi.`pid`=#{pid}
</select>
mybatis的一堆多映射使用配置的更多相关文章
- 深入浅出Mybatis系列八-mapper映射文件配置之select、resultMap
注:本文转载自南轲梦 注:博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之inse ...
- 深入浅出Mybatis系列七-mapper映射文件配置之insert、update、delete
注:本文转载自南轲梦 注:博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 上篇文章<深入浅出Mybatis系列(六)---objectFactory.p ...
- 深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap
上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之insert.update.delete>介绍了insert.update.delete的用法,本篇将介绍select ...
- 深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap good
上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之insert.update.delete>介绍了insert.update.delete的用法,本篇将介绍select ...
- 深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap[转]
上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之insert.update.delete>介绍了insert.update.delete的用法,本篇将介绍select ...
- MyBatis学习之路之configuration配置
1.首先讲解的是MyBatis核心配置文件configuration.xml的配置 一个完整的configuration.xml配置顺序如下: properties,settings,typeAlia ...
- 【转载】Mybatis多参数查询映射
转载地址:http://www.07net01.com/zhishi/402787.html 最近在做一个Mybatis的项目,由于是接触不久,虽然看了一下资料,但在实际开发中还是暴 露了很多问题,其 ...
- 【mybatis深度历险系列】mybatis中的高级映射一对一、一对多、多对多
学习hibernate的时候,小编已经接触多各种映射,mybatis中映射有到底是如何运转的,今天这篇博文,小编主要来简单的介绍一下mybatis中的高级映射,包括一对一.一对多.多对多,希望多有需要 ...
- 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建四:配置springmvc
在用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建三:配置spring并测试的基础上 继续进行springmvc的配置 一:配置完善web.xml文件
随机推荐
- 关于:Express会被Koa2取代吗?
知会上看到有个问题<Express会被Koa2取代吗?>.刚好对Express.koa有点小研究,于是简单回答了一下. 1.先说结论 目前没有看到Express会被koa2取代的迹象. 目 ...
- Native Boot 从一个 VHD 引导系统的相关说明
Native Boot 是 Windows 7 和 Windows Server 2008 R2 提供的一个新的功能,它允许从一个 VHD 文件引导一个操作系统,但是需要注意的是目前的 Windows ...
- 从零开始配置webpack(基于webpack 4 和 babel 7版本)
webpack 核心概念: Entry: 入口 Module:模块,webpack中一切皆是模块 Chunk:代码库,一个chunk由十多个模块组合而成,用于代码合并与分割 Loader:模块转换器, ...
- SVN 部署(基于 Linux)
1.通过 yum 命令安装 svnserve,命令如下: # 此命令会全自动安装svn服务器相关服务和依赖,安装完成会自动停止命令运行 yum -y install subversion # 若需查看 ...
- 一个简单的wed服务器SHTTPD(8)———— URI分析
//start from the very beginning,and to create greatness //@author: Chuangwei Lin //@E-mail:979951191 ...
- eclipse手动添加SVN插件
最近使用eclipse时,用help下自动下载更新svn总是出错,网上找到手动安装方法,记录下一种可行的 1.手动下载svn插件(百度SVNsite-1.8.18) 2.将下载好的SVNsite-1. ...
- 最长公共子序列(Longest common subsequence)
问题描述: 给定两个序列 X=<x1, x2, ..., xm>, Y<y1, y2, ..., yn>,求X和Y长度最长的公共子序列.(子序列中的字符不要求连续) 这道题可以 ...
- Java——单双引号的区别
单引号: 单引号包括的是单个字符,表示的是char类型.例如: char a='1' 双引号: 双引号可以包括0个或者多个字符,表示的是String类型. 例如: String s="ab ...
- C. Yet Another Counting Problem(循环节规律)
\(给出a,b,l,r,求在区间[l,r]内有多少x满足x%a%b!=x%b%a\) \(--------------------分割!!~----------------------------\) ...
- 看直播 csust oj
看直播 Description 小明喜欢看直播,他订阅了很多主播,主播们有固定的直播时间 [Li, Ri] . 可是他网速只有2M,不能同时播放两个直播,所以同一时间只能看一个直播. 并且他只会去看能 ...