1.注解配置
采用注解的方式将实体类与数据库表相对应,例如表面,列名,多对一的对应关系
@Entity
public class Product {
// 主键ID
@Id
private Long productId;
// 商品名
private String productName;
// 商品简介
private String productDesc;
// 简略图
private String imgAddr;
// 原价
private String normalPrice;
// 现价(推广价格)
private String promotionPrice;
// 权重,越大越排前显示
private Integer priority;
// 创建时间
private Date createTime;
// 最近一次的更新时间
private Date lastEditTime;
// 0.下架 1.在前端展示系统展示
private Integer enableStatus;
// 图片详情图列表,跟商品是多对一的关系
//商品作为一方使用OneToMany注解
@OneToMany //指定一对多关系
@Cascade(value={CascadeType.SAVE_UPDATE}) //设定级联关系
@JoinColumn(name="productImgId")
private List<ProductImg> productImgList;
// 商品类别,一件商品仅属于一个商品类别
@ManyToOne()
@JoinColumn(name = "productCategoryId")
private ProductCategory productCategory;
// 店铺实体类,标明商品属于哪个店铺
@ManyToOne()
@JoinColumn(name = "shop_id")
private Shop shop;
2. 采用xml配置文件的方式
<mapper namespace="com.xxx.xxxx.ProductDao">
<resultMap id="productMap" type="com.imooc.o2o.entity.Product">
<id column="product_id" property="productId" />
<result column="product_name" property="productName" />
<result column="product_desc" property="productDesc" />
<result column="img_addr" property="imgAddr" />
<result column="normal_price" property="normalPrice" />
<result column="promotion_price" property="promotionPrice" />
<result column="priority" property="priority" />
<result column="create_time" property="createTime" />
<result column="last_edit_time" property="lastEditTime" />
<result column="enable_status" property="enableStatus" />
<association property="productCategory" column="product_category_id"
javaType="com.light.entity.ProductCategory">
<id column="product_category_id" property="productCategoryId" />
<result column="product_category_name" property="productCategoryName" />
</association>
<association property="shop" column="shop_id"
javaType="com.light.entity.Shop">
<id column="shop_id" property="shopId" />
<result column="owner_id" property="ownerId" />
<result column="shop_name" property="shopName" />
</association>
<collection property="productImgList" column="product_id"
ofType="com.light.entity.ProductImg">
<id column="product_img_id" property="productImgId" />
<result column="detail_img" property="imgAddr" />
<result column="img_desc" property="imgDesc" />
<result column="priority" property="priority" />
<result column="create_time" property="createTime" />
<result column="product_id" property="productId" />
</collection>
</resultMap>