定义表名注解
定义字段注解
定义普通javaBean,用上面的注解界定建表sql
解析注解生成建表sql
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
- /*注解的分类
- * 1.标记注解(marker annotation)
- * 注解体内没有定义任何元素,只起一个标记提示作用
- * 常见的就是java.lang包下的Deprecated,Override,SuppressWarnings
- Deprecated 编译时会提示方法过时
- Override 编译时验证重写父类方法签名是否正确
- SuppressWarnings 取消警告
- 2.元注解
- 只用来修饰注解定义的注解
- 下面用到的Retention,Target
- Retention用来指定定义的注解要保留到什么时候
- 有三个枚举值:
- RetentionPolicy.SOURCE 编译是会调用,不会保留到class文件中
- RetentionPolicy.CLASS 会跟随保留到class文件中
- RetentionPolicy.RUNTIME 保留到class文件中,并且class被加载时还可以通过反射操作注解
- Target用来规定注解可以修饰的程序元素的种类
- 其有一个ElementType[]的枚举数组参数
- ElementType.PACKAGE 包
- ElementType.TYPE 类,接口,注解,枚举
- ElementType.METHOD 方法声明
- ElementType.FIELD 字段
- ......
- * 注解一旦定义好之后,就可以像使用public,static这样的的modifiers一样,用注解修饰类,方法或属性
- */
- @Retention(RetentionPolicy.RUNTIME)//可以保留到类被加载运行时
- @Target(ElementType.TYPE) //指定该注解用来修饰类...
- public @interface Table { //定义注解的关键字@interface
- /*
- * 元素定义的返回类型限定为:基本类型,String,Class,emum,annotation
- 或者是前述类型的数组
- */
- String name();
- }
定义字段注解
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
- /*
- * 定义字段的注解
- */
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.FIELD) //该注解只能用在成员变量上
- public @interface Column {
- int length() default 0; //用来存放字段的长度
- String name() default "" ;//用来存放字段的名字
- //至于数据库字段的类型,后面根据反射获取成员变量的类型获取
- }
定义普通javaBean,用上面的注解界定建表sql
- import java.util.Date;
- /*
- * 一个简单使用例子,根据注解生成创建表语句
- * 使用注解时,可以用key-value键值对的形式为注解的元素赋值
- */
- @Table(name="table_person") //表名
- public class PersonBean {
- @Column(length=8,name="person_id")
- private Integer id;
- @Column(length=32,name="pname")
- private String name;
- @Column(name="birth") //Date类型不需要指定length
- private Date birth;
- public Integer getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Date getBirth() {
- return birth;
- }
- public void setBirth(Date birth) {
- this.birth = birth;
- }
- }
解析注解生成建表sql
- import java.lang.annotation.Annotation;
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
- import java.util.Date;
- public class TestMain {
- //用来解析所有成员变量的方法
- public static String[] getColumns(Field[] fArr){
- String[] columns = new String[fArr.length];
- String columnName="";
- int columnLength=0;
- String columnType = "";
- for(int i=0;i<fArr.length;i++){
- Field f = fArr[i];
- String name = f.getName(); //成员变量名
- Class type = f.getType(); //成员变量类型
- //判断该成员变量上是不是存在Column类型的注解
- if(f.isAnnotationPresent(Column.class)){
- //存在
- Column c = f.getAnnotation(Column.class);//获取实例
- //获取元素值
- columnName = c.name();
- columnLength = c.length();
- }
- //如果未指定列名,默认列名使用成员变量名
- if("".equals(columnName)){
- columnName = name;
- }
- //如果未指定字段长度,默认32
- if(0 == columnLength){
- columnLength = 32;
- }
- //如果成员变量是String类型的,数据库字段是varchar类型
- if(String.class == type){
- columnType = "varchar";
- }else if(Integer.class == type){
- columnType = "number";//Integer类型的用number
- }
- //把每一个成员变量相关信息存放到返回数组中
- if(Date.class == type){//Date类型的用date
- columns[i] = columnName+" date";
- }else{
- columns[i] = columnName+" "+columnType+"("+columnLength+")";
- }
- }
- return columns;
- }
- public static void main(String[] args) throws Exception {
- StringBuffer sql = new StringBuffer("create table ");
- Class c1 = Class.forName("com.cao.annotation.PersonBean");//加载使用注解的bean,(bean的路径包括bean的包)
- if(c1.isAnnotationPresent(Table.class)){
- //该class存在Table类型的注解,获取指定的表名
- Table table = (Table) c1.getAnnotation(Table.class);
- String tableName = table.name();
- sql.append(tableName+" (");
- }
- //获取bean所声明的成员变量(include private)
- Field[] fArr = c1.getDeclaredFields();
- //解析这些字段的注解设定值
- String[] columns = getColumns(fArr);
- //拼接解析后的成员变量信息成创建表语句
- for(int i=0;i<columns.length;i++){
- if(i==(columns.length-1)){
- sql.append("\n"+columns[i]+")");
- }else{
- sql.append("\n"+columns[i]+",");
- }
- }
- System.out.println(sql.toString());
- /*结果:
- * create table table_person (
- person_id number(8),
- pname varchar(32),
- birth date)
- */
- }
- }