Java中常见的注解
@Override 覆盖父类的方法
@Deprecated 方法已过时
@SuppressWarnings("deprecation") 忽略Deprecated警告
注解按照运行机制划分
- 源码注解:注解只在源码中存在,编译成.class文件就不存在了;
- 编译时注解:注解在源码和.class文件中都存在(如:@Override、@Deprecated、@SuppressWarings);
- 运行时注解 :在运行阶段还起作用,甚至会影响运行逻辑的注解(如:@Autowired)
元注解
元注解(meta-annotation):
元注解的作用就是负责注解其他注解。Java5.0定义了4个标准的meta-annotation类型,它们被用来提供对其它 annotation类型作说明。Java5.0定义的元注解:
1.@Target,
@Target说明了Annotation所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使用了target可更加明晰其修饰的目标。
取值(ElementType)有:
1.CONSTRUCTOR:用于描述构造器
2.FIELD:用于描述域
3.LOCAL_VARIABLE:用于描述局部变量
4.METHOD:用于描述方法
5.PACKAGE:用于描述包6.PARAMETER:用于描述参数
7.TYPE:用于描述类、接口(包括注解类型) 或enum声明
2.@Retention
表示需要在什么级别保存该注释信息,用于描述注解的生命周期
取值(RetentionPoicy)有:
1.SOURCE:在源文件中有效(即源文件保留)
2.CLASS:在class文件中有效(即class保留)
3.RUNTIME:在运行时有效(即运行时保留)
3.@Documented,
@Documented用于描述其它类型的annotation应该被作为被标注的程序成员的公共API,因此可以被例如javadoc此类的工具文档化。Documented是一个标记注解,没有成员。
4.@Inherited
@Inherited 元注解是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。如果一个使用了@Inherited修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。
自定义注解:
使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口,由编译程序自动完成其他细节。
定义注解格式:
public @interface 注解名 {定义体}
Annotation类型里面的参数设定:
1. 只能用public或默认(default)这两个访问权修饰.例如,String value();这里把方法设为defaul默认类型;
2. 参数成员只能用基本类型byte,short,char,int,long,float,double,boolean八种基本数据类型和 String,Enum,Class,annotations等数据类型,以及这一些类型的数组.例如,String value();这里的参数成员就为String;
3. 如果只有一个参数成员,最好把参数名称设为"value",
通过自定义注解实现模拟Hibernate的查询操作,模拟sql语句自动生成
package com.pflm.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;
/**
* 表的自定义注解
* @author qxw
* @data 2018年6月7日下午5:01:55
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Table {
String value() ;
}
字段自定义注解
package com.pflm.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;
/**
* 字段自定义注解
* @author qxw
* @data 2018年6月20日下午6:14:45
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Column {
String value() ;
}
package com.pflm.annotaionTest;
import com.pflm.annotation.Column;
import com.pflm.annotation.Table;
@Table("student")
public class Student {
@Column("email")
public String email;
@Column("name")
public String name;
@Column("age")
public int age;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.pflm.annotaionTest;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import com.pflm.annotation.Column;
import com.pflm.annotation.Table;
/**
* 实现读取自定义注解,拼接sql语句
* @author qxw
* @data 2018年6月20日下午6:21:08
*/
public class Filter {
public static void main(String[] args) {
Student s=new Student();
s.setAge(10);
s.setEmail("870439570@qq.com,919085258@qq.com");
s.setName("萨达的飞洒");
System.out.println(queryList(s));
}
/**
* 模拟hibernte功能, 获取类上的注解实现 sql拼接
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static String queryList(Object obj){
StringBuilder sql=new StringBuilder();
//获取到class
Class c=obj.getClass();
//获取到类的自定义注解上,table上的表名称
boolean exists=c.isAnnotationPresent(Table.class);
if(!exists){
return null;
}
Table t=(Table) c.getAnnotation(Table.class);
//拿到表明
String tableName=t.value();
sql.append("select * from ").append(tableName).append(" where 1=1 ");
//遍历所有的字段
Field[] f=c.getDeclaredFields();
for(Field field:f){
//处理每个字段对应的sql
//拿到字段注解上的名称
boolean isFexists=field.isAnnotationPresent(Column.class);
if(!isFexists){
continue;
}
Column col=field.getAnnotation(Column.class);
String columnName=col.value();
//拿到字段值
String filedName=field.getName();
//通过反射获取字段值 首字母大写
String getMothodName="get"+filedName.substring(0, 1).toUpperCase()+filedName.substring(1);
Object fieldValue=null;
try {
//得到方法对象
Method getMethod=c.getMethod(getMothodName);
//传入对象名字
fieldValue=getMethod.invoke(obj);
} catch (Exception e) {
e.printStackTrace();
}
//拼装sql
if(fieldValue==null ||(fieldValue instanceof Integer &&(Integer)fieldValue==0)){
continue;
}
sql.append(" and ").append(filedName);
if(fieldValue instanceof String){
if(((String) fieldValue).contains(",")){
String []arr=((String) fieldValue).split(",");
sql.append(" in(");
for(String s:arr){
sql.append("'").append(s).append("'");
}
sql.append(")");
}else{
sql.append("=").append("'").append(fieldValue).append("'");
}
}else if(fieldValue instanceof Integer) {
sql.append("=").append(fieldValue);
}
}
return sql.toString();
}
public static String save(Object obj){
return null;
}
}