java學習

          java學習

           

          java自定義annotation模仿hibernate注解實體和表對象

          package com.annotation;
          import static java.lang.annotation.ElementType.FIELD;
          import static java.lang.annotation.ElementType.METHOD;
          import java.lang.annotation.Documented;
          import java.lang.annotation.Retention;
          import java.lang.annotation.RetentionPolicy;
          import java.lang.annotation.Target;
          @Documented
          @Retention(RetentionPolicy.RUNTIME)
          @Target(value={ FIELD, METHOD})
          public @interface Column {
          String ColumnName();
          }
          package com.annotation;
          import static java.lang.annotation.ElementType.FIELD;
          import static java.lang.annotation.ElementType.METHOD;
          import java.lang.annotation.Documented;
          import java.lang.annotation.Retention;
          import java.lang.annotation.RetentionPolicy;
          import java.lang.annotation.Target;
          @Documented
          @Retention(RetentionPolicy.RUNTIME)
          @Target(value={ FIELD, METHOD})
          public @interface Id {
          }
          package com.annotation;
          import static java.lang.annotation.ElementType.TYPE;
          import java.lang.annotation.Documented;
          import java.lang.annotation.Retention;
          import java.lang.annotation.RetentionPolicy;
          import java.lang.annotation.Target;
          @Documented
          @Retention(RetentionPolicy.RUNTIME)
          @Target(value={  TYPE})
          public @interface Table {
          String tableName();
          }
          package com;
          import java.lang.reflect.Field;
          import java.lang.reflect.ParameterizedType;
          import java.lang.reflect.Type;
          import com.annotation.Column;
          import com.annotation.Id;
          import com.annotation.Table;
          public class BaseDao<T,Pk> {
          private Class<T> persistentClass;
          @SuppressWarnings("unused")
          private Class<Pk> persistentPK;
          private String tableName;//表名稱
          private String id;//主鍵
          public BaseDao() {
          ParameterizedType ptype=(ParameterizedType) this.getClass().getGenericSuperclass();
          Type[] types = ptype.getActualTypeArguments();
          for (Type type : types) {
          System.out.println(type.toString());
          }
          this.persistentPK = (Class<Pk>) types[1];
          this.persistentClass = (Class<T>) types[0];
          Table table = this.persistentClass.getAnnotation(Table.class);
          tableName=table.tableName();
          Field[] fields = this.persistentClass.getDeclaredFields();
          for (Field field : fields) {
          field.setAccessible(true);
          Id annotationId = field.getAnnotation(Id.class);
          if(annotationId != null){
          Column annotationCo = field.getAnnotation(Column.class);
          id=annotationCo.ColumnName();
          break;
          }
          }
          }
          public T getT(T t){
          System.out.println(tableName);
          System.out.println(id);
          return t;
          }
          }
          package com;
          import com.annotation.Column;
          import com.annotation.Id;
          import com.annotation.Table;
          @Table(tableName = "t_user")
          public class User {
          @Id
          @Column(ColumnName = "uid")
          private String id="1";
          public String getId()  {
          return id;
          }
          public void setId(String id) {
          this.id = id;
          }
          public User(String id) {
          super();
          this.id = id;
          System.out.println("有參數");
          }
          public User() {
          System.out.println("沒有參數");
          }
          }
          package com;
          public class UserDao extends BaseDao<User, String>{
          }

          posted on 2017-11-14 17:15 楊軍威 閱讀(244) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 中江县| 横山县| 台湾省| 沙坪坝区| 霍城县| 临武县| 龙胜| 扎兰屯市| 博爱县| 防城港市| 辽阳市| 密山市| 绍兴县| 绥中县| 万荣县| 河东区| 萨迦县| 麦盖提县| 基隆市| 南部县| 西昌市| 剑川县| 北流市| 郧西县| 海原县| 蓝田县| 阳高县| 襄樊市| 油尖旺区| 中西区| 台中市| 怀仁县| 平潭县| 平乐县| 丘北县| 普格县| 宜川县| 瓮安县| 南部县| 康马县| 阿克|