自己仿站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>