注解是JDK 1.5之后引入的一个语言特性,一般用于对类,方法,属性的修饰。主要的目的在于简化java中的配置,用注解代替配置。 下面是几个Java底层内置的几个注解
Copy package java . lang ;
import java . lang . annotation . * ;
import static java . lang . annotation . ElementType . * ;
/**
* A program element annotated @Deprecated is one that programmers
* are discouraged from using, typically because it is dangerous,
* or because a better alternative exists. Compilers warn when a
* deprecated program element is used or overridden in non-deprecated code.
*
* @author Neal Gafter
* @since 1.5
* @jls 9.6.3.6 @Deprecated
*/
@ Documented
@ Retention ( RetentionPolicy . RUNTIME )
@ Target (value = {CONSTRUCTOR , FIELD , LOCAL_VARIABLE , METHOD , PACKAGE , PARAMETER , TYPE})
public @ interface Deprecated {
}
注解编程其实就是使用注解的特性进行编程的一个技巧,主要包含两个核心点:注解的定义,反射判断 注解的定义就是定义自己需要使用的注解 反射判断表示在适当的实际反射获取注解的值进行相应逻辑的处理
Copy [src / anotations / Table . java ]
package anotaions ;
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() ;
}
Copy [src / anotations / Column . java ]
package anotaions ;
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() ;
}
Copy [src / Student . java ]
import anotaions . Column ;
import anotaions . Table ;
@ Table ( "student" )
public class Student {
@ Column ( "id" )
private int id;
@ Column ( "user_name" )
private String username;
@ Column ( "age" )
private int age;
@ Column ( "city" )
private String city;
@ Column ( "phone" )
private String phone;
public int getId () {
return id;
}
public String getUsername () {
return username;
}
public int getAge () {
return age;
}
public String getCity () {
return city;
}
public String getPhone () {
return phone;
}
public void setId ( int id) {
this . id = id;
}
public void setUsername ( String username) {
this . username = username;
}
public void setAge ( int age) {
this . age = age;
}
public void setCity ( String city) {
this . city = city;
}
public void setPhone ( String phone) {
this . phone = phone;
}
}
Copy [src / DBDriver . java ]
import anotaions . Column ;
import anotaions . Table ;
import java . lang . reflect . Field ;
import java . lang . reflect . Method ;
public class DBDriver {
public static String query ( Student student) throws Exception {
StringBuilder sb = new StringBuilder() ;
Class < ? > clazz = student . getClass ();
boolean tableAnnoExits = clazz . isAnnotationPresent ( Table . class );
// 判断 class 对象上是否有 @Table 注解
if ( ! tableAnnoExits) {
return null ;
}
// 获取 @Table 注解中的表名,拼接 SQl
Table table = clazz . getAnnotation ( Table . class );
String tableName = table . value ();
sb . append ( "SELECT * FROM " + tableName + " WHERE 1 = 1" );
// 获取 class 对象中的所有字段并遍历
Field [] fields = clazz . getDeclaredFields ();
for ( Field field : fields) {
boolean fieldAnnoExits = field . isAnnotationPresent ( Column . class );
// 判断字段上是否含有 @Column 注解
if ( ! fieldAnnoExits) {
continue ;
}
// 获取注解中的字段名
Column column = field . getAnnotation ( Column . class );
String columnName = column . value ();
// 通过 get() 方法获取传入的字段值
String fieldName = field . getName ();
String getMethodName = "get" + fieldName . substring ( 0 , 1 ) . toUpperCase ()
+ fieldName . substring ( 1 );
Method method = clazz . getMethod (getMethodName);
Object fieldValue = method . invoke (student);
if (fieldValue == null
|| (fieldValue instanceof Integer && (Integer)fieldValue == 0 )) {
continue ;
}
// 接着拼接 SQL 语句
sb . append ( " AND " ) . append (columnName) . append ( " = " );
if (fieldValue instanceof String) {
sb . append ( "'" ) . append (fieldValue) . append ( "'" );
} else if (fieldValue instanceof Integer) {
sb . append (fieldValue);
}
}
return sb . toString ();
}
public static void main ( String [] args) throws Exception {
Student student1 = new Student() ;
student1 . setUsername ( "张三" );
student1 . setAge ( 18 );
student1 . setCity ( "hangzhou" );
String s1 = query(student1) ;
System . out . println (s1);
Student student2 = new Student() ;
student2 . setUsername ( "李四" );
student2 . setPhone ( "12345678977" );
String s2 = query(student2) ;
System . out . println (s2);
}
}