The benefit of developing with annotations is to reduce the use of configuration files. In practice, as projects become more complex and more functional, there are a lot of configuration files. However, when there are too many configuration files, the problems that arise during the actual maintenance process are not easy to locate, which in vain increases the workload. The use of annotations to develop, can reduce the use of configuration files, convenient code maintenance, at the same time, in the development speed also has a significant increase, therefore, learn to use annotation development, it is necessary to master a skill.
The following shows you the process of developing an automated SQL statement using annotations.
First, you define an entity class that is used to map to database fields, and the database field names and entity class variable names remain consistent for convenience.
Package com.huawei.andrid.net.annotation; @Table (' Person ') public class person{@Column ("name") Private String name;@ Column ("Sex") Private String Sex, @Column ("id") private int ID, @Column ("age") private int age;public string GetName () { return name; public void SetName (String name) {this.name = name;} Public String Getsex () {return sex;} public void Setsex (String sex) {this.sex = sex;} public int getId () {return ID;} public void setId (int id) {this.id = ID;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}
Here we define annotations for this entity class, including @table and @column two annotations.
Package Com.huawei.andrid.net.annotation;import static Java.lang.annotation.elementtype.type;import static Java.lang.annotation.retentionpolicy.runtime;import Java.lang.annotation.documented;import Java.lang.annotation.retention;import java.lang.annotation.Target; @Documented @retention (RUNTIME) @Target (TYPE) Public @interface table{public String value ();}
Meta-Annotations:
Support for annotations when generating Javadoc @Documented
Life cycle of @Retention (RUNTIME) annotations
@Target (type) annotation on the class
Package Com.huawei.andrid.net.annotation;import static Java.lang.annotation.elementtype.field;import static Java.lang.annotation.retentionpolicy.runtime;import Java.lang.annotation.documented;import Java.lang.annotation.retention;import java.lang.annotation.Target; @Documented @retention (RUNTIME) @Target (FIELD) Public @interface column{public String value ();}
Finally, we need to parse the annotations, using the Java reflection mechanism.
private static String query (Object p) throws Nosuchmethodexception, SecurityException, Illegalaccessexception, IllegalArgumentException, Invocationtargetexception{stringbuilder str = new StringBuilder ();//Get Class object by reflection, To get the value of the note class<? Extends object> obj = P.getclass ();//The class on which the object is judged has no annotations @tableboolean isexiststable = obj.isannotationpresent ( Table.class); if (!isexiststable) {return null;} Gets the table annotation, and gets the value of the note, which is the table name Obj.getannotation (table.class); String tableName = Table.value ();//Assemble Sqlstr.append ("select * from"). Append (TableName). Append ("where 1=1");// Gets all the member variables and iterates over the annotation value on the member variable field[] fields = Obj.getdeclaredfields (); for Field field:fields {Boolean Isexistcolumn = Field.isannotationpresent (Column.class); if (!isexistcolumn) {continue;} Gets the annotation value on the member variable, column column = Field.getannotation (column.class); String columnName = Column.value ();//Gets the member variable's Get method name string methodName = "Get" + columnname.substring (0, 1). toUpperCase () + Columnname.substring (1);//Gets the member variable of the Get method by way method = Obj.getmethod (methodName);//The Get method that executes the member variable, the argument is Object value = Method.invoke (p),//filter out the null value in the member variable, and 0if (null = = value || (Value instanceof integer && (integer) value = = 0)) {continue;} Str.append ("and"). Append (ColumnName). Append ("="). Append (value); return str.tostring ();}
Test:
public static void Main (string[] args) {person p = new person ();p. SetName ("Wanglu");p. Setage (25); String querysql = null;try{querysql = Query (p);} catch (Nosuchmethodexception | SecurityException | illegalaccessexception | illegalargumentexception| InvocationTargetException e) {e.printstacktrace ();} System.out.println (querysql);}
Developing automatic SQL generation using Java annotations