今晚读了think in java 的章节,感觉很不错,我就敲了下来,贴上代码给以后一个回顾:
建议提前读一下think in java 注解 。
说明创建注解我在第一个注解说明下,以后的注解不在说明。‘
dbtable 注解:
/**
* project name:myannotation
* file name:dbtable.java
* package name:com.iflytek.db
* date:2016-8-28下午08:20:54
* copyright (c) 2016, syzhao@iflytek.com all rights reserved.
*
*/
package com.iflytek.db;
import java.lang.annotation.elementtype;
import java.lang.annotation.retention;
import java.lang.annotation.retentionpolicy;
import java.lang.annotation.target;
/**
@target:
@target说明了annotation所修饰的对象范围:annotation可被用于 packages、types(类、接口、枚举、annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在annotation类型的声明中使用了target可更加明晰其修饰的目标。
作用:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)
取值(elementtype)有:
1.constructor:用于描述构造器
2.field:用于描述域
3.local_variable:用于描述局部变量
4.method:用于描述方法
5.package:用于描述包
6.parameter:用于描述参数
7.type:用于描述类、接口(包括注解类型) 或enum声明
@retention:
@retention定义了该annotation被保留的时间长短:某些annotation仅出现在源代码中,而被编译器丢弃;而另一些却被编译在class文件中;编译在class文件中的annotation可能会被虚拟机忽略,而另一些在class被装载时将被读取(请注意并不影响class的执行,因为annotation与class在使用上是被分离的)。使用这个meta-annotation可以对 annotation的“生命周期”限制。
作用:表示需要在什么级别保存该注释信息,用于描述注解的生命周期(即:被描述的注解在什么范围内有效)
取值(retentionpoicy)有:
1.source:在源文件中有效(即源文件保留)
2.class:在class文件中有效(即class保留)
3.runtime:在运行时有效(即运行时保留)
retention meta-annotation类型有唯一的value作为成员,它的取值来自java.lang.annotation.retentionpolicy的枚举类型值
*/
@target(elementtype.type)
@retention(retentionpolicy.runtime)
public @interface dbtable
{
public string name() default "";
}
constraints 约束注解:
/**
* project name:myannotation
* file name:constraints.java
* package name:com.iflytek.db
* date:2016-8-28下午08:27:08
* copyright (c) 2016, syzhao@iflytek.com all rights reserved.
*
*/
package com.iflytek.db;
import java.lang.annotation.elementtype;
import java.lang.annotation.retention;
import java.lang.annotation.retentionpolicy;
import java.lang.annotation.target;
@target(elementtype.field)
@retention(retentionpolicy.runtime)
public @interface constraints
{
boolean primarykey() default false;
boolean allownull() default true;
boolean unique() default false;
}
sqlinteger int注解:
/**
* project name:myannotation
* file name:sqlinteger.java
* package name:com.iflytek.db
* date:2016-8-29下午10:24:11
* copyright (c) 2016, syzhao@iflytek.com all rights reserved.
*
*/
package com.iflytek.db;
import java.lang.annotation.elementtype;
import java.lang.annotation.retention;
import java.lang.annotation.retentionpolicy;
import java.lang.annotation.target;
@target(elementtype.field)
@retention(retentionpolicy.runtime)
public @interface sqlinteger
{
string name() default "";
constraints constraints() default @constraints;
}
sqlstring 字符注解:
/**
* project name:myannotation
* file name:sqlstring.java
* package name:com.iflytek.db
* date:2016-8-29下午10:28:04
* copyright (c) 2016, syzhao@iflytek.com all rights reserved.
*
*/
package com.iflytek.db;
import java.lang.annotation.elementtype;
import java.lang.annotation.retention;
import java.lang.annotation.retentionpolicy;
import java.lang.annotation.target;
@target(elementtype.field)
@retention(retentionpolicy.runtime)
public @interface sqlstring
{
int value() default 0;
string name() default "";
constraints constraints() default @constraints;
}
创建表的处理器:
/**
* project name:myannotation
* file name:tablecreator.java
* package name:com.iflytek.table
* date:2016-8-29下午10:57:52
* copyright (c) 2016, syzhao@iflytek.com all rights reserved.
*
*/
package com.iflytek.table;
import java.lang.annotation.annotation;
import java.lang.reflect.field;
import java.util.arraylist;
import java.util.list;
import com.iflytek.db.constraints;
import com.iflytek.db.dbtable;
import com.iflytek.db.sqlinteger;
import com.iflytek.db.sqlstring;
public class tablecreator
{
public static void main(string[] args)
{
createtable(member.class);
}
//创建表sql语句
private static void createtable(class<?> cl)
{
//获取dbtable注解
dbtable dbtable = cl.getannotation(dbtable.class);
//判断dbtable注解是否存在
if (dbtable == null)
{
system.out.println("没有找到关于dbtable");
return;
}
//如果@dbtable注解存在获取表明
string tablename = dbtable.name();
//判断表名是否存在
if (tablename.length() < 1)
{
//不存在,说明默认就是类名,通过 cl.getsimplename()获取类名并且大写
tablename = cl.getsimplename().touppercase();
}
//定义获取column的容器
list<string> columndefs = new arraylist<string>();
//循环属性字段
//说明:getdeclaredfields()获得某个类的所有申明的字段,即包括public、private和proteced,但是不包括父类的申明字段。
//getfields()获得某个类的所有的公共(public)的字段,包括父类。
for (field field : cl.getdeclaredfields())
{
//定义表字段名称变量
string columnname = null;
//获取字段上的注解(现在字段允许多个注解,因此返回的是数组)
annotation[] anns = field.getdeclaredannotations();
//判断属性是否存在注解
if (anns.length < 1)
continue;
//判断是否是我们定义的数据类型
if (anns[0] instanceof sqlinteger)
{
//获取sqlinteger 注解
sqlinteger sint = (sqlinteger)anns[0];
//判断是否注解的name是否有值
if (sint.name().length() < 1)
{
//如果没有值,说明是类的属性字段,获取属性并转换大写
columnname = field.getname().touppercase();
}
else
{ //如果有值,获取设置的name值
columnname = sint.name();
}
//放到属性的容器内
columndefs.add(columnname + " int " + getconstraints(sint.constraints()));
}
//同上sqlinteger,这里不写注释了
if (anns[0] instanceof sqlstring)
{
sqlstring sstring = (sqlstring)anns[0];
if (sstring.name().length() < 1)
{
columnname = field.getname().touppercase();
}
else
{
columnname = sstring.name();
}
columndefs.add(columnname + " varchar(" + sstring.value() + ")" + getconstraints(sstring.constraints()));
}
//定义生成创建表的sql语句
stringbuilder createcommand = new stringbuilder("create table " + tablename + "(");
//循环上面属性容器,
for (string columndef : columndefs)
{
//把属性添加到sql语句中
createcommand.append("\n " + columndef + ",");
//去掉最后一个逗号
string tablecreate = createcommand.substring(0, createcommand.length() - 1) + ");";
//打印
system.out.println("table creation sql for " + cl.getname() + " is :\n" + tablecreate);
}
}
}
private static string getconstraints(constraints con)
{
string constraints = "";
//判断是否为null
if (!con.allownull())
{
constraints += " not null ";
}
//判断是否是主键
if (con.primarykey())
{
constraints += " primary key ";
}
//是否唯一
if (con.unique())
{
constraints += " unique ";
}
return constraints;
}
}
以上代码拷贝出来,就可以运行了!
上面虽然是简单的创建表语句,但我们可以蔓延到hibernate的domain类里的注解,各种curd ,何尝不是这样处理的呢,只是hibernate有很多东西,但是万变不离其宗,以后有机会研究一下hibernate 。
收获:
读了以后,对于注解知道为什么要这么用了,其实顾名思义就是一个注解,只是有一个处理器来处理这个注解,这对我以后用到注解方面应该有帮助的,
时间不早了,就写到这里!
结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。