应用注解来反射实体类生成表

SpringDataJpa或者是Hibernate都可以用类来生成表,于是自己模拟一个来深入了解其原理
1. 表名注解



package com.ainy.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * <p>
 * TODO 自定义注解,注解 表的实体类
 * </p>
 *
 * @author AINY
 * @version 2019年4月12日
 */

@Target(value = { ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTableAnnotation {
	String value();
}

2. 表字段注解


package com.ainy.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * <p>
 * TODO 注解表中的字段的
 * </p>
 * 
 * @author AINY
 * @version 2019年4月12日
 */
@Target(value = { ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface MyField {
	/**
	 * 下面是一些属性 使用注解的时候可以填写,如果只有一个value 可以直接@MyField("xxx")
	 */
	// 字段名
	String name();

	// 字段类型
	String type();

	// 字段长度
	int length();
}

3. 使用注解标注的实体类


package com.ainy.table;

import com.ainy.annotation.MyField;
import com.ainy.annotation.MyTableAnnotation;

/**
 * <p>
 * TODO 使用注解来注解一个实体类以及他的字段 一会使用反射来得到他的信息
 * </p>
 * 
 * @author AINY
 * @version 2019年4月12日
 */
@MyTableAnnotation(value = "test_annotation_table")
public class UserMyAnnotationGetTable {
	@MyField(name = "t_id", type = "varchar", length = 32)
	private String t_id;
	@MyField(name = "t_name", type = "varchar", length = 32)
	private String t_name;
	@MyField(name = "t_num", type = "int", length = 11)
	private int t_num;

	/**
	 * @return the t_id
	 */
	public String getT_id() {
		return t_id;
	}

	/**
	 * @param t_id
	 *            the t_id to set
	 */
	public void setT_id(String t_id) {
		this.t_id = t_id;
	}

	/**
	 * @return the t_name
	 */
	public String getT_name() {
		return t_name;
	}

	/**
	 * @param t_name
	 *            the t_name to set
	 */
	public void setT_name(String t_name) {
		this.t_name = t_name;
	}

	/**
	 * @return the t_num
	 */
	public int getT_num() {
		return t_num;
	}

	/**
	 * @param t_num
	 *            the t_num to set
	 */
	public void setT_num(int t_num) {
		this.t_num = t_num;
	}

}

4. 测试类


package com.ainy.useAnnotation;

import java.lang.reflect.Field;

import com.ainy.annotation.MyField;
import com.ainy.annotation.MyTableAnnotation;

/**
 * <p>
 * TODO 使用注解来获取信息
 * </p>
 *
 * @author AINY
 * @version 2019年4月12日
 */

public class TestAnnotation {
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException {
		Class clazz = Class.forName("com.ainy.table.UserMyAnnotationGetTable");
		MyTableAnnotation table = (MyTableAnnotation) clazz.getAnnotation(MyTableAnnotation.class);
		System.out.println("表名是" + table.value());
		Field[] declaredFields = clazz.getDeclaredFields();
		for (Field field : declaredFields) {
			System.out.println("实体类字段名是" + field.getName());
			Field declaredField = clazz.getDeclaredField(field.getName());
			MyField annotation2 = declaredField.getAnnotation(MyField.class);
			System.out.println("表字段为:" + annotation2.name() + "-------字段类型:" + annotation2.type() + "-------字段长度是"
					+ annotation2.length());
		}
	}

}

5. 测试结果
在这里插入图片描述
只要你反射 得到了表名字段名,怎么创建表就不用说了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值