static findanotation(Method method, Class annotationType) Method:
通过传入AnnotatedElement和注解类型来查找方法或者类对象上的注解
参数method是获取调用类的方法和annotation Type注解Class,例如下部分程序
this.persistentClass = persistentClass;
Table table = AnnotationUtils.findAnnotation(persistentClass,
Table.class);
if (table == null) {
throw new DaoException(persistentClass.getName() + "没有定义@table");
}
this.tableName = table == null ? persistentClass.getName() : table
.name();
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(persistentClass);
} catch (IntrospectionException e) {
throw new DaoException(e);
}
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor pd : pds) {
Id id = AnnotationUtils
.findAnnotation(pd.getReadMethod(), Id.class);
if (id != null) {
Column idColumn = AnnotationUtils.findAnnotation(
pd.getReadMethod(), Column.class);
if (idColumn != null) {
pk = idColumn.name();
} else {
pk = pd.getName();
}
GeneratedValue gv = AnnotationUtils.findAnnotation(
pd.getReadMethod(), GeneratedValue.class);
if (gv == null) {
strategy = GenerationType.IDENTITY;
} else {
strategy = gv.strategy();
}
}
Column column = AnnotationUtils.findAnnotation(pd.getReadMethod(),
Column.class);
property2ColumnMap.put(pd.getName(), column == null ? pd.getName()
: column.name());
column2PropertyMap
.put(column == null ? pd.getName() : column.name(),
pd.getName());
Transient transient_ = AnnotationUtils.findAnnotation(
pd.getReadMethod(), Transient.class);
if (transient_ != null) {
transientPropertys.add(pd.getName());
}
}
if ("".equals(this.getPk())) {
throw new DaoException(persistentClass.getName()
+ "中没有在get方法上定义@Id");
}
package com.spring.server.entity;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* TbWords generated by hbm2java
*/
@Entity
@Table(name = "tb_words")
public class TbWords implements java.io.Serializable {
private Long twId;
private String twCode;
private String twName;
private String twValue;
private Date twAddDate;
private Long twAddPerson;
private Integer twStatus;
public TbWords() {
}
public TbWords(String twCode, String twName, Integer twStatus) {
this.twCode = twCode;
this.twName = twName;
this.twStatus = twStatus;
}
public TbWords(String twCode, String twName, String twValue, Date twAddDate, Long twAddPerson, Integer twStatus) {
this.twCode = twCode;
this.twName = twName;
this.twValue = twValue;
this.twAddDate = twAddDate;
this.twAddPerson = twAddPerson;
this.twStatus = twStatus;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "tw_id", unique = true, nullable = false)
public Long getTwId() {
return this.twId;
}
public void setTwId(Long twId) {
this.twId = twId;
}
@Column(name = "tw_code", nullable = false, length = 10)
public String getTwCode() {
return this.twCode;
}
public void setTwCode(String twCode) {
this.twCode = twCode;
}
@Column(name = "tw_name", nullable = false, length = 50)
public String getTwName() {
return this.twName;
}
public void setTwName(String twName) {
this.twName = twName;
}
@Column(name = "tw_value", length = 10)
public String getTwValue() {
return this.twValue;
}
public void setTwValue(String twValue) {
this.twValue = twValue;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "tw_add_date", length = 19)
public Date getTwAddDate() {
return this.twAddDate;
}
public void setTwAddDate(Date twAddDate) {
this.twAddDate = twAddDate;
}
@Column(name = "tw_add_person")
public Long getTwAddPerson() {
return this.twAddPerson;
}
public void setTwAddPerson(Long twAddPerson) {
this.twAddPerson = twAddPerson;
}
@Column(name = "tw_status", nullable = false)
public Integer getTwStatus() {
return this.twStatus;
}
public void setTwStatus(Integer twStatus) {
this.twStatus = twStatus;
}
}