Spring AnnotationUtils注解公共Util

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

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值