java实现的类和表持久化

//映射的过程:

package com.ly.orm;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

public class ClassMapper {
    private String[] cols;

    public String[] getCols() {
        return cols;
    }

    private String schema;

    private ClassMapper() {
    }

    public String getSchema() {
        return schema;
    }

    public static ClassMapper get(Class<?> source, HashMap<String, Class<?>[]> map) {
        Field[] fs = source.getDeclaredFields();
        ArrayList<String> temp = new ArrayList<String>();
        StringBuilder sb = new StringBuilder();
        for (Field f : fs) {// fuck these endless for and ifelse
            if (f.getAnnotation(I.class) != null) {
                continue;
            }
            Class<?> cls = f.getType();
            for (String key : map.keySet()) {
                boolean found = false;
                Class<?>[] classes = map.get(key);
                for (Class<?> c : classes) {
                    if (!c.equals(cls)) {
                        continue;
                    }
                    found = true;
                    String col, str;
                    C a = f.getAnnotation(C.class);
                    if (a == null || a.getName().length() == 0) {
                        col = f.getName();
                    } else {
                        col = a.getName();
                    }
                    str = col + ' ' + key;
                    if (a != null && a.getDesc().length() > 0) {
                        str += ' ' + a.getDesc();
                    }
                    temp.add(col);
                    sb.append(str + ',');
                    break;
                }
                if (found) {
                    break;
                }
            }
        }
        if (sb.length() == 0) {
            return null;
        }
        sb.setLength(sb.length() - 1);
        T a = source.getAnnotation(T.class);
        String t = a == null ? source.getSimpleName() : a.getName();
        ClassMapper result = new ClassMapper();
        result.schema = String.format("create table %s(%s);", t, sb);
        result.cols = new String[temp.size()];
        temp.toArray(result.cols);
        return result;
    }
}

//还有三个Annotation:

package com.ly.orm;

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.TYPE)
public @interface T {
    String getName();
}
package com.ly.orm;

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 C {
    final static String PRIMARY_KEY = "primary key";
    final static String AUTOINCREMENT = "autoincrement";
    final static String NOT_NULL = "not null";
    final static String DEFAULT_NOW = "default datetime('now','localtime')";

    String getName() default "";

    String getDesc() default "";
}
package com.ly.orm;

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 I {
}

//最后测试一下:

import com.ly.orm.C;
import com.ly.orm.ClassMapper;
import com.ly.orm.I;
import com.ly.orm.SQLiteMapper;
import com.ly.orm.T;

@T(getName = "ggggod")
public class God {
    @C(getDesc = C.PRIMARY_KEY + ' ' + C.AUTOINCREMENT)
    int a;
    String aa;
    @C(getName = "qwe123")
    public int aaa;
    @I
    public String aaaa;

    public static void main(String[] args) {
        ClassMapper m = com.ly.orm.ClassMapper.get(God.class, SQLiteMapper.get());
        System.out.println(m.getSchema());
    }
}

//输出结果:
create table ggggod(a integer primary key autoincrement,aa text,qwe123 integer);

posted @ 2016-12-09 21:12  大胡子毛绒老头  阅读(924)  评论(0编辑  收藏  举报