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,当时还遗留下一些 ...
随机推荐
- Mysql日期时间大全
MySQL日期时间函数大全 DAYOFWEEK(date) 返回日期date是星期几(1=星期天,2=星期一,--7=星期六,ODBC标准)mysql> select DAYOFWEEK('1 ...
- 将gridFS中的图片文件写入硬盘
开启用户验证下的gridfs 连接使用,在执行脚本前可以在python shell中 from pymongo import Connectionfrom gridfs import *con = C ...
- BizTalk动手实验(四)Schema开发测试
1 课程简介 通过本课程熟悉Schema的相关开发技术 2 准备工作 1. 熟悉XML.XML Schema.XSLT等相关XML开发技术 2. 新建BizTalk空项目 3 演示 3.1 格式化XM ...
- IOS第18天(5,CABasicAnimation基本动画)
******* #import "HMViewController.h" @interface HMViewController () @property (nonatomic, ...
- 【iCore3 双核心板_ uC/OS-III】例程一:认识 uC/OS-III
实验指导书及代码包下载: http://pan.baidu.com/s/1i4FuMep iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- P1038 神经网络
#include <bits/stdc++.h> using namespace std; const int maxn = 105; struct node { int situatio ...
- EntityFramework SQLite
安装完sqlite的nuget包后,还要设置App.config文件才能正常使用 1. 在<providers>节点添加一条提供器配置 <provider invariantNam ...
- 蓝牙--对象交换协议(OBEX)
1.OBEX协议概述 OBEX是IrOBEX的简称,IrOBEX协议是红外数据协会IrDA开发的用于红外数据链路上数据对象交换的会话层协议.OBEX是一种紧凑高效的二进制协议,功能类似于HTTP协议. ...
- c# 对于批量表的统一查询 WM_CONCAT行列转换行数
//需要显示的列 sql = string.Format(@" SELECT WM_ ...
- NPOI操作excel之写入数据到excel表
在上一篇<NPOI操作excel之读取excel数据>我们把excel数据写入了datatable中,本篇就讲如何把datatable数据写入excel中. using System; u ...