整理一下自己的笔记,关于Annotation注解的一个小案例。
Annotation注解:
1.首先,我们定义annotation,关联数据库每一张表中的字段,数据在读取时自动匹配对应的实体中的属性。这我们就不用在考虑加载数据是表中字段是否与实体中的属性匹配了。
package org.apath.com.common;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/***
* 注解--数据库字段与实体类字段
* @author Dawn
* @author 2010-04-15
*/
@Target({ElementType.METHOD,ElementType.FIELD}) //表示这个注解是放在方法上的
@Retention(RetentionPolicy.RUNTIME) // 表示着个方法是在运行时反射出来
public @interface ColumnAnnotation {
//列名 注解的属性
String columnName();
}
//end annotation
2.下面是一个实体,在实体的每个getter方法上注入我的annotation,标志从这里开始。
package org.apath.com.entity;
import org.apath.com.common.ColumnAnnotation;
/**
* 存放用户基本信息类
* @author Dawn
* @author 2010-04-14
*/
public class UserInfo {
private int userId; //用户Id
private String userName; //真实姓名
private String passWord; //密码
private int departId; //所在部门
private int gender; //性别
private int roleId; //用户角色
private int userStateId; //用户状态
private String face;//用户图像
public String getFace() {
return face;
}
@ColumnAnnotation(columnName="face")
public void setFace(String face) {
this.face = face;
}
public int getUserStateId() {
return userStateId;
}
@ColumnAnnot