This example is designed to mark entities with custom annotations, to provide a basis for generating SQL automatically, to simulate hibernate annotations, and to search for the principles of annotations.
1. Define Table annotations
[Java]View PlainCopy
- Package test;
- Import java.lang.annotation.Documented;
- Import Java.lang.annotation.ElementType;
- Import java.lang.annotation.Inherited;
- Import java.lang.annotation.Retention;
- Import Java.lang.annotation.RetentionPolicy;
- Import Java.lang.annotation.Target;
- @Inherited
- @Target ({elementtype.type})
- @Retention (Retentionpolicy.runtime)
- @Documented
- Public @interface Table {
- String value () default "";
- }
2. Define Column annotations
[Java]View PlainCopy
- Package test;
- Import java.lang.annotation.Documented;
- Import Java.lang.annotation.ElementType;
- Import java.lang.annotation.Inherited;
- Import java.lang.annotation.Retention;
- Import Java.lang.annotation.RetentionPolicy;
- Import Java.lang.annotation.Target;
- @Inherited
- @Target ({Elementtype.field})
- @Retention (Retentionpolicy.runtime)
- @Documented
- Public @interface Column {
- String value () default "";
- }
3. Define entities that use annotations
[Java]View PlainCopy
- Package test;
- @Table ("Tb_test")
- Public class Testdto {
- @Deprecated
- private String tt;
- @Column ("_id")
- private String ID;
- @Column ("username")
- private String name;
- Public testdto (string ID, string name) {
- super ();
- this.id = ID;
- this.name = name;
- }
- Public String getId () {
- return ID;
- }
- public void SetId (String id) {
- this.id = ID;
- }
- Public String GetName () {
- return name;
- }
- public void SetName (String name) {
- this.name = name;
- }
- }
4. Test Notes
[Java]View PlainCopy
- Package test;
- Import Java.lang.reflect.Field;
- Import Java.lang.reflect.Method;
- Public class Test {
- public static void Main (string[] args) {
- Testdto testdto = new Testdto ("123", "34");
- Testdto testDto1 = new Testdto ("123", "test1");
- Testdto TestDto2 = new Testdto ("", " test1,test2,test3,test4");
- String sql = assemblesqlfromobj (testdto);
- String SQL1 = Assemblesqlfromobj (testDto1);
- String sql2 = Assemblesqlfromobj (TestDto2);
- SYSTEM.OUT.PRINTLN (SQL);
- System.out.println (SQL1);
- System.out.println (SQL2);
- }
- /**
- * Use annotations to assemble query conditions and generate query statements
- *
- * @param obj
- * @return
- */
- public static String assemblesqlfromobj (Object obj) {
- Table table = Obj.getclass (). getannotation (table. Class);
- StringBuffer sbsql = new StringBuffer ();
- String tableName = Table.value ();
- Sbsql.append ("SELECT * from" + TableName + "where 1=1");
- field[] fileds = Obj.getclass (). Getdeclaredfields ();
- For (Field f:fileds) {
- String fieldName = F.getname ();
- String methodName = "Get" + fieldname.substring (0, 1). toUpperCase ()
- + fieldname.substring (1);
- try {
- Column column = f.getannotation (column. Class);
- if (column! = null) {
- method = Obj.getclass (). GetMethod (MethodName);
- String value = (string) method.invoke (obj);
- if (value! = null &&!value.equals ("")) {
- if (!isnum (Column.value ()) &&!isnum (value)) {
- //Determine if the parameter is in type parameter
- if (Value.contains (",")) {
- Sbsql.append ("and" + column.value () + "in (" + Value + ")");
- } Else {
- Sbsql.append ("and" + column.value () + "like '%" + value + "% '");
- }
- } Else {
- Sbsql.append ("and" + column.value () + "=" + Value + "");
- }
- }
- }
- } catch (Exception e) {
- E.printstacktrace ();
- }
- }
- return sbsql.tostring ();
- }
- /**
- * Check that the given value is not the ID type 1. check field name 2. Check field values
- *
- * @param target
- * @return
- */
- public Static boolean isnum (String target) {
- Boolean isnum = false;
- if (Target.tolowercase (). Contains ("id")) {
- Isnum = true;
- }
- if (target.matches ("\\d+")) {
- Isnum = true;
- }
- return isnum;
- }
- }
Test results:
SELECT * from Tb_test where 1=1 and _id=123 and username=34
SELECT * from Tb_test where 1=1 and _id=123 and username like '%test1% '
SELECT * from Tb_test where 1=1 and username in (TEST1,TEST2,TEST3,TEST4)
Annotations: Java custom annotations Application examples