| java annotation demo | |
package exp;![]() import java.lang.annotation.*; import java.lang.reflect.*;![]() ![]() ![]() @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @interface PrimaryKey![]() ![]() { }![]() ![]() ![]() @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @interface TEntity![]() ![]() { String table_name(); String primary_key(); }![]() @TEntity(table_name="groups",primary_key="group_id") class Group![]() ![]() { private String m_group_id="group_id"; private String m_group_name="group_name"; @PrimaryKey public String getGroupID()![]() { return this.m_group_id; } public String setGroupID(String group_id)![]() { return this.m_group_id=group_id; } public String getGroupName()![]() { return this.m_group_name; } public String setGroupName(String group_name)![]() { return this.m_group_name=group_name; } }![]() ![]() public class Main ![]() ![]() { public static void main(String[] args) throws Exception![]() { Group g = new Group(); TEntity t=g.getClass().getAnnotation(TEntity.class); System.out.println(String.format("Group's mapping table is %s,and the table's primary key is %s",t.table_name(),t.primary_key())); Method[] methods=g.getClass().getDeclaredMethods(); for(Method method : methods)![]() { if(method.getModifiers()!=Method.PUBLIC && !method.getName().startsWith("get")) continue; PrimaryKey p = method.getAnnotation(PrimaryKey.class); if(p==null) System.out.println(String.format("%s is not primary key and value is %s",method.getName(),method.invoke(g,new Object[0]))); else System.out.println(String.format("%s is primary key and value is %s",method.getName(),method.invoke(g,new Object[0]))); } ![]() /**//* Group's mapping table is groups,and the table's primary key is group_id getGroupID is primary key and value is group_id getGroupName is not primary key and value is group_name![]() */ }![]() } |
|
| 楼主 创建:08-06-21 15:20:20 更新:08-06-21 15:20:20 | |
| 发表回复 | |
| 1/1(共1页) | |
| 大狗熊 | 1 package exc; 2 3 import java.lang.annotation.ElementType; 4 import java.lang.annotation.Retention; 5 import java.lang.annotation.RetentionPolicy; 6 import java.lang.annotation.Target; 7 8 @Retention(RetentionPolicy.RUNTIME) 9 @Target(ElementType.TYPE) 10 public @interface TableAnnotation 11 { 12 String table_name() default ""; 13 String primary_key() default "id"; 14 } 15 16 package exc; 17 18 import java.lang.annotation.ElementType; 19 import java.lang.annotation.Retention; 20 import java.lang.annotation.RetentionPolicy; 21 import java.lang.annotation.Target; 22 23 @Retention(RetentionPolicy.RUNTIME) 24 @Target(ElementType.FIELD) 25 public @interface FieldAnnotation 26 { 27 boolean select() default true; 28 boolean update() default true; 29 boolean insert() default true; 30 } 31 32 33 package exc; 34 @TableAnnotation(primary_key="id",table_name="users") 35 public class User 36 { 37 @FieldAnnotation(select=true,update=false,insert=true) 38 public String id; 39 public String name; 40 public String password; 41 public String nickname; 42 } 43 44 45 public class SQLBuilder 46 { 47 private static Class<TableAnnotation> tc = TableAnnotation.class; 48 private static Class<FieldAnnotation> fc = FieldAnnotation.class; 49 50 private static String get_table_name(Class<?> ce) 51 { 52 TableAnnotation ea = ce.getAnnotation(tc); 53 String table_name = null; 54 if (ea == null || (table_name = ea.table_name()).equals("")) 55 return ce.getSimpleName().toLowerCase().concat("s"); 56 return table_name; 57 } 58 59 private static String get_primary_key(Class<?> ce) 60 { 61 TableAnnotation ea = ce.getAnnotation(tc); 62 if (ea != null) 63 return ea.primary_key(); 64 65 return "id"; 66 } 67 68 public static String create_insert_sql(Class<?> ce) 69 { 70 String table_name = get_table_name(ce); 71 StringBuffer sbs = new StringBuffer(String.format("insert into %s (", table_name)); 72 StringBuffer sbe = new StringBuffer(") values ("); 73 Field[] fields = ce.getFields(); 74 int j = 0; 75 FieldAnnotation fa = null; 76 77 for (Field f : fields) 78 { 79 if ((fa = f.getAnnotation(fc)) != null && !fa.insert()) 80 continue; 81 82 if (j++ == 0) 83 { 84 sbs.append(f.getName()); 85 sbe.append(’?’); 86 } 87 else 88 { 89 sbs.append(String.format(",%s", f.getName())); 90 sbe.append(",?"); 91 } 92 } 93 sbe.append(’)’); 94 return sbs.append(sbe).toString(); 95 } 96 97 public static String create_update_sql(Class<?> ce) 98 { 99 int j = 0; 100 Field[] fields = ce.getFields(); 101 String table_name = get_table_name(ce),primary_key = get_primary_key(ce),field_name = null; 102 FieldAnnotation fa = null; 103 StringBuffer sb = new StringBuffer(); 104 105 sb.append("update "); 106 sb.append(table_name); 107 108 for (Field f : fields) 109 { 110 if ((field_name = f.getName()).equals(primary_key) || ((fa = f.getAnnotation(fc)) != null && !fa.update())) 111 continue; 112 if (j++ == 0) 113 sb.append(String.format(" set %s=?", field_name)); 114 else 115 sb.append(String.format(",%s=?", field_name)); 116 } 117 118 sb.append(String.format(" where %s=?", primary_key)); 119 return sb.toString(); 120 } 121 122 public static String create_delete_sql(Class<?> ce) 123 { 124 return String.format("delete from %s where %s=?", get_table_name(ce), get_primary_key(ce)); 125 } 126 127 public static String create_select_all_sql(Class<?> ce) 128 { 129 FieldAnnotation fa = null; 130 StringBuffer sb = new StringBuffer(); 131 Field[] fields = ce.getFields(); 132 int j = 0; 133 134 for (Field f : fields) 135 { 136 if ((fa = f.getAnnotation(fc)) != null && !fa.select()) 137 continue; 138 if (j++ == 0) 139 sb.append(f.getName()); 140 else 141 sb.append(String.format(",%s", f.getName())); 142 } 143 144 return String.format("select %s from %s", sb, get_table_name(ce)); 145 } 146 147 public static String create_find_by_primary_key_sql(Class<?> ce) 148 { 149 return create_find_by_field_sql(ce,get_primary_key(ce)); 150 } 151 152 public static String create_find_by_field_sql(Class<?> ce,String field_name) 153 { 154 return String.format("%s where %s=?", create_select_all_sql(ce), field_name); 155 } 156 } |
| 1楼 创建:08-07-24 17:50:30 更新:08-07-24 17:50:59 | |
| 发表回复 | |