Annotation
Annotation是给类,方法或域上加的一种特殊的标记,可以通过反射取到注解的类型和值,从而完成某种特定的操作。
定义注解需要使用元注解,元注解有@Retention和@Target
//@Retention用来定义该注解在哪一个级别可用,在源代码中(SOURCE),类文件中(CLASS),或者运行时(RUNTIME)
//@Target用来定义注解将用在什么地方 类,方法,域...
比如:
@Retention(RetentionPolicy.RUNTIME) 表明在运行时保留该注解
@Target(ElementType.METHOD) 表明为方法注解,只能在方法声明中使用
注解不支持继承,注解元素的类型可以是基本类型,String,Class, enum, 和另一个注解。
没有元素的注解称为标记注解。
简单例子:
----------根据Java Bean建立数据库表-----------
//数据库字段属性枚举
public enum EOption {
//可空
NONE, //非空
NOT_NULL, PRIMARY, UNIQUE; public boolean withIn(EOption[] options){
for(EOption o : options){
if(o==this){
return true;
}
}
return false;
} }
----------
//数据库字段类型枚举
public enum EType { //自动判断
AUTO, CHAR, VARCHAR, SMALLINT, INT, BIGINT, TEXT, BLOB, DATE;
}
---------数据表注解------
@Retention(RUNTIME)
@Target(TYPE)
public @interface DTable {
//表名
public String name() default ""; }
--------表字段注解------
@Retention(RUNTIME)
@Target(FIELD)
public @interface DColumn { //字段类型
EType type() default EType.VARCHAR; //类型相关值
int value() default 10; //字段名称
String name() default ""; //字段属性
EOption[] option() default EOption.NOT_NULL; }
--------测试Bean-----
@DTable(name="user_t")
public class UserBean { @DColumn(type=EType.INT, option={EOption.NOT_NULL, EOption.PRIMARY})
private int id; @DColumn(30)
private String name; @DColumn(type=EType.INT)
private Integer age; @DColumn(30)
private String email; @DColumn(type=EType.DATE)
private String create_date; }
--------执行建表--------
public class TableCreator { private static String getCon(EOption[] options) {
String ret = "";
if (EOption.NOT_NULL.withIn(options)) {
ret += " NOT NULL ";
}
if (EOption.PRIMARY.withIn(options)) {
ret += " PRIMARY KEY ";
}
if (EOption.UNIQUE.withIn(options)) {
ret += " UNIQUE ";
}
return ret;
} public static void main(String[] args) throws ClassNotFoundException { String classname = "dbAnnotation.UserBean"; Class<?> c = Class.forName(classname);
DTable table = (DTable) c.getAnnotation(DTable.class);
if (table == null) {
System.out.println(c.getName() + " no DTable Annotation");
return;
}
String tableName = table.name();
if (tableName.length() < 1) {
tableName = c.getName().toLowerCase();
} List<String> columnNames = new ArrayList<>();
for (Field f : c.getDeclaredFields()) { Annotation[] a = f.getDeclaredAnnotations();
if (a.length < 1) {
continue;
}
DColumn dcol = (DColumn) a[0];
String cName = dcol.name().length() < 1 ? f.getName().toLowerCase() : dcol.name(); if (dcol.type() == EType.INT)
{
columnNames.add(cName + " INT" + getCon(dcol.option()));
}
else if (dcol.type() == EType.VARCHAR)
{
columnNames.add(cName + " VARCHAR(" + dcol.value() + ")" + getCon(dcol.option()));
} } StringBuilder sb = new StringBuilder();
sb.append("create table ");
sb.append(tableName);
sb.append(" (\n");
for (String s : columnNames) {
sb.append(s);
sb.append(",\n");
}
sb.append(")"); System.out.println(sb.toString());
} }
-------输出------
create table user_t (
id INT NOT NULL PRIMARY KEY ,
name VARCHAR(30) NOT NULL ,
age INT NOT NULL ,
email VARCHAR(30) NOT NULL ,
)
-------
end
Annotation的更多相关文章
- Spring Enable annotation – writing a custom Enable annotation
原文地址:https://www.javacodegeeks.com/2015/04/spring-enable-annotation-writing-a-custom-enable-annotati ...
- 【java】细说 JAVA中 标注 注解(annotation)
Java注解是附加在代码中的一些元信息,用于一些工具在编译.运行时进行解析和使用,起到说明.配置的功能.注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用 下面我们来详细说说这个注解,到底是怎么一 ...
- Java学习之注解Annotation实现原理
前言: 最近学习了EventBus.BufferKinfe.GreenDao.Retrofit 等优秀开源框架,它们新版本无一另外的都使用到了注解的方式,我们使用在使用的时候也尝到不少好处,基于这种想 ...
- Java Annotation概述
@(Java)[Annotation|Java] Java Annotation概述 用途 编译器的相关信息,如用于检测错误和一些警告 编译时和部署时的处理,如一些软件用于自动生成代码之类的 运行时处 ...
- hibernate学习三(使用Annotation,注解)
一.新建一个工程hibernate_02_HelloWorld_Annotation(复制01工程并重命名); 二.新建一个实体类teacher.java,数据库中新建teacher表; import ...
- struts2使用annotation注意事项
struts2使用annotation注意事项 1.包名只能以.action .actions .struts .struts2结尾.如:com.cnbolgs.web.actions 2.类名只 ...
- Data Validate 之 Data Annotation
什么是Data Annotation ? 如何使用 ? 自定义Validate Attribute EF Db first中使用Data Annotation asp.net MVC中使用Data ...
- SpringMvc的xml配置与annotation配置的例子的区别
1.导入jar包时,要在xml配置基础上加 spring-aop-4.2.2.RELEASE.jar (注解的时候需要) 2.编写controller的时候要annotation需要做相关配置即红色部 ...
- spring3.0使用annotation完全代替XML(续)
从回帖的反应来看,大多数人还是不赞成完全代替XML的,这点倒是在意料之中.我个人还是倾向于用代码来取代XML的Bean定义,当然这更多的是关乎个人偏好,不代表与我观点不同的人就是错的. 先来说说代码相 ...
- spring3.0使用annotation完全代替XML(三)
很久之前写过两篇博客: spring3.0使用annotation完全代替XML spring3.0使用annotation完全代替XML(续) 用java config来代替XML,当时还遗留下一些 ...
随机推荐
- 一次与iptables有关的Openstack排错
先说下环境: 宿主机A(192.168.1.242)上运行着实例a(192.168.1.176), 宿主机B(192.168.1.56)上运行着实例b(192.168.1.50). 用户说从实例b上t ...
- js中的text(),html() ,val()的区别
js中的text(),html() ,val()的区别 text(),html() ,val()三个方法用于html元素的存值和取值,但是他们各有特点,text()用于html元素文本内容的存取,ht ...
- hibernate学习(4)——实体配置详解
1.实体 编写规则 提供一个无参数 public访问控制符的构造器 提供一个标识属性,映射数据表主键字段,hibernate以id识别,必须有主键 所有属性提供public访问控制符的 set ge ...
- js获取url中参数
/** * 获取地址栏参数值 * @param name 参数名 * @returns */ $(function () { var url = location.search; //获取url中 ...
- 【转】发布一个基于NGUI编写的UI框架
发布一个基于NGUI编写的UI框架 1.加载,显示,隐藏,关闭页面,根据标示获得相应界面实例 2.提供界面显示隐藏动画接口 3.单独界面层级,Collider,背景管理 4.根据存储的导航信息完成界面 ...
- SinalR+WebSocket
1.参考资料: http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-server http://signalr.ne ...
- Jfianl
http://www.oschina.net/question/257183_149268----------- 添加Handler: me.add(new ContextPathHandler(&q ...
- java API 知识:截取特殊标识之前的字符串
一: double a = 23.36; String b = String.valueOf(a); String d = b.substring(, b.lastIndexOf(".&qu ...
- IEnumerable和IEnumerator
概述 IEnumerable和IEnumerator接口存在的意义:用来实现迭代的功能! public interface IEnumerable { IEnumerator GetEnumerato ...
- python_os
1. 基本功能的介绍 os模块包含普通的操作系统的功能 2. 常用的变量 (1)os.name 获取正在使用的平台, Windows 返回 nt, Linux或者Unix 返回 posix 3. 常用 ...