java學(xué)習(xí)

          java學(xué)習(xí)

           

          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("有參數(shù)");
          }
          public User() {
          System.out.println("沒有參數(shù)");
          }
          }
          package com;
          public class UserDao extends BaseDao<User, String>{
          }

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


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


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 秦皇岛市| 邓州市| 简阳市| 太原市| 绥中县| 安西县| 六盘水市| 安义县| 马尔康县| 凤阳县| 隆昌县| 安达市| 无锡市| 盱眙县| 南昌县| 樟树市| 武夷山市| 灌云县| 胶南市| 调兵山市| 珲春市| 曲沃县| 海伦市| 正定县| 饶平县| 南陵县| 青冈县| 雅安市| 吴江市| 浪卡子县| 汕头市| 林口县| 丹巴县| 包头市| 榕江县| 馆陶县| 建湖县| 华容县| 瑞丽市| 龙里县| 云林县|