声明自定义注解@Table
1、声明自定义注解@Table
(1)加上String类型的配置参数value
(2)并限定@Table的使用位置为类上
(3)并指定生命周期为“运行时”
@Target(ElementType.TYPE)//并限定@Table的使用位置为类上
@Retention(RetentionPolicy.RUNTIME)//并指定生命周期为“运行时”
public @interface Table {
String value();//加上String类型的配置参数value
}
2、声明自定义注解@Column
(1)加上String类型的配置参数name,表示表格的列名
(2)加上String类型的配置参数type,表示表格的列数据类型
(3)并限定@Column的使用位置在属性上
(4)并指定生命周期为“运行时”
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
String name();
String type();
}
3、声明User类,
(1)属性:id, username, password, email
(2)在User类上,标记@Table注解,并为value赋值为"t_user"
(3)在User类的每一个属性上标记@Column,并为name和type赋值,例如:
id:name赋值为no,type赋值为int
username:name赋值为username,type赋值为varchar(20)
password:name赋值为pwd,type赋值为char(6)
email:name赋值为email,type赋值为varchar(50)
@Table("t_user")
public class User {
@Column(name="no",type="int")
private int id;
@Column(name="username",type="varchar(20)")
private String username;
@Column(name="pwd",type="char(6)")
private String password;
@Column(name="email",type="varchar(50)")
private String email;
public User(int id, String username, String password, String email) {
super();
this.id = id;
this.username = username;
this.password = password;
this.email = email;
}
public User() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email + "]";
}
}