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. 测试结果
只要你反射 得到了表名字段名,怎么创建表就不用说了。