<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 楊軍威 閱讀(241) 評論(0)  編輯  收藏


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


    網站導航:
     

    導航

    統計

    常用鏈接

    留言簿

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: aaa毛片免费观看| 中文在线免费视频| 在线观看AV片永久免费| 337p欧洲亚洲大胆艺术| 久爱免费观看在线网站| 91精品国产亚洲爽啪在线影院| 免费观看男人吊女人视频| 亚洲av中文无码乱人伦在线咪咕| 青青操视频在线免费观看| 亚洲日韩国产一区二区三区| 国产精品美女久久久免费 | 凹凸精品视频分类国产品免费| 亚洲午夜精品久久久久久app | 成人午夜免费视频| 久久亚洲色一区二区三区| a毛片久久免费观看| 亚洲第一AAAAA片| 亚洲免费在线视频播放| 中文字幕亚洲综合久久综合| 国产精品国产免费无码专区不卡| 黄色免费网址在线观看| 国产亚洲一区二区手机在线观看 | 国产大片免费网站不卡美女| 亚洲综合一区二区三区四区五区 | 日本免费无遮挡吸乳视频电影| 美女露隐私全部免费直播| 国产亚洲人成A在线V网站| 久久精品电影免费动漫| 国产精品亚洲精品观看不卡| 哒哒哒免费视频观看在线www| 三级黄色免费观看| 亚洲国产成人超福利久久精品| 免费看美女被靠到爽| 精品人妻系列无码人妻免费视频| 亚洲国产一区二区a毛片| 好吊妞在线成人免费| 三级毛片在线免费观看| 在线a亚洲老鸭窝天堂av高清| 亚洲裸男gv网站| 国产精品1024永久免费视频| 美女扒开屁股让男人桶爽免费|