java學(xué)習(xí)

          java學(xué)習(xí)

           

          java自定義annotation模仿hibernate注解實(shí)體和表對(duì)象

          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;//表名稱(chēng)
          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("沒(méi)有參數(shù)");
          }
          }
          package com;
          public class UserDao extends BaseDao<User, String>{
          }

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


          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


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

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 荣成市| 绥化市| 广昌县| 广宗县| 庆城县| 宜春市| 镇坪县| 桐柏县| 旬邑县| 马公市| 铁力市| 肇东市| 项城市| 胶南市| 江北区| 东方市| 日照市| 龙海市| 鄂伦春自治旗| 邹平县| 田阳县| 许昌市| 阿拉善右旗| 阿城市| 鄂温| 遂昌县| 土默特右旗| 和龙市| 温州市| 怀安县| 渝北区| 客服| 望奎县| 靖安县| 壤塘县| 高邑县| 来安县| 建宁县| 万载县| 平江县| 永平县|