java自定义注解类的学习心得

创建自定义注解与编写接口很相似,除了它的接口关键字前有个@符号。我们可以在注解中定义方法,直接上例子。

   常用元注解:

</pre><span style="font-size:18px;"><span style="font-size:14px;"></span></span><pre name="code" class="java">@Documented
指被注解的元素可以被javadoc或其他工具文档化。
<pre name="code" class="java">@Inherited
表示一个注解类型会被自动继承,如果用户在类声明的时候查询注解类型,同时类声明中也没有这个类型的注解,那么注解类型会自动查询该类的父类,这个过程将会不停地重复,直到该类型的注解被找到为止,或是到达类结构的顶层。
@target(常用的几种类型type-类,method--方法,field-变量,constructor-构造函数)
@retention (注解的保存时间,常用为source-原文件,class-.class文件,runtime-运行时)

 

   下面是一个用自定义注解进行动态拼接sql的例子,加深对注解使用的印象。

package com.tom.test;

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;

@Documented
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
  String value() default "";
}

package com.tom.test;

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;

@Documented
@Inherited
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
   String value() default "";
}

package com.tom.test;
@Table("admin")
public class Admin {
	@Column("a_id")
  private int id;
	@Column("admin_name")
  private String name;
	@Column("sex")
  private String sex;
/**
 * @return the id
 */
public int getId() {
	return id;
}
/**
 * @param id the id to set
 */
public void setId(int id) {
	this.id = id;
}
/**
 * @return the name
 */
public String getName() {
	return name;
}
/**
 * @param name the name to set
 */
public void setName(String name) {
	this.name = name;
}
/**
 * @return the sex
 */
public String getSex() {
	return sex;
}
/**
 * @param sex the sex to set
 */
public void setSex(String sex) {
	this.sex = sex;
}
public Admin(int id, String name, String sex) {
	super();
	this.id = id;
	this.name = name;
	this.sex = sex;
}
public Admin() {
	super();
}
  
}

package com.tom.test;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class SqlHelper {
	public static void main(String[] args) throws Exception {
		Admin admin = new Admin(1, "李四", "男");
		System.out.println(makeSQL(admin));
	}

	public static String makeSQL(Object object) throws Exception {
		Table table = object.getClass().getAnnotation(Table.class);
		String tableName = table.value();
		StringBuffer sql = new StringBuffer();
		sql.append("select 1 from " + tableName + " where 1=1 ");
		Field[] fields = object.getClass().getDeclaredFields();//getDeclaredFields() 可获取类中的所有变量
		 //Field[] fields = object.getClass().getFields();  getFields()方法只能获取public访问修饰符的变量
		for (Field field : fields) {
			String columnValue = field.getAnnotation(Column.class).value();
			if (!columnValue.equals("")) {
				String methodName = "get"
						+ field.getName().substring(0, 1).toUpperCase()
						+ field.getName().substring(1);
				Method method = object.getClass().getMethod(methodName);
				Object val = method.invoke(object); //如从Admin类中调用getId()方法
				sql.append(val instanceof Integer ? " And "+columnValue + "="
						+ val.toString() + " " : " And "+columnValue + "= '"
						+ val.toString() + "' ");
			}

		}
		return sql.toString();
	}
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值