二、注解Annotation:
1. 实体类:@Entity @Table(name="user")
2. 主键:@Id @GeneratedValue
n @GeneratedValue里面可以放两个属性
generator | |
strategy |
AUTO 自动生成,是默认选项,MySQL自动生成 |
IDENTITY 自己指定主键 |
SEQUENCE |
TABLE |
3. 字段:@Column(length=10, nullable=false, name="XXX")
n 定义长度,不允许为空,自定义数据库字段名称
4. 日期:@Temporal(TemporalType.DATE)
n DATE(日期), TIME(时间), TIMESTAMP(日期+时间)
n private Date birthday;
5. 枚举(enum):@Enumerated(EnumType.STRING)
n @Column(length=5,nullable=false)
n EnumType.ORDINAL枚举的序列(从0开始)放到数据库中是序列数字,EnumType.STRING字符串值
n 设置默认值:private Gender gender = Gender.MAN;
6. 大文本:@Lob public String getInfo(){return info;}
n 用字符串生成的最大长度是255,这时就要用到Lob,
n 在mysql中是LONGTEXT类型,
7. 文件:@Lob @Basic(fetch=FetchType.LAZY)
n public Byte[] getFile(){return file;}
n 二进制数据,在mysql中LONGBLOB,从文件中读到字节数据,然后放到数据库。当加载时,我们延迟加载,当需要时再加载这个文件,这样就不会在不需要文件时占用内存。但在访问时EntityManager,session必需是打开的,不需就会有错误。
8. 隐藏字段:@Transient属性不成为持久化字段(数据库中不生成对应字段)
package cn.itcast.bean.user;
import java.util.Date;
import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Lob; import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.persistence.Transient;
@Entity public class Person { private Integer id; private String name; private Date birthday; private Gender gender = Gender.MAN; private String info; private Byte[] file; private String imagepath; @Id @GeneratedValue public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(length=30, nullable=false) public String getName() { return name; } public void setName(String name) { this.name = name; } @Temporal(TemporalType.DATE) public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Column(length=5, nullable=false) public Gender getGender() { return gender; } public void setGender(Gender gender) { this.gender = gender; } @Lob public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } @Lob @Basic(fetch=FetchType.LAZY) //由于我们在加载Person的时间必不立即加载文件,所以选择延迟加载 public Byte[] getFile() { return file; } public void setFile(Byte[] file) { this.file = file; } @Transient //该属性就不会出现在数据库字段中 public String getImagepath() { return imagepath; } public void setImagepath(String imagepath) { this.imagepath = imagepath; } }
|
package cn.itcast.bean.user; import java.util.Date; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Enumerated; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import javax.persistence.Temporal; import javax.persistence.TemporalType;
@Entity public class Buyer { /** 用户名 **/ private String username;//只允许字母/数字/下划线 /** 密码 **/ private String password;//采用MD5加密 /** 真实姓名 **/ private String realname; /** 电子邮箱 **/ private String email; /** 性别 **/ private Gender gender=Gender.MAN; /** 联系信息 **/ private ContactInfo contactInfo; /** 是否启用 **/ private Boolean visible=true; /** 注册时间 **/ private Date regTime = new Date();
public Buyer() { } public Buyer(String username) { this.username = username; } public Buyer(String username, String password, String email) { this.username = username; this.password = password; this.email = email; } @Id @Column(length=20) public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Column(length=32, nullable=false) public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Column(length=10) public String getRealname() { return realname; } public void setRealname(String realname) { this.realname = realname; } @Column(length=40,nullable=false) public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Enumerated(EnumType.STRING) @Column(length=5,nullable=false) public Gender getGender() { return gender; } public void setGender(Gender gender) { this.gender = gender; }
@OneToOne(cascade=CascadeType.ALL) @JoinColumn(name="contact_id") public ContactInfo getContactInfo() { return contactInfo; } public void setContactInfo(ContactInfo contactInfo) { this.contactInfo = contactInfo; } @Column(nullable=false) public Boolean getVisible() { return visible; } public void setVisible(Boolean visible) { this.visible = visible; } @Column(nullable=false) @Temporal(TemporalType.TIMESTAMP) public Date getRegTime() { return regTime; } public void setRegTime(Date regTime) { this.regTime = regTime; } } |