• 纯手写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());
    }
    }
  • 相关阅读:
    Linux修改主机名称
    Druid监控SQL语句
    CentOS7.5搭建Hadoop分布式集群
    CentOS7.5 解决ifconfig报错
    windows 用VMware创建linux虚拟机,安装操作系统CentOS7.2
    MySQL报错this is incompatible with sql_mode=only_full_group_by
    CentOS配置Redis环境变量
    CentOS7.4搭建GitLab
    修改服务器路由策略
    Centos7 安装python3
  • 原文地址:https://www.cnblogs.com/nancheng/p/9222877.html
Copyright © 2020-2023  润新知