您的位置:首页 > 编程语言 > Java开发

Spring AnnotationUtils注解公共Util

2018-01-18 16:11 162 查看

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);//获标有Id注解的方法
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());//{code=code, createTime=create_time, class=class, account=account}
column2PropertyMap
.put(column == null ? pd.getName() : column.name(),
pd.getName());//{code=code, create_time=createTime, class=class, account=account}
Transient transient_ = AnnotationUtils.findAnnotation(
pd.getReadMethod(), Transient.class);//transient临时字段 不对其进行解析操作
if (transient_ != null) {
transientPropertys.add(pd.getName());
}
}
if ("".equals(this.getPk())) {
throw new DaoException(persistentClass.getName()
+ "中没有在get方法上定义@Id");
}


package com.spring.server.entity;
// Generated 2015-8-31 15:24:09 by Hibernate Tools 4.3.1

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

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: