Write SQL statements for table creation using annotations, and compile SQL statements using annotations

Source: Internet
Author: User

Write SQL statements for table creation using annotations, and compile SQL statements using annotations

I read the think in java chapter tonight and it feels good. I just knocked it down and pasted the code for a further review:

We recommend that you read the think in java annotation in advance.

Note: I created an annotation under the first annotation description, and subsequent annotations are not described. '

DBTable annotation:

/*** Project Name: myannotation * File Name: DBTable. java * Package Name: com. iflytek. db * Date: 08:20:54 * Copyright (c) 2016, syzhao@iflytek.com All Rights Reserved. **/package com. iflytek. db; import java. lang. annotation. elementType; import java. lang. annotation. retention; import java. lang. annotation. retentionPolicy; import java. lang. annotation. target;/** @ Target: @ Target indicates the range of objects modified by Annotation: Annotat Ion can be used for packages, types (class, interface, enumeration, Annotation type), type members (methods, constructor, member variables, and enumeration values) method parameters and local variables (such as loop variables and catch parameters ). In the Annotation type declaration, target can be used to better clarify the object to be modified. Purpose: Describe the scope of use of the annotation (that is, where the description can be used) value (ElementType): 1. CONSTRUCTOR: used to describe the CONSTRUCTOR 2. FIELD: used to describe the domain 3. LOCAL_VARIABLE: used to describe local variables 4. METHOD: used to describe METHOD 5. PACKAGE: used to describe the PACKAGE 6. PARAMETER: used to describe PARAMETER 7. TYPE: used to describe classes, interfaces (including Annotation types), or enum declarations @ Retention: @ Retention defines the duration of Annotation Retention: Some Annotation only appears in the source code, while others are compiled in the class file. The Annotation compiled in the class file may be ignored by the virtual machine, others will be read when the class is loaded (note that the execution of the class is not affected because the Annotation and class are separated in use ). This meta-Annotation can be used to limit the "Life Cycle" of Annotation. Purpose: indicates the level at which the annotation information needs to be saved. It is used to describe the life cycle of the annotation (that is, the scope of the description Description in which the annotation is valid) values (RetentionPoicy) include: 1. SOURCE: valid in the SOURCE file (that is, the SOURCE file is retained) 2. CLASS: valid in the class file (that is, the class is retained) 3. RUNTIME: valid at RUNTIME (that is, reserved during RUNTIME) Retention meta-annotation type has a unique value as a member, and its value comes from java. lang. annotation. enumeration type value of RetentionPolicy */@ Target (ElementType. TYPE) @ Retention (RetentionPolicy. RUNTIME) public @ interface DBTable {public String name () default "";}

Constraints constraint annotation:

/*** Project Name: myannotation * File Name: Constraints. java * Package Name: com. iflytek. db * Date: 08:27:08 * Copyright (c) 2016, syzhao@iflytek.com All Rights Reserved. **/package com. iflytek. db; 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 Constraints {boolean primaryKey () default false; boolean allowNull () default true; boolean unique () default false ;}

SQLInteger int annotation:

/*** Project Name: myannotation * File Name: SQLInteger. java * Package Name: com. iflytek. db * Date: 10:24:11 * Copyright (c) 2016, syzhao@iflytek.com All Rights Reserved. **/package com. iflytek. db; 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 SQLInteger {String name () default ""; Constraints constraints () default @ Constraints ;}

SQLString character annotation:

/*** Project Name: myannotation * File Name: SQLString. java * Package Name: com. iflytek. db * Date: 10:28:04 * Copyright (c) 2016, syzhao@iflytek.com All Rights Reserved. **/package com. iflytek. db; 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 SQLString {int value () default 0; String name () default ""; Constraints constraints () default @ Constraints ;}

Table creation processor:

/*** Project Name: myannotation * File Name: TableCreator. java * Package Name: com. iflytek. table * Date: 10:57:52 * Copyright (c) 2016, syzhao@iflytek.com All Rights Reserved. **/package com. iflytek. table; import java. lang. annotation. annotation; import java. lang. reflect. field; import java. util. arrayList; import java. util. list; import com. iflytek. db. constraints; import com. iflytek. db. DBTable; import com. Iflytek. db. SQLInteger; import com. iflytek. db. SQLString; public class TableCreator {public static void main (String [] args) {createTable (Member. class);} // create a table SQL statement private static void createTable (Class <?> Cl) {// obtain the DBTable annotation DBTable dbTable = cl. getAnnotation (DBTable. class); // determine whether the DBTable annotation exists if (dbTable = null) {System. out. println ("about DBTable not found"); return;} // If the @ DBTable annotation is obtained, it indicates String tableName = dbTable. name (); // determines whether the table name exists if (tableName. length () <1) {// does not exist. It indicates that the class name is used by default. getSimpleName () gets the class name and uppercase tableName = cl. getSimpleName (). toUpperCase ();} // defines the List of containers for obtaining the column <String> columnDefs = new Array List <String> (); // cyclic attribute field // Description: getDeclaredFields () obtains all declarative fields of a class, including public, private, and proteced, but does not include the declarative fields of the parent class. // GetFields () obtains all the public fields of a class, including the parent class. For (Field field: cl. getDeclaredFields () {// define the table field name variable String columnName = null; // obtain the Annotation on the field (currently the field allows multiple annotations, so the returned array is) annotation [] anns = field. getDeclaredAnnotations (); // determines whether the property has an annotation if (anns. length <1) continue; // determine whether the data type we have defined is if (anns [0] instanceof SQLInteger) {// obtain SQLInteger annotation SQLInteger sInt = (SQLInteger) anns [0]; // determines whether the name of the annotation has a value if (sInt. name (). length () <1) {// if there is no value, it indicates that it is an attribute field of the class. Obtain the attribute and convert it to uppercase. ColumnName = field. getName (). toUpperCase ();} else {// if there is a value, obtain the set name value columnName = sInt. name ();} // put it in the container of the attribute columnDefs. add (columnName + "INT" + getConstraints (sInt. constraints ();} // same as SQLInteger. if (anns [0] instanceof SQLString) {SQLString sString = (SQLString) anns [0] is not commented here; if (sString. name (). length () <1) {columnName = field. getName (). toUpperCase ();} else {columnName = sString. name ();} ColumnDefs. add (columnName + "VARCHAR (" + sString. value () + ")" + getConstraints (sString. constraints ();} // define the SQL statement StringBuilder createCommand = new StringBuilder ("CREATE TABLE" + tableName + "("); // loop the preceding attribute container, for (String columnDef: columnDefs) {// Add the attribute to the SQL statement createCommand. append ("\ n" + columnDef + ","); // remove the last comma String tableCreate = createCommand. substring (0, createCommand. le Ngth ()-1) + ");"; // print System. out. println ("Table creation SQL for" + cl. getName () + "is: \ n" + tableCreate) ;}} private static String getConstraints (Constraints con) {String constraints = ""; // determine whether it is null if (! Con. allowNull () {constraints + = "not null";} // determines whether it is the primary key if (con. primaryKey () {constraints + = "primary key";} // if (con. unique () {constraints + = "UNIQUE";} return constraints ;}}

Copy the above Code to run it!

Although the above is a simple table creation statement, we can spread to the annotations in the domain class of hibernate, and the various Curds are not handled in this way, but hibernate has many things, however, there will be a chance to study hibernate in the future.

Gains:

After reading the annotation, I know why it is necessary to use the annotation. As the name suggests, it is an annotation, but there is a processor to process this annotation. This should be helpful for the annotation I will use later,

It's not too early to write it here!

Result:

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.