jdk反射+spring注解实现类似hibernate以类建表功能

说明:连接数据库并运行sql步骤没做,此处只为通过类构建出sql语句!!!!主键注解没写

1.定义自定义注解

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Component
public @interface SxTable {

    /**
     *
     * @return 表名
     */
    String value();
}
/**
 * 属性列名
 *
 * @author qiaofeng
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface SxField {

    /**
     * 列名
     */
    String columnName();

    /**
     * 类型
     */
    String type();

    /**
     * 长度
     */
    int length();
}

@Component必须要加上,不然applicationContext.getBeansWithAnnotation(SxTable.class)会获取不到。

2.定义使用注解的类,下面是两个普普通通的entity,自己自行分开成两个类。

@Data
@SxTable("sx_student")
public class SxStudent {

    @SxField(columnName = "id", type = "varchar", length = 32)
    private String id;

    private String name;

    private int number;

    @SxField(columnName = "success", type = "tinyint", length = 1)
    private boolean success;
}

@Data
@SxTable("sx_school")
public class SxSchool {

    @SxField(columnName = "id", type = "varchar", length = 32)
    private String id;

    private String name;

    @SxField(columnName = "position", type = "varchar", length = 32)
    private String success;
}

3.编写处理带有自定义注解的逻辑


/**
 * 获取SxTable注解的类,将其转为sql
 * @author qiaofeng
 */
@Configuration
public class MyApplicationContextAware implements ApplicationContextAware {

    
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        getAnnotationSxTableAll(applicationContext);
    }


    private void getAnnotationSxTableAll(ApplicationContext applicationContext){
        Map beansWithAnnotation = applicationContext.getBeansWithAnnotation(SxTable.class);
        for (Map.Entry entry : beansWithAnnotation.entrySet()){
            printSql(entry.getValue().getClass());
        }
    }

    private void printSql(Class aClass){
        //获取所有注解
        Annotation[] annotations = aClass.getAnnotations();
        //获取类上的指定注解
        SxTable tableAnnotation = aClass.getAnnotation(SxTable.class);

        //获取类属性上的注解
        Field[] declaredFields = aClass.getDeclaredFields();
        if (declaredFields.length == 0){
            return;
        }
        StringBuffer stringBuffer = new StringBuffer("create table " + tableAnnotation.value());
        stringBuffer.append("(");
        for (Field field : declaredFields){
            SxField annotation = field.getAnnotation(SxField.class);
            if (annotation != null){
                stringBuffer.append(annotation.columnName()).append(" ").append(annotation.type()).append("(").append(annotation.length()).append("),");
            }else {
                stringBuffer.append(field.getName()).append(" ").append(field.getType()==String.class? "varchar" : "int").append("(").append(255).append("),");
            }
        }
        String sql = stringBuffer.substring(0, stringBuffer.lastIndexOf(",")) + ");";
        System.out.println("创建表的sql为:" + sql);
    }

}

有两种,上面为其中一种,实现ApplicationContextAware接口,程序初始化时,会执行setApplicationContext方法,处理逻辑放在此方法中。

       第二种,实现ApplicationListener接口,处理方式类似,最终也是获取到applicationContext上下文进行操作。

4.效果

完毕!!!

你可能感兴趣的:(笔记,java)