1.使用一对多,取到两表之前或多表之间的值
(1)第一步实体类(三表关系)
(a)BigType.java
//商品分类--大
public class BigType {
private int id;//编号
private String name;//类型名称
private String remarks;//别名
//一对多--one端
private Set<SmallType> smallTypes = new HashSet<SmallType>();
private Set<Product> products = new HashSet<Product>();
//封装
}
(b)SmallType.java
//商品分类--小
public class SmallType {
private int id;//编号
private String name;//类型名称
private String remarks;//别名
private int bigTypeId;//商品类型ID-大
private BigType bigType;//many端
private Set<Product> products = new HashSet<Product>();
//封装
}
(c)Product.java
public class Product {
private int id;//编号
private String description;//商品详情
private int hot;//热门推荐,1为热门,0不是热门
private Date hotTime;//热门时间
private String name;//商品名称
private int price;//商品价格
private String proPic;//商品图片
private int specialPrice;//今日特价,1为特价,0不是特价
private Date specialPriceTime;//特价时间
private int stock;//库存
private int bigTypeId;//商品类型id-大
private int smallTypeId;//商品类型id-小
private SmallType smallType;
private BigType bigType;
//封装
}
(2)第二步在mapper.xml文件中填写
(a)BigTypeMapper.xml文件
<resultMap type="BigType" id="bigTypeList">
<!-- 双向必须配置该表主键 不然显示不出 -->
<result property="id" column="id"/>
<collection property="smallTypes" column="id" select="cn.jbit.dao.SmallTypeDao.getSmallTypeByBigTypeId"></collection>
<collection property="products" column="id" select="cn.jbit.dao.ProductDao.getBigTypeByBigTypeId"></collection>
</resultMap>
<!-- 双向一定都要写查询语句 -->
<select id="getBigTypeById" resultMap="bigTypeList">
select * from t_bigtype where id = #{id}
</select>
(b)SmallTypeMapper.xml 文件
<resultMap type="SmallType" id="smallTypeList">
<association property="bigType" column="bigTypeId" select="cn.jbit.dao.BigTypeDao.getBigTypeById"></association>
<!-- 多对一的关联关联复杂类型association column="bigTypeId"外键 -->
</resultMap>
<resultMap type="SmallType" id="smList">
<!-- 双向必须配置该表主键 不然显示不出 -->
<result property="id" column="id"/>
<collection property="products" column="id" select="cn.jbit.dao.ProductDao.getProductBySmallTypeId"></collection>
</resultMap>
<!-- 双向一定都要写查询语句 -->
<select id="getSmallTypeByBigTypeId" resultMap="smallTypeList">
select * from t_smalltype where bigTypeId=#{bigTypeId}
</select>
<select id="getSmallTypeById" resultMap="smList">
select * from t_smalltype where id=#{id}
</select>
(c)ProductMapper.xml文件
<resultMap type="Product" id="productlist">
<association property="smallType" column="smallTypeId" select="cn.jbit.dao.SmallTypeDao.getSmallTypeById"></association>
<association property="bigType" column="bigTypeId" select="cn.jbit.dao.BigTypeDao.getBigTypeById"></association>
<!-- 多对一的关联关联复杂类型association column="bigTypeId"外键 -->
</resultMap>
<select id="getProductBySmallTypeId" resultMap="productlist">
SELECT * FROM t_product WHERE smallTypeId=#{smallTypeId}
</select>
<select id="getBigTypeByBigTypeId" resultMap="productlist">
SELECT * FROM t_product WHERE bigTypeId=#{bigTypeId}
</select>
(3)第三步 cn.jbit.dao的文件中(文件的方法与mapper一一对应)
(a)BigTypeDao.java
public interface BigTypeDao {
BigType getBigTypeById(int id);//一对多商品表或小类表
}
(b)SmallTypeDao.java
public interface SmallTypeDao {
SmallType getSmallTypeByBigTypeId(int bigTypeId);//多对一大类表
SmallType getSmallTypeById(int id);//一对多商品表
}
(c)ProductDao.java
public interface ProductDao {
Product getProductBySmallTypeId(int smallTypeId);
Product getBigTypeByBigTypeId(int bigTypeId);
}
做好这些准备之后就可以在控制器中写方法取数据了!!!