在看一些项目的源代码时经常会遇到注释的应用,这里做一些总结。
用一个例子来说明吧:
一、注解使用
使用开源框架LiteOrm来定义一张数据库表:
import com.litesuits.orm.db.annotation.*;
import com.litesuits.orm.db.annotation.PrimaryKey.AssignType;
@Table("address")
public class Address extends BaseModel{
public static final String COL_ID = "_id";
public static final String COL_ADDRESS = "_address";
@PrimaryKey(AssignType.AUTO_INCREMENT)
@Column(COL_ID)
public long id;
@NotNull
@Unique
@Column(COL_ADDRESS)
public String address;
public Address(String address) {
this.address = address;
}
@Override
public String toString() {
return " address:" + address;
}
}
BaseModel声明;
import com.litesuits.orm.db.annotation.Check;
import com.litesuits.orm.db.annotation.Ignore;
public class BaseModel implements Serializable {
@Check("bm NOT NULL")
public String bm = "all models have";
@Ignore
private String ignore = " 这个属性不会出现在数据库里的。因为被标记了ignore";
@Override
public String toString() {
return "BaseModel{" +
"bm='" + bm + '\'' +
'}';
}
}
这张表表名为address,里面有两个字段_id和_address。通过注释实现了建立字段、设定约束等功能,非常简洁明了,剩下的工作都由框架来完成了。
定义表时出现的注解:
@Table("address")
@PrimaryKey(AssignType.AUTO_INCREMENT)
@Column(COL_ID)
@NotNull
@Unique
2、注解的声明
Table.java
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
public String value();
}
PrimaryKey.java
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PrimaryKey {
public AssignType value();
enum AssignType {
BY_MYSELF,
AUTO_INCREMENT
}
}
3. 注解的解析
public static synchronized EntityTable getTable(Class<?> claxx, boolean needPK) {
//...
List<Field> fields = FieldUtil.getAllDeclaredFields(claxx);
//Field[] fields = claxx.getDeclaredFields();
for (Field f : fields) {
if (FieldUtil.isInvalid(f)) {
continue;
}
Property p = new Property();
p.field = f;
// 获取列名,每个属性都有,没有注解默认取属性名
Column col = f.getAnnotation(Column.class);
if (col != null) {
p.column = col.value();
} else {
p.column = f.getName();
}
// 主键判断
//if(Log.isPrint)Log.i(TAG, "PrimaryKey : " + PrimaryKey.class + " field: "+ f + " asst:" + AssignType.AUTO_INCREMENT);
PrimaryKey key = f.getAnnotation(PrimaryKey.class);
if (key != null) {
// 主键不加入属性Map
table.key = new com.litesuits.orm.db.model.PrimaryKey(p, key.value());
// 主键为系统分配,对类型有要求
if (table.key.isAssignedBySystem()) {
if (!FieldUtil.isLong(table.key.field) && !FieldUtil.isInteger(table.key.field)) {
throw new RuntimeException(
PrimaryKey.AssignType.AUTO_INCREMENT
+ "要求主键属性必须是long或者int( the primary key should be long or int...)\n " +
"提示:把你的主键设置为long或int型"
);
}
}
} else {
//ORM handle
//if(Log.isPrint)Log.i(TAG, "Mapping : " + Mapping.class+ " field: "+ f);
Mapping mapping = f.getAnnotation(Mapping.class);
if (mapping != null) {
table.addMapping(new MapProperty(p, mapping.value()));
} else {
table.pmap.put(p.column, p);
}
}
}
return table;
}
用一个例子来说明吧:
一、注解使用
使用开源框架LiteOrm来定义一张数据库表:
import com.litesuits.orm.db.annotation.*;
import com.litesuits.orm.db.annotation.PrimaryKey.AssignType;
@Table("address")
public class Address extends BaseModel{
public static final String COL_ID = "_id";
public static final String COL_ADDRESS = "_address";
@PrimaryKey(AssignType.AUTO_INCREMENT)
@Column(COL_ID)
public long id;
@NotNull
@Unique
@Column(COL_ADDRESS)
public String address;
public Address(String address) {
this.address = address;
}
@Override
public String toString() {
return " address:" + address;
}
}
BaseModel声明;
import com.litesuits.orm.db.annotation.Check;
import com.litesuits.orm.db.annotation.Ignore;
public class BaseModel implements Serializable {
@Check("bm NOT NULL")
public String bm = "all models have";
@Ignore
private String ignore = " 这个属性不会出现在数据库里的。因为被标记了ignore";
@Override
public String toString() {
return "BaseModel{" +
"bm='" + bm + '\'' +
'}';
}
}
这张表表名为address,里面有两个字段_id和_address。通过注释实现了建立字段、设定约束等功能,非常简洁明了,剩下的工作都由框架来完成了。
定义表时出现的注解:
@Table("address")
@PrimaryKey(AssignType.AUTO_INCREMENT)
@Column(COL_ID)
@NotNull
@Unique
2、注解的声明
Table.java
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
public String value();
}
PrimaryKey.java
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PrimaryKey {
public AssignType value();
enum AssignType {
BY_MYSELF,
AUTO_INCREMENT
}
}
3. 注解的解析
public static synchronized EntityTable getTable(Class<?> claxx, boolean needPK) {
//...
List<Field> fields = FieldUtil.getAllDeclaredFields(claxx);
//Field[] fields = claxx.getDeclaredFields();
for (Field f : fields) {
if (FieldUtil.isInvalid(f)) {
continue;
}
Property p = new Property();
p.field = f;
// 获取列名,每个属性都有,没有注解默认取属性名
Column col = f.getAnnotation(Column.class);
if (col != null) {
p.column = col.value();
} else {
p.column = f.getName();
}
// 主键判断
//if(Log.isPrint)Log.i(TAG, "PrimaryKey : " + PrimaryKey.class + " field: "+ f + " asst:" + AssignType.AUTO_INCREMENT);
PrimaryKey key = f.getAnnotation(PrimaryKey.class);
if (key != null) {
// 主键不加入属性Map
table.key = new com.litesuits.orm.db.model.PrimaryKey(p, key.value());
// 主键为系统分配,对类型有要求
if (table.key.isAssignedBySystem()) {
if (!FieldUtil.isLong(table.key.field) && !FieldUtil.isInteger(table.key.field)) {
throw new RuntimeException(
PrimaryKey.AssignType.AUTO_INCREMENT
+ "要求主键属性必须是long或者int( the primary key should be long or int...)\n " +
"提示:把你的主键设置为long或int型"
);
}
}
} else {
//ORM handle
//if(Log.isPrint)Log.i(TAG, "Mapping : " + Mapping.class+ " field: "+ f);
Mapping mapping = f.getAnnotation(Mapping.class);
if (mapping != null) {
table.addMapping(new MapProperty(p, mapping.value()));
} else {
table.pmap.put(p.column, p);
}
}
}
return table;
}