JAVA 使用反射解析注解

ORM

ORM (Object Relation Mapping) 对象关系映射

将数据库中的表和java中的对象建立映射关系

比如数据库中存在表

idstuNameage
1001张小三23
1002王一一34

java语言

public class Student {
    int id;
    String stuName;
    int age;
}

与数据库定义语言映射

CREATE TABLE 'tb_student' (
	id int(11) NOT NOLL AUTO_INCREMENT,
    stuname varchar(20) NOT NULL,
    age int(11) NOT NULL,
    PRIMARY KEY (id)
) DEFAULT CHARSER=utf8
  1. 类与表格结构对应
  2. 属性和字段对应
  3. 对象和记录对应

使用ORM映射来学习反射取得注解

  1. 编写一个User类

    package pers.jssd.entity;
    
    import pers.jssd.annotation.TableField;
    import pers.jssd.annotation.UserTable;
    
    /**
     * 实体类
     *
     * @author jssd
     * Create 2019-07-11 19:47
     */
    @UserTable("t_user")
    public class User {
       @TableField(name = "t_name", type = "varchar", length = 12)
       private String name;
    
       @TableField(name = "t_age", type = "int", length = 3)
       private int age;
    
       @TableField(name = "t_sax", type = "char", length = 2)
       private int sax;
    
       public User() {
       }
    
       public User(String name, int age) {
          this.sax = 1;
          this.name = name;
          this.age = age;
       }
    
       public User(String name) {
          this.name = name;
          this.sax = 1;
       }
    
       public String getName() {
          return name;
       }
    
       public void setName(String name) {
          this.name = name;
       }
    
       public int getAge() {
          return age;
       }
    
       public void setAge(int age) {
          this.age = age;
       }
    
       public int getSax() {
          return sax;
       }
    
       public void setSax(int sax) {
          this.sax = sax;
       }
    
    }
    
  2. 编写类的注解

    /**
     * @author jssd
     * Create 2019-07-13 10:48
     */
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface UserTable {
       String value();
    }
    
  3. 编写字段的注解

    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface TableField {
       String name();
       String type();
       int length();
    }
    
  4. 通过反射解析注解, 并取得java中属性名和数据库表中字段的映射关系

    public static void main(String[] args) throws NoSuchFieldException {
       Class userClz = User.class;
       // 获取类的注解
       UserTable declaredAnnotation = (UserTable) userClz.getDeclaredAnnotation(UserTable.class);
       // 从类的注解中得到表名
       System.out.println("declaredAnnotation.value() = " + declaredAnnotation.value());
    
       // 获取某个属性的注解
       TableField tableField = (TableField) userClz.getDeclaredField("name").getDeclaredAnnotation(TableField.class);
       System.out.println("tableField = " + tableField);
       System.out.println("tableField.name() = " + tableField.name());
       System.out.println("tableField.type() = " + tableField.type());
       System.out.println("tableField.length() = " + tableField.length());
    }
    
  5. 可进行sql语句拼接等操作

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值