java 基础学习之注解

一 元注解

@Target  表示 注解作用域  file  (实例)  method 方法 type 类

@Retention   runTIme 在运行时可用  class 类文件中的使用,source 注解被编译器丢弃(源代码期间使用)

@Documented javadoc文件

@Inhertied  子类继承

二 自定义注解

简单点 就是自己编写个自定义注解,然后在通过反射 解析注解。java 中的万物皆对象,(Class.forName(),类也对象像,method方法也是对象)

@interface nn()

   {

    String value();

     }

private static String select(demo d1) {
		// TODO Auto-generated method stub
		StringBuilder sb=new StringBuilder();
		
		sb.append("select * from");
 		Class c=d1.getClass();
		  if(c.isAnnotationPresent(table.class)){
			table tt = (table)c.getAnnotation(table.class);
		    sb.append(tt.value()).append("where 1=1");//加载  名字(table)
		   Field[] files = c.getDeclaredFields();
		   //获取所有字段
		   for (int i = 0; i < files.length; i++) {
			if(files[i].isAnnotationPresent(coumnt.class)){
				coumnt coumnt = files[i].getAnnotation(coumnt.class);
				//获取字段   ming cheng
				Object zhi = null;
				String cc=coumnt.value();
				String methodname="get"+files[i].getName().substring(0,1).toUpperCase()+files[i].getName().substring(1);
				try {
					//获取  字段值
					Method method = c.getMethod(methodname);
					 zhi =method.invoke(d1,null);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
				if(cc==null||(zhi instanceof Integer &&(Integer)zhi==0)){
					continue;
				}
					sb.append("and").append(cc).append("=").append("'").append(zhi).append("'");
				
			}
			else{
				System.err.println(" xoumnt not exist");
				continue;
			}
		}
		}
		else{
			System.out.println("table.class is not exist");
		}
		
		  return sb.toString();
	  }

}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值