Two mapping methods between database tables and entity classes

1. Annotation configuration

Use annotations to correspond entity classes to database tables, such as surfaces, column names, and many-to-one correspondence

@Entity
public class Product {
	// primary key ID
	@Id
	private Long productId;
	// Product name
	private String productName;
	// Product Description
	private String productDesc;
	// sketch
	private String imgAddr;
	// original price
	private String normalPrice;
	// current price (promotion price)
	private String promotionPrice;
	// Weight, the bigger the weight, the higher it will be displayed
	private Integer priority;
	// create time
	private Date createTime;
	// last update time
	private Date lastEditTime;
	// 0. Off the shelf 1. Display on the front-end display system
	private Integer enableStatus;

	// A list of picture details, which has a many-to-one relationship with the product
	//Commodities are annotated with OneToMany as one side
	@OneToMany //Specify a one-to-many relationship
	@Cascade(value={CascadeType.SAVE_UPDATE}) //Set the cascade relationship
	@JoinColumn(name="productImgId")  
	private List<ProductImg> productImgList;
	// Product category, a product belongs to only one product category
	@ManyToOne()
	@JoinColumn(name = "productCategoryId")
	private ProductCategory productCategory;
	// Store entity class, indicating which store the product belongs to
	@ManyToOne()
	@JoinColumn(name = "shop_id")
	private Shop shop;

2. Using xml configuration file

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

Both methods have their pros and cons

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324848397&siteId=291194637