自动生成默认的数据表
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* @ClassName OrderDetail
* @Description
* @Author Sky1in3
* @Date 2019/1/11 2:11
* @Version 1.0
*/
@Entity
@Data
public class Order {
/** 订单详情id */
private String detailId;
/** 订单id */
private String orderId;
/** 课程id */
private String productId;
/** 课程名 */
private String productName;
/** 课程售价 */
private Double productPrice;
/** 课程库存 */
private Integer productQuantity;
/** 课程图标 */
private String productIcon;
}
以类名、字段名为表的表名和列名来自动生成数据表。
自定义的方式生成数据表
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* @ClassName OrderDetail
* @Description
* @Author Sky1in3
* @Date 2019/1/11 2:11
* @Version 1.0
*/
@Entity
@Table(name = "order_detail")
public class OrderDetail {
/** 订单详情id */
private String detailId;
/** 订单id */
private String orderId;
/** 课程id */
private String productId;
/** 课程名 */
private String productName;
/** 课程售价 */
private Double productPrice;
/** 课程库存 */
private Integer productQuantity;
/** 课程图标 */
private String productIcon;
@Id
@Column(name = "detail_id", length = 32)
public String getDetailId() {
return detailId;
}
public void setDetailId(String detailId) {
this.detailId = detailId;
}
@Column(name = "order_id")
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
@Column(name = "product_id")
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
@Column(name = "product_name")
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
@Column(name = "product_price")
public Double getProductPrice() {
return productPrice;
}
public void setProductPrice(Double productPrice) {
this.productPrice = productPrice;
}
@Column(name = "product_quantity")
public Integer getProductQuantity() {
return productQuantity;
}
public void setProductQuantity(Integer productQuantity) {
this.productQuantity = productQuantity;
}
@Column(name = "product_icon")
public String getProductIcon() {
return productIcon;
}
public void setProductIcon(String productIcon) {
this.productIcon = productIcon;
}
}
这种方式可以自定义表名和列名。
注:使用@Table注解必须要在字段的get()方法上面加上@Column,不然生成失败;
如果启动项目的时候报一下异常。
Specified key was too long; max key length is 767 bytes
在主键上声明一下长度就可以解决。
例如:
@Id
@Column(name = "detail_id", length = 32)
public String getDetailId() {
return detailId;
}
网友评论