zoukankan      html  css  js  c++  java
  • 纯手写ORM框架

    必须掌握Java的反射机制和自定义注解

    package com.wyh.test;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    import java.lang.reflect.Field;
    
    //注解是对应表的关联
    @Retention(RetentionPolicy.RUNTIME)
    @interface Table {
    String value();
    }
    
    //属性对应注解
    @Retention(RetentionPolicy.RUNTIME)
    @interface Propety {
    String name();
    
    int leng() default 0;
    
    }
    @Table("student")
    class Student 
    {
    @Propety(name = "student_id", leng = 10)
    private String studentId;
    @Propety(name = "student_name")
    private String studentName;
    @Propety(name = "student_age")
    private String studentAge;
    public String getStudentId() {
    
    return studentId;
    }
    public void setStudentId(String studentId) {
    
    this.studentId = studentId;
    }
    public String getStudentName() {
    
    return studentName;
    }
    public void setStudentName(String studentName) {
    
    this.studentName = studentName;
    }
    public String getStudentAge() {
    
    return studentAge;
    }
    public void setStudentAge(String studentAge) {
    
    this.studentAge = studentAge;
    }
    
    
    }
    public class Test003 {
    public static void main(String[] args) throws ClassNotFoundException {
    Class<?> forName = Class.forName("com.wyh.test.Student");//反射机制
    
    // 获取所有当前的方法
    Field[] declaredFields = forName.getDeclaredFields();
    StringBuffer sf = new StringBuffer();
    sf.append(" select ");
    for (int i = 0; i < declaredFields.length; i++) {
    Field field = declaredFields[i];
    //获取属性上的注解
    Propety propety = field.getDeclaredAnnotation(Propety.class);
    sf.append(propety.name());
    if (i < declaredFields.length - 1) {
    sf.append(" , ");
    }
    }
    // 获取类上注解参数
    Table declaredAnnotation = forName.getDeclaredAnnotation(Table.class);
    sf.append(" from " + declaredAnnotation.value());
    System.out.println(sf.toString());
    }
    }
  • 相关阅读:
    后台点赞 接口
    三表联查
    后台投票 接口
    MSXML insertBefore(IXMLDOMNode *newChild, VARIANT refChild) 传参
    WTL中菜单栏及工具栏项状态改变应注意的地方
    使用WTL的消息反射封装CEdit实现监听控件文本改变事件
    修改字体
    CEdit实现文本换行
    VC中获取窗口控件相对客户区的坐标
    关闭HTC手机充电时屏幕一直亮着绿色电池的办法
  • 原文地址:https://www.cnblogs.com/nancheng/p/9222877.html
Copyright © 2011-2022 走看看