您的位置:首页 > 编程语言 > Java开发

Java注解2--解析注解与注解反射生成SQL语句

2018-01-31 13:40 741 查看
三解析注解
注解处理器
解析注解的代码例子
测试元注解Retention
测试元注解Inherited
知识导图

(三)解析注解

1. 注解处理器

       何为解析注解?即通过反射获取类、函数或成员上的运行时注解信息,从而实现动态控制程序运行的逻辑。
解析注解主要用到两个类库:
1.1.  java.lang.annotation.Annotation
Java使用Annotation接口来代表程序元素前面的注解,该接口是所有Annotation类型的父接口。
1.2. java.lang.reflect.AnnotatedElement
AnnotatedElement 接口代表程序中可以接受注解的程序元素,是所有程序元素(Class、Method、Field、Package和Constructor)的父接口。获取该接口对象之后,即可以调用对象方法来访问Annotation信息,常用有如下几个:
         1. getAnnotations():返回该程序元素上存在的所有注解。
         2. isAnnotationPresent(annotation.class):判断该程序元素上是否包含指定类型的注解
         3. getDeclaredAnnotations():返回直接存在于此元素上的所有注释。与此接口中的其他方法不同,该方法将忽略继承的注释。

2. 解析注解的代码例子

定义一个注解如下:

[java]
view plain
copy

print?

package jtzeng;  
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;  
@Target({ElementType.METHOD,ElementType.TYPE})  
@Retention(RetentionPolicy.RUNTIME)  
@Inherited  
@Documented  
public @interface Description {   
    String desc();  
    String author() default "JTZeng";  
    int age() default 21;  
}   

package jtzeng;
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;
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Description {
String desc();
String author() default "JTZeng";
int age() default 21;
}

定义一个使用了注解的Test类:

[java]
view plain
copy

print?

package jtzeng;  
@Description(desc="this is ElementType.TYPE",author="JTZeng",age=21)  
public class Test1 {  
    @Description(desc="this is ElementType.METHOD",author="JTZeng",age=18)  
    public void run(){  
        System.out.println("I can run!");  
    }  
}  

package jtzeng;
@Description(desc="this is ElementType.TYPE",author="JTZeng",age=21)
public class Test1 {
@Description(desc="this is ElementType.METHOD",author="JTZeng",age=18)
public void run(){
System.out.println("I can run!");
}
}

再编写一个解析类:

[java]
view plain
copy

print?

package jtzeng;  
import java.lang.annotation.Annotation;  
import java.lang.reflect.Method;  
public class ParseAnno {  
    public static void main(String[] args) {  
        try {  
            /* 
             * 1.使用类加载器加载类 
             * Class.forName("类名字符串") (注意:类名字符串必须是全称,包名+类名) 
             */  
            Class c = Class.forName("jtzeng.Test1");  
              
            //2.判断类上是否存在注解,并获取类上面注解的实例  
            if(c.isAnnotationPresent(Description.class)){  
                Description Description = (Description) c.getAnnotation(Description.class);  
                System.out.println(Description.desc());  
                System.out.println(Description.author());  
                System.out.println(Description.age());  
            }  
              
            //3.判断方法上是否存在注解,并获取方法上面注解的实例  
            Method[] ms = c.getMethods();  
            for (Method method : ms) {  
                if(method.isAnnotationPresent(Description.class)){  
                    Description Description = (Description)method.getAnnotation(Description.class);  
                    System.out.println(Description.desc());  
                    System.out.println(Description.author());  
                    System.out.println(Description.age());  
                }  
            }  
            //另一种获取方法上的注解的解析方法  
            for (Method method : ms) {  
                Annotation[] as = method.getAnnotations();  
                for (Annotation annotation : as) {  
                    if(annotation instanceof Description){  
                        System.out.println(((Description) annotation).desc());  
                        System.out.println(((Description) annotation).author());  
                        System.out.println(((Description) annotation).age());  
                    }  
                }  
            }  
              
        } catch (ClassNotFoundException e) {  
            e.printStackTrace();  
        }  
    }  
}  

package jtzeng;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class ParseAnno {
public static void main(String[] args) {
try {
/*
* 1.使用类加载器加载类
* Class.forName("类名字符串") (注意:类名字符串必须是全称,包名+类名)
*/
Class c = Class.forName("jtzeng.Test1");

//2.判断类上是否存在注解,并获取类上面注解的实例
if(c.isAnnotationPresent(Description.class)){
Description Description = (Description) c.getAnnotation(Description.class);
System.out.println(Description.desc());
System.out.println(Description.author());
System.out.println(Description.age());
}

//3.判断方法上是否存在注解,并获取方法上面注解的实例
Method[] ms = c.getMethods();
for (Method method : ms) {
if(method.isAnnotationPresent(Description.class)){
Description Description = (Description)method.getAnnotation(Description.class);
System.out.println(Description.desc());
System.out.println(Description.author());
System.out.println(Description.age());
}
}
//另一种获取方法上的注解的解析方法
for (Method method : ms) {
Annotation[] as = method.getAnnotations();
for (Annotation annotation : as) {
if(annotation instanceof Description){
System.out.println(((Description) annotation).desc());
System.out.println(((Description) annotation).author());
System.out.println(((Description) annotation).age());
}
}
}

} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}

运行解析类,结果如下,有三部分,第一部分是类上的注解,第二、三部分是不同的方法解析方法上的注解:



3.测试元注解@Retention

如果把@Retention改为SOURCE或者CLASS,再次运行ParseAnno类:





          运行结果如下,什么也没有,这进一步说明了,只有设置为RUNTIME,才可以在运行时通过反射机制来获取注解信息,从而实现动态控制程序运行的逻辑。



4. 测试元注解@Inherited

创建一个接口和一个父类,并改写Test1类:

[java]
view plain
copy

print?

package jtzeng;  
@Description(desc = "this is Interface")    //因为注解Description中的author和age成员有默认值,所以可以省略  
public interface Invalid {  
    @Description(desc = "this is Interface method")  
    public void print();  
}  
  
  
package jtzeng;  
@Description(desc = "this is class")  
public class Valid {  
    @Description(desc = "this is class method")  
    public void run(){  
        System.out.println("注解继承只能在子类中有效,不能在接口中继承");  
    }  
}  
  
  
package jtzeng;  
public class Test1 extends Valid implements Invalid {  
    @Override  
    public void run(){  
        System.out.println("覆盖父类的方法");  
    }  
    @Override  
    public void print() {  
        System.out.println("实现接口的方法");  
    }  
}  

package jtzeng;
@Description(desc = "this is Interface")    //因为注解Description中的author和age成员有默认值,所以可以省略
public interface Invalid {
@Description(desc = "this is Interface method")
public void print();
}

package jtzeng;
@Description(desc = "this is class")
public class Valid {
@Description(desc = "this is class method")
public void run(){
System.out.println("注解继承只能在子类中有效,不能在接口中继承");
}
}

package jtzeng;
public class Test1 extends Valid implements Invalid {
@Override
public void run(){
System.out.println("覆盖父类的方法");
}
@Override
public void print() {
System.out.println("实现接口的方法");
}
}

再一次运行ParseAnno解析类,输出结果如下,说明只能继承父类的注解,并且是类上的注解:



5. 知识导图

最后给出一张Java注解的知识导图,觉得总结得不错,这里就直接拿来了,导图来源:http://www.cnblogs.com/peida/archive/2013/04/26/3038503.html



(四)注解反射生成SQL语句

1.任务说明

        ①有一张用户表,字段包括:用户ID、用户名、昵称、年龄、性别、所在城市、邮箱、手机号;
        ②使用java注解来对用户表的每个字段或字段的组合条件进行动态生成SQL查询语句。

2.代码实现

2.1 定义注解

定义一个描述用户表的注解:

[java]
view plain
copy

print?

package dao;  
import java.lang.annotation.ElementType;  
import java.lang.annotation.Retention;  
import java.lang.annotation.RetentionPolicy;  
import java.lang.annotation.Target;  
  
@Target({ElementType.TYPE})  
@Retention(RetentionPolicy.RUNTIME)  
public @interface Table {  
      
    String value();  
}  

package dao;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {

String value();
}


定义一个描述用户表属性字段的注解:

[java]
view plain
copy

print?

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

package dao;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {

String value();
}


2.2 定义映射到数据库的bean

用户表,字段包括:用户ID、用户名、昵称、年龄、性别、所在城市、邮箱、手机号:

[java]
view plain
copy

print?

package dao;  
  
@Table("user")  
public class User {  
      
    @Column("id")  
    private int id;  
      
    @Column("user_name")  
    private String userName;  
      
    @Column("nick_name")  
    private String nickName;  
      
    @Column("age")  
    private int age;  
      
    @Column("city")  
    private String city;  
      
    @Column("email")  
    private String email;  
      
    @Column("mobile")  
    private String mobile;  
  
    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  
    public String getUserName() {  
        return userName;  
    }  
    public void setUserName(String userName) {  
        this.userName = userName;  
    }  
    public String getNickName() {  
        return nickName;  
    }  
    public void setNickName(String nickName) {  
        this.nickName = nickName;  
    }  
    public int getAge() {  
        return age;  
    }  
    public void setAge(int age) {  
        this.age = age;  
    }  
    public String getCity() {  
        return city;  
    }  
    public void setCity(String city) {  
        this.city = city;  
    }  
    public String getEmail() {  
        return email;  
    }  
    public void setEmail(String email) {  
        this.email = email;  
    }  
    public String getMobile() {  
        return mobile;  
    }  
    public void setMobile(String mobile) {  
        this.mobile = mobile;  
    }  
}  

package dao;

@Table("user")
public class User {

@Column("id")
private int id;

@Column("user_name")
private String userName;

@Column("nick_name")
private String nickName;

@Column("age")
private int age;

@Column("city")
private String city;

@Column("email")
private String email;

@Column("mobile")
private String mobile;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
}


2.3 返回SQL查询语句的实现

根据参数动态返回查询语句:

[java]
view plain
copy

print?

package dao;  
import java.lang.reflect.Field;  
import java.lang.reflect.Method;  
  
public class ReturnQuery {  
    public static String query(Object u){  
        StringBuilder sqlStrBuilder = new StringBuilder();  
        //1.获取到Class  
        Class c = u.getClass();  
        //判断是否包含Table类型的注解  
        if(!c.isAnnotationPresent(Table.class)){  
            return null;  
        }  
        //2.获取到table的名字  
        Table t = (Table) c.getAnnotation(Table.class);  
        String tableName = t.value();  
        //加入 where 1=1 ,是为了防止未来如果没有条件的情况下也不会报错  
        sqlStrBuilder.append("select * from ").append(tableName).append(" where 1=1");  
          
        Field[] fArray = c.getDeclaredFields();   //获取类属性的所有字段,  
        //3.遍历所有的字段  
        for (Field field : fArray) {   
            //4.处理每个字段对应的sql  
            //判断是否包含Column类型的注解  
            if(!field.isAnnotationPresent(Column.class)){  
                continue;  
            }  
            //4.1.拿到字段上面注解的值,即Column注解的值  
            Column column = field.getAnnotation(Column.class);  
            String columnName = column.value();    
            //4.2.拿到字段的名  
            String filedName = field.getName();  
            //获取相应字段的getXXX()方法  
            String getMethodName = "get" + filedName.substring(0, 1).toUpperCase()  
                    + filedName.substring(1);  
            //字段的值  
            Object fieldValue = null;//属性有int、String等,所以定义为Object类  
            try {  
                Method getMethod = c.getMethod(getMethodName);  
                fieldValue = getMethod.invoke(u);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }   
              
            //4.3.拼接sql  
            if(fieldValue==null || (fieldValue instanceof Integer && (Integer)fieldValue==0)){  
                continue;  
            }  
            sqlStrBuilder.append(" and ").append(filedName);  
            if(fieldValue instanceof String){  
                if(((String)fieldValue).contains(",")){  
                    String[] values = ((String)fieldValue).split(",");  
                    sqlStrBuilder.append(" in(");  
                    for (String v : values) {  
                        sqlStrBuilder.append("'").append(v).append("'").append(",");  
                    }  
                    sqlStrBuilder.deleteCharAt(sqlStrBuilder.length()-1);  
                    sqlStrBuilder.append(")");  
                }  
                else{  
                    sqlStrBuilder.append("=").append("'").append(fieldValue).append("'");  
                }  
            }  
            else if(fieldValue instanceof Integer){  
                sqlStrBuilder.append("=").append(fieldValue);  
            }  
        }  
        return sqlStrBuilder.toString();  
          
    }  
}  

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

public class ReturnQuery {
public static String query(Object u){
StringBuilder sqlStrBuilder = new StringBuilder();
//1.获取到Class
Class c = u.getClass();
//判断是否包含Table类型的注解
if(!c.isAnnotationPresent(Table.class)){
return null;
}
//2.获取到table的名字
Table t = (Table) c.getAnnotation(Table.class);
String tableName = t.value();
//加入 where 1=1 ,是为了防止未来如果没有条件的情况下也不会报错
sqlStrBuilder.append("select * from ").append(tableName).append(" where 1=1");

Field[] fArray = c.getDeclaredFields();   //获取类属性的所有字段,
//3.遍历所有的字段
for (Field field : fArray) {
//4.处理每个字段对应的sql
//判断是否包含Column类型的注解
if(!field.isAnnotationPresent(Column.class)){
continue;
}
//4.1.拿到字段上面注解的值,即Column注解的值
Column column = field.getAnnotation(Column.class);
String columnName = column.value();
//4.2.拿到字段的名
String filedName = field.getName();
//获取相应字段的getXXX()方法
String getMethodName = "get" + filedName.substring(0, 1).toUpperCase()
+ filedName.substring(1);
//字段的值
Object fieldValue = null;//属性有int、String等,所以定义为Object类
try {
Method getMethod = c.getMethod(getMethodName);
fieldValue = getMethod.invoke(u);
} catch (Exception e) {
e.printStackTrace();
}

//4.3.拼接sql
if(fieldValue==null || (fieldValue instanceof Integer && (Integer)fieldValue==0)){
continue;
}
sqlStrBuilder.append(" and ").append(filedName);
if(fieldValue instanceof String){
if(((String)fieldValue).contains(",")){
String[] values = ((String)fieldValue).split(",");
sqlStrBuilder.append(" in(");
for (String v : values) {
sqlStrBuilder.append("'").append(v).append("'").append(",");
}
sqlStrBuilder.deleteCharAt(sqlStrBuilder.length()-1);
sqlStrBuilder.append(")");
}
else{
sqlStrBuilder.append("=").append("'").append(fieldValue).append("'");
}
}
else if(fieldValue instanceof Integer){
sqlStrBuilder.append("=").append(fieldValue);
}
}
return sqlStrBuilder.toString();

}
}


2.4 返回SQL语句的测试类

[java]
view plain
copy

print?

package dao;  
public class Test {  
    public static void main(String[] args) {  
        User u1 = new User();  
        u1.setId(9);  //查询id为9的用户  
          
        User u2 = new User();  
        u2.setUserName("JTZeng");   //模糊查询用户名为JTZeng的用户  
        u2.setAge(21);  
          
        User u3 = new User();  
        u3.setEmail("123@163.com,123@qq.com");  //查询邮箱有任意一个的用户  
          
        String sql1 = ReturnQuery.query(u1);    //查询id为9的用户  
        String sql2 = ReturnQuery.query(u2);    //查询用户名为JTZeng和年龄为21的用户  
        String sql3 = ReturnQuery.query(u3);    //查询邮箱中有任意一个的用户  
          
        System.out.println(sql1);  
        System.out.println(sql2);  
        System.out.println(sql3);  
    }  
}  

package dao;
public class Test {
public static void main(String[] args) {
User u1 = new User();
u1.setId(9);  //查询id为9的用户

User u2 = new User();
u2.setUserName("JTZeng"); 	//模糊查询用户名为JTZeng的用户
u2.setAge(21);

User u3 = new User();
u3.setEmail("123@163.com,123@qq.com"); 	//查询邮箱有任意一个的用户

String sql1 = ReturnQuery.query(u1); 	//查询id为9的用户
String sql2 = ReturnQuery.query(u2);	//查询用户名为JTZeng和年龄为21的用户
String sql3 = ReturnQuery.query(u3);	//查询邮箱中有任意一个的用户

System.out.println(sql1);
System.out.println(sql2);
System.out.println(sql3);
}
}


输出结果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: