Java annotation demo ing Table Generation example from thinking in java 4 Chapter 20 code
Thinking in java 4 free download: http://download.csdn.net/detail/liangrui1988/7580155
Package org. rui. annotation. database; import java. lang. annotation. *;/*** generate a database table * @ author lenovo **/@ Target (ElementType. TYPE) @ Retention (RetentionPolicy. RUNTIME) public @ interface DBTable {public String name () default "";}
Package org. rui. annotation. database; import java. lang. annotation. elementType; import java. lang. annotation. retention; import java. lang. annotation. retentionPolicy; import java. lang. annotation. target;/*** SQL type ** @ author lenovo **/@ Target (ElementType. FIELD) @ Retention (RetentionPolicy. RUNTIME) public @ interface SQLString {int value () default 0; String name () default ""; Constraints constraints () default @ Constraints ;}
Package org. rui. annotation. database; import java. lang. annotation. elementType; import java. lang. annotation. retention; import java. lang. annotation. retentionPolicy; import java. lang. annotation. target;/*** SQL type ** @ author lenovo **/@ Target (ElementType. FIELD) @ Retention (RetentionPolicy. RUNTIME) public @ interface SqlInteger {int value () default 0; String name () default ""; Constraints constraints () default @ Constraints ;}
Package org. rui. annotation. database; import java. lang. annotation. elementType; import java. lang. annotation. retention; import java. lang. annotation. retentionPolicy; import java. lang. annotation. target;/*** annotation prepared by the javaBean field * @ author lenovo **/@ Target (ElementType. FIELD) @ Retention (RetentionPolicy. RUNTIME) public @ interface Constraints {boolean primaryKey () default false; boolean allowNull () default true; boolean unique () default false ;}
Package org. rui. annotation. database;/*** if the unique () element in the @ Constraints annotation to be embedded is true, use this element as constraints () the default value of the element * needs to be defined as follows * @ author lenovo **/public @ interface Uniqueness {Constraints constraints () default @ Constraints (unique = true );}
package org.rui.annotation.database;@DBTable(name="MEMBER")public class Member {@SQLString(30)String firstName;@SQLString(50) String lastName;@SqlInteger Integer age;@SQLString(value=30,constraints=@Constraints(primaryKey=true))String handle;static int memberCount;//getpublic String getFirstName() {return firstName;}public String getLastName() {return lastName;}public Integer getAge() {return age;}public String getHandle() {return handle;}@Overridepublic String toString(){return handle;}}
Package org. rui. annotation. database; import java. io. objectInputStream. getField; import java. lang. annotation. annotation; import java. lang. reflect. field; import java. util. arrayList; import java. util. list; /*** For more information, see chapter 20 of thinking in java 4th * @ author lenovo **/public class TableCreator {// obtain the public static String getConstraints (constraints con) {String constraints = ""; if (con. allowNull () constraint S + = "not null"; if (con. primaryKey () constraints + = "primary key"; if (con. unique () constraints + = "UNIQUE"; return constraints;} public static void main (String [] args) throws ClassNotFoundException {String [] arr = new String [] {"org. rui. annotation. database. member "}; if (arr. length <1) {System. out. println ("0000"); System. exit (0) ;}for (String className: arr) {Class <?> Clzz = Class. forName (className); DBTable table = clzz. getAnnotation (DBTable. class); if (table = null) {System. out. println ("dbtable annotaions in class:" + className); continue;} String tableName = table. name (); if (tableName. length () <1) tableName = clzz. getName (). toUpperCase (); // result set List <String> columnDefs = new ArrayList <String> (); // obtain all fields for (Field f: clzz. getDeclaredFields () {String columnName = null; // obtain the Annotation [] annot = f. getAnnotations (); if (annot. length <1) continue; // If SqlIntegerif (annot [0] instanceof SqlInteger) {SqlInteger sInt = (SqlInteger) annot [0]; // use the field name if (sInt. name (). length () <1) {columnName = f. getName (). toUpperCase ();} else {columnName = sInt. name ();} columnDefs. add (columnName + "INT" + getConstraints (sInt. constraints ();} // if it is a string internal if (annot [0] instanceof SQLString) {SQLString sStr = (SQLString) annot [0]; if (sStr. name (). length () <1) {columnName = f. getName (). toUpperCase ();} else {columnName = sStr. name ();} columnDefs. add (columnName + "VARCHAR (" + sStr. value () + ")" + getConstraints (sStr. constraints ();} StringBuilder SQL = new StringBuilder ("create table" + tableName + "("); for (String c: columnDefs) {SQL. append ("\ n" + c + ","); // concatenate a field // remove trailing commaString tableCreate = SQL. substring (0, SQL. length ()-1) + ");"; System. out. println ("table ceration SQL fro" + className + "is: \ n" + tableCreate) ;}}}/ ** output: table ceration SQL fro org. rui. annotation. database. member is: create table member (firstname varchar (30) not null); table ceration SQL fro org. rui. annotation. database. member is: create table member (firstname varchar (30) not null); table ceration SQL fro org. rui. annotation. database. member is: create table member (firstname varchar (30) not null, lastname varchar (50) not null); table ceration SQL fro org. rui. annotation. database. member is: create table member (firstname varchar (30) not null); table ceration SQL fro org. rui. annotation. database. member is: create table member (firstname varchar (30) not null, lastname varchar (50) not null); table ceration SQL fro org. rui. annotation. database. member is: create table member (firstname varchar (30) not null, lastname varchar (50) not null, age intnot null); table ceration SQL fro org. rui. annotation. database. member is: create table member (firstname varchar (30) not null); table ceration SQL fro org. rui. annotation. database. member is: create table member (firstname varchar (30) not null, lastname varchar (50) not null); table ceration SQL fro org. rui. annotation. database. member is: create table member (firstname varchar (30) not null, lastname varchar (50) not null, age intnot null); table ceration SQL fro org. rui. annotation. database. member is: create table member (firstname varchar (30) not null, lastname varchar (50) not null, age intnot null, handle varchar (30) not nullprimary key );*/