JAVA注解拼接SQL,并对字段进行排序

本文介绍了一个使用Java注解来自动生成SQL创建语句的例子。通过定义自定义注解及解析这些注解,可以方便地从Java类生成对应的数据库表结构。文章展示了如何定义注解、实现注解的解析逻辑,并提供了完整的示例代码。

package com.example.annotation;

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

/**
 * 项目名称:
 * 类描述:
 * 创建人:
 * 创建时间:2015/12/15 19:20
 * 修改人:
 * 修改时间:2015/12/15 19:20
 * 修改备注:
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface DataBaseTable {
    public String tableName();
}

 

package com.example.annotation;

/**
 * 项目名称:
 * 类描述:
 * 创建人:
 * 创建时间:2015/12/15 19:23
 * 修改人:
 * 修改时间:2015/12/15 19:23
 * 修改备注:
 */
@DataBaseTable(tableName = "CustomTable")
public class CustomModel {
    @ColumnsName(fieldName = "userId",number = 1)
    public String mImUserId;

    @ColumnsName(fieldName = "UserCustomList",number = 3)
    public byte[] mUserCustomList;

    @ColumnsName(fieldName = "datatype",number = 2)
    public int mType;
}
 

package com.example.annotation;

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

/**
 * 项目名称:
 * 类描述:
 * 创建人:
 * 创建时间:2015/12/15 19:21
 * 修改人:
 * 修改时间:2015/12/15 19:21
 * 修改备注:
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ColumnsName {
    String fieldName() default "";
    int number() default 0;
}

 

package com.example.annotation;

import java.util.Comparator;

/**
 * 项目名称:MyApplication
 * 类描述:
 * 创建人:
 * 创建时间:2016/3/7 9:59
 * 修改人:
 * 修改时间:2016/3/7 9:59
 * 修改备注:
 */
public class ColumnsComparator implements Comparator<ColumnsName>{

    @Override
    public int compare(ColumnsName c1, ColumnsName c2) {
        if(c1.number() > c2.number()){
            return 1;
        }
        if(c1.number() <= c2.number()){
            return -1;
        }
        return 0;
    }
    
}

package com.example.annotation;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 项目名称:
 * 类描述:
 * 创建人:
 * 创建时间:2015/12/15 19:26
 * 修改人:
 * 修改时间:2015/12/15 19:26
 * 修改备注:
 */
public class AnnotationTest {

    /**
     * 运行注解,拼出sql
     * 
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        Field[] fields = CustomModel.class.getFields();
        DataBaseTable tableModel = (DataBaseTable) CustomModel.class
                .getAnnotation(DataBaseTable.class);
        String tableName = tableModel.tableName();
        List<ColumnsName> list = new ArrayList<ColumnsName>(fields.length);
        String sql = "CREATE TABLE IF NOT EXISTS " + tableName + "(";
        for (int i = 0; i < fields.length; i++) {
            ColumnsName tabFeild = fields[i].getAnnotation(ColumnsName.class);
            list.add(tabFeild);
            if (tabFeild != null) {
                if (i == 0) {
                    sql = sql + tabFeild.fieldName() + " "
                            + getColumnType(fields[i].getType());
                } else {
                    sql = sql + " ," + tabFeild.fieldName() + " "
                            + getColumnType(fields[i].getType());
                }
            }
        }
        sql = sql + ");";
        System.out.println(sql);
        //对反射进行排序
        Collections.sort(list, new ColumnsComparator());
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).number() + "\t" + list.get(i).fieldName() );
        }
    }

    /**
     * 得到type
     * 
     * @param type
     * @return
     */
    public static String getColumnType(Type type) {
        String colums = "TEXT";
        if (type == Long.class || (type == Long.TYPE)) {

        } else if (Integer.class == type || (type == Integer.TYPE)) {
            colums = "INTEGER";
        } else if (type == String.class) {

        } else if (type == byte[].class) {
            colums = "BLOB";
        }

        return colums;
    }


    
}
 

 

欢迎大家提出修改意见

转载于:https://my.oschina.net/u/1013713/blog/682865

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值