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<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(SxTable.class);
        for (Map.Entry<String, Object> 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<ApplicationReadyEvent>接口,处理方式类似,最终也是获取到applicationContext上下文进行操作。

4.效果

完毕!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值