博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java注解
阅读量:7040 次
发布时间:2019-06-28

本文共 2405 字,大约阅读时间需要 8 分钟。

hot3.png

注解Table

package annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import javax.lang.model.element.Element;@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE)public @interface Table {	public String tableName();	public String schema() default "b3blog";}

注解Column

package annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Inherited@Documented@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)public @interface Column {	public String value();}

定义一个实体User

package model;import annotation.Column;import annotation.Table;@Table(tableName="solo_user")public class User {		@Column("id")	private Integer id;	@Column("name")	private String name;	@Column("sex")	private Integer sex;	public Integer getId() {		return id;	}	public void setId(Integer id) {		this.id = id;	}	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}	public Integer getSex() {		return sex;	}	public void setSex(Integer sex) {		this.sex = sex;	}		}

测试类Test

package javaassist;import java.lang.annotation.Annotation;import java.lang.reflect.Field;import model.User;import annotation.Column;import annotation.Table;public class Test {    public static void main(String[] args) throws Exception {    	    	    	//得到所有的注解    	Annotation[] annotations=User.class.getAnnotations();    	for (int i = 0; i < annotations.length; i++) {    		//判断注解类型    		if (annotations[i].annotationType().equals(Table.class)) {				Table tableAnnotation=(Table) annotations[i];				System.out.println("表名称:"+tableAnnotation.tableName());				System.out.println("schema默认值:"+tableAnnotation.schema());			}    				}    	    	Field[] fields=User.class.getDeclaredFields();    	for (Field field : fields) {			Column columnAnnotation=field.getAnnotation(Column.class);			if (columnAnnotation!=null) {				System.out.println("字段名称:"+field.getName()+"  注解value:"+columnAnnotation.value());			}		}    }}

运行结果

表名称:solo_userschema默认值:b3blog字段名称:id  注解value:id字段名称:name  注解value:name字段名称:sex  注解value:sex

转载于:https://my.oschina.net/u/3238650/blog/1579659

你可能感兴趣的文章
降维中的特征选择
查看>>
如何在ChemDraw中缩短双键长度
查看>>
【tarjan+lca】有机化学之神偶尔会做作弊
查看>>
Android之permission权限列表(AndroidManifest.xml)
查看>>
Redux入门学习
查看>>
我的友情链接
查看>>
利用AWS boto实现EC2 存储卷的自动快照
查看>>
微软私有云解决方案专家认证之路
查看>>
曾经的痛啊 关于 becomeFirstResponder
查看>>
Android Service
查看>>
解决iphone safari上的圆角问题
查看>>
zabbix源码安装
查看>>
phpcms笔记
查看>>
查看系统用户登录信息命令
查看>>
CMS之图片管理(2)
查看>>
php 魔术方法总结(持续更新)
查看>>
利用ADMT进行Exchange跨域迁移之一:配置域信任
查看>>
javascript获取系统当前时间
查看>>
【java解惑】java中那些反常识的小知识
查看>>
bash内部命令变量
查看>>