Java annotations are widely used in web frameworks, which makes it easier to configure relationships between objects
The two most commonly used configuration object dependencies in the Web framework are the annotations and XML configuration file methods, which are relatively less common in the API configuration.
The following implements a table annotation to implement the correspondence between the database table and the entity bean, Implementing a column annotation to implement each field in the database table and the entity bean between each property
The corresponding relationship. The ORM in Java is basically based on this idea.
Table Callout Code:
Package Com.panther.dong.annotation.anno;import java.lang.annotation.*;/** * Created is panther on 15-8-3. */@Target ({elementtype.type}) @Retention (retentionpolicy.runtime) @Inherited @documentedpublic @interface Table { String value () Default "";}
Column Comment Code:
Package Com.panther.dong.annotation.anno;import java.lang.annotation.*;/** * Created is panther on 15-8-3. */@Target ({Elementtype.field}) @Retention (retentionpolicy.runtime) @Inherited @documentedpublic @interface Column { String value () Default "";}
Create a student entity bean and give the bean a table annotation and a column annotation
Package Com.panther.dong.annotation.pojo;import Com.panther.dong.annotation.anno.column;import com.panther.dong.annotation.anno.table;/** * Created by Panther on 15-8-3. */@Table ("Student") public class Student {@Column ("id") private int id; @Column ("name") private String name; @Column ("age") private int age; @Column ("Sex") Private String sex; Public Student () {} public Student (int ID, string name, Int. age, String sex) {this.id = ID; THIS.name = name; This.age = age; This.sex = sex; } public int getId () {return id; } public void setId (int id) {this.id = ID; } public String GetName () {return name; } public void SetName (String name) {this.name = name; } public int Getage () {return age; public void Setage (int.) {this.age = age; } public String Getsex () {return sex; } public void Setsex (String sex) {this.sex = SEX }}
Gets the name of the table in the corresponding database in the table annotation student by reflection, obtains the field in the attribute database in the bean by the column annotation, obtains the property in the runtime entity bean by reflection
Of the value, pieced together out of SQL. The code is as follows:
Package Com.panther.dong.annotation.parse;import Com.panther.dong.annotation.anno.column;import Com.panther.dong.annotation.anno.table;import Com.panther.dong.annotation.pojo.student;import java.lang.reflect.field;/** * Created by Panther on 15-8-3. */public class Parsepojo {private final static String Query_sql = "select * from"; /** * Parsing Annotations * * @param object Annotated objects * @return */Public String Getselect (Object object) {St Ring sql = Query_sql; Get the name of the table you want to query Class CLS = Object.getclass (); Boolean istable = Cls.isannotationpresent (Table.class); if (istable) {Table tableName = (table) cls.getannotation (Table.class); SQL + = Tablename.value (); }//Patchwork SQL if (Object! = null) {SQL + = "where"; }//Get property corresponding to the name of the annotation and the value of the property runtime field[] fields = Cls.getdeclaredfields (); for (Field field:fields) {field.setaccessible (true); Object val = Null try {val = (object) field.get (object); } catch (Exception e) {System.out.println ("Get Fileld value failure"); } Boolean iscolumn = Field.isannotationpresent (Column.class); if (iscolumn) {column ColumnName = (column) field.getannotation (Column.class); if (val! = null && val instanceof String) {sql + = Columnname.value () + "=\ '" + val + "\ ' && amp; ";} else if (val! = null && val instanceof Integer) {sql + = Columnname.value () + "=" + val + "& ;& "; }}} sql = sql.substring (0, Sql.length ()-2); return SQL; } public static void Main (string[] args) {Student Student = new Student (); Student.setage (19); Student.setname ("Panther"); String sql = new Parsepojo (). Getselect (student); SYSTEM.OUT.PRINTLN (SQL); }}
Run the program and get the result:
Run the result to get the SQL statement, and then query the database based on SQL statements to get RESULTS!!! (subsequent query of the database should be very simple)
The directory structure of the code is:
Java reflection and annotations are useful in configuring the configuration between the framework's class relationships and the resources
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Annotations Example