Annotations: Java custom annotations Application examples

Source: Internet
Author: User

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
  1. Package test;
  2. Import java.lang.annotation.Documented;
  3. Import Java.lang.annotation.ElementType;
  4. Import java.lang.annotation.Inherited;
  5. Import java.lang.annotation.Retention;
  6. Import Java.lang.annotation.RetentionPolicy;
  7. Import Java.lang.annotation.Target;
  8. @Inherited
  9. @Target ({elementtype.type})
  10. @Retention (Retentionpolicy.runtime)
  11. @Documented
  12. Public @interface Table {
  13. String value () default "";
  14. }


2. Define Column annotations

[Java]View PlainCopy
  1. Package test;
  2. Import java.lang.annotation.Documented;
  3. Import Java.lang.annotation.ElementType;
  4. Import java.lang.annotation.Inherited;
  5. Import java.lang.annotation.Retention;
  6. Import Java.lang.annotation.RetentionPolicy;
  7. Import Java.lang.annotation.Target;
  8. @Inherited
  9. @Target ({Elementtype.field})
  10. @Retention (Retentionpolicy.runtime)
  11. @Documented
  12. Public @interface Column {
  13. String value () default "";
  14. }


3. Define entities that use annotations

[Java]View PlainCopy
  1. Package test;
  2. @Table ("Tb_test")
  3. Public class Testdto {
  4. @Deprecated
  5. private String tt;
  6. @Column ("_id")
  7. private String ID;
  8. @Column ("username")
  9. private String name;
  10. Public testdto (string ID, string name) {
  11. super ();
  12. this.id = ID;
  13. this.name = name;
  14. }
  15. Public String getId () {
  16. return ID;
  17. }
  18. public void SetId (String id) {
  19. this.id = ID;
  20. }
  21. Public String GetName () {
  22. return name;
  23. }
  24. public void SetName (String name) {
  25. this.name = name;
  26. }
  27. }


4. Test Notes

[Java]View PlainCopy
  1. Package test;
  2. Import Java.lang.reflect.Field;
  3. Import Java.lang.reflect.Method;
  4. Public class Test {
  5. public static void Main (string[] args) {
  6. Testdto testdto = new Testdto ("123", "34");
  7. Testdto testDto1 = new Testdto ("123", "test1");
  8. Testdto TestDto2 = new Testdto ("", " test1,test2,test3,test4");
  9. String sql = assemblesqlfromobj (testdto);
  10. String SQL1 = Assemblesqlfromobj (testDto1);
  11. String sql2 = Assemblesqlfromobj (TestDto2);
  12. SYSTEM.OUT.PRINTLN (SQL);
  13. System.out.println (SQL1);
  14. System.out.println (SQL2);
  15. }
  16. /** 
  17. * Use annotations to assemble query conditions and generate query statements
  18. *
  19. * @param obj
  20. * @return
  21. */
  22. public static String assemblesqlfromobj (Object obj) {
  23. Table table = Obj.getclass (). getannotation (table.   Class);
  24. StringBuffer sbsql = new StringBuffer ();
  25. String tableName = Table.value ();
  26. Sbsql.append ("SELECT * from" + TableName + "where 1=1");
  27. field[] fileds = Obj.getclass (). Getdeclaredfields ();
  28. For (Field f:fileds) {
  29. String fieldName = F.getname ();
  30. String methodName = "Get" + fieldname.substring (0, 1). toUpperCase ()
  31. + fieldname.substring (1);
  32. try {
  33. Column column = f.getannotation (column.   Class);
  34. if (column! = null) {
  35. method = Obj.getclass (). GetMethod (MethodName);
  36. String value = (string) method.invoke (obj);
  37. if (value! = null &&!value.equals ("")) {
  38. if (!isnum (Column.value ()) &&!isnum (value)) {
  39. //Determine if the parameter is in type parameter
  40. if (Value.contains (",")) {
  41. Sbsql.append ("and" + column.value () + "in (" + Value + ")");
  42. } Else {
  43. Sbsql.append ("and" + column.value () + "like '%" + value + "% '");
  44. }
  45. } Else {
  46. Sbsql.append ("and" + column.value () + "=" + Value + "");
  47. }
  48. }
  49. }
  50. } catch (Exception e) {
  51. E.printstacktrace ();
  52. }
  53. }
  54. return sbsql.tostring ();
  55. }
  56. /** 
  57. * Check that the given value is not the ID type 1. check field name 2. Check field values
  58. *
  59. * @param target
  60. * @return
  61. */
  62. public Static boolean isnum (String target) {
  63. Boolean isnum = false;
  64. if (Target.tolowercase (). Contains ("id")) {
  65. Isnum = true;
  66. }
  67. if (target.matches ("\\d+")) {
  68. Isnum = true;
  69. }
  70. return isnum;
  71. }
  72. }


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

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.