- 针对上一篇文章,使用xml映射文件和反射实现dao,提出了扩展功能,利用注解来体现实体类和表的映射关系
- 本文是上一篇文章的扩展
- 使用反射和xml实现dao
1. 注解的使用
@Override
public String toString() {
return super.toString();
}
- @Override 就是一个注解,表示此方法是重写了父类的方法,此外还有许多Java里面的内置注解
- 元注解(Retention, ElementType,Documented,Inherited)
- 常用的两个元注解 (Retention, ElementType)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
package com.lovely.test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
public @interface MyAnnotation {
String name();
}
2. 使用注解体现映射关系
package com.lovely.base;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Table {
String name();
}
package com.lovely.base;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Id {
String idName();
String idColumn();
String seqName();
}
package com.lovely.base;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Column {
String columnName();
}
package com.lovely.entity;
import java.sql.Date;
import com.lovely.base.Column;
import com.lovely.base.Id;
import com.lovely.base.Table;
@Table(name="goods_info")
public class Goods {
@Id(idName="gid", idColumn="goods_id", seqName="goods_seq")
private Integer gid;
@Column(columnName="goods_name")
private String gname;
@Column(columnName="goods_price")
private Double gprice;
@Column(columnName="goods_date")
private Date gdate;
@Column(columnName="goods_factory")
private String gfactory;
public static HashMap<String, MapperData> map = new HashMap<String, MapperData>();
static {
try {
Class<?> baseDaoClass = Class.forName("com.lovely.dao.BaseDao");
String filePath = baseDaoClass.getResource("/com/lovely/entity/").getFile();
File dir = new File(filePath);
File[] files = dir.listFiles();
for (File file : files) {
String className = "com.lovely.entity." + file.getName().substring(0, file.getName().indexOf("."));
Class<?> entityClass = Class.forName(className);
Table table = entityClass.getAnnotation(Table.class);
if (table != null) {
MapperData mapperData = new MapperData();
mapperData.setClassName(className);
mapperData.setTableName(table.name());
Field[] fields = entityClass.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Id id = fields[i].getAnnotation(Id.class);
if (id != null) {
MapperId mapperId = new MapperId();
mapperId.setIdName(id.idName());
mapperId.setIdColumn(id.idColumn());
mapperId.setSeqName(id.seqName());
mapperData.setMapperId(mapperId);
}
Column column = fields[i].getAnnotation(Column.class);
if (column != null) {
mapperData.getProperties().put(fields[i].getName(), column.columnName());
}
}
map.put(className, mapperData);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}