//映射的过程:

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);

java实现的类和表持久化的更多相关文章

  1. Java I/O---Properties类(持久化键值对)

    1.Properties类简介 Properties类(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这 ...

  2. 高速创建和mysql表相应的java domain实体类

    今天创建了一个表有十几个字段,创建完之后必定要写一个与之相应的java domain实体类. 这不是反复的工作吗?为什么不先把这个表的全部的字段查出来,然后放到linux环境下,用sed工具在每一行的 ...

  3. 《深入理解Java虚拟机》类文件结构

    上节学习回顾 在上一节当中,主要以自己的工作环境简单地介绍了一下自身的一些调优或者说是故障处理经验.所谓百变不离其宗,这个宗就是我们解决问题的思路了. 本节学习重点 在前面几章,我们宏观地了解了虚拟机 ...

  4. Java常用工具类题库

    一.    填空题 在Java中每个Java基本类型在java.lang包中都在一个相应的包装类,把基本类型数据转换为对象,其中包装类Integer是___Number__的直接子类. 包装类Inte ...

  5. Java中Properties类的操作

    知识学而不用,就等于没用,到真正用到的时候还得重新再学.最近在看几款开源模拟器的源码,里面涉及到了很多关于Properties类的引用,由于Java已经好久没用了,而这些模拟器大多用Java来写,外加 ...

  6. Java中Properties类的操作配置文件

    知识学而不用,就等于没用,到真正用到的时 候还得重新再学.最近在看几款开源模拟器的源码,里面涉及到了很多关于Properties类的引用,由于Java已经好久没用了,而这些模拟器大多用 Java来写, ...

  7. Java:日历类、日期类、数学类、运行时类、随机类、系统类

    一:Calendar类 java.util 抽象类Calendar   1.static Calendar getInstance()使用默认时区和语言环境获得一个日历. 2. int get(int ...

  8. Java中Properties类的学习总结

    学习目标: 1.认识properties文件,理解其含义,会正确创建properties文件. 2.会使用java.util.Properties类来操作properties文件. 一.认识prope ...

  9. Java中Properties类

    1 简介: JDK提供的java.util.Properties类继承自Hashtable类并且实现了Map接口,用map来存储key-value数据,所以存入的数据是无序的.其中键和值都是字符串类型 ...

随机推荐

  1. nginx安装及负载均衡配置

    Nginx (“engine x”) 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev 为俄罗斯访问量第二 ...

  2. amgular $q用法

    amgular $q用法   在用JQuery的时候就知道 promise 是 Js异步编程模式的一种模式,但是不是很明白他跟JQuery的deferred对象有什么区别.随着公司项目的进行,要跟后台 ...

  3. [课程设计]任务进度条&开发日志目录

    任务进度条&开发日志目录 周期 时间 任务 Sprint One   11.14     ●  Scrum团队分工及明确任务1.0    Sprint One   11.15   ●  Scr ...

  4. 模块(configparser+shutil+logging)

    一.configparser模块 1.模块介绍 configparser用于处理特定格式的文件,其本质上是利用open来操作文件. *注:(这里解释一下特定格式的文件) a.有section和opti ...

  5. this的问题

    javascript this可以绑定到:全局对象,自己定义的对象,用构造函数生成的对象,通过call或者apply更改绑定的对象    1.全局对象  1 2 3 4 5 function glob ...

  6. [原]调试ComFriendlyWaitMtaThreadProc崩溃

    项目里安装了UIA相关的钩子来监听UIA相关事件,退出的时候偶尔会崩溃在ComFriendlyWaitMtaThreadProc中,如下  从上图可以看出 是访问到无效的地址了,用!address 0 ...

  7. Ubuntu上Docker安装Trouble Shooting

    (我的环境是Mint7.1,相当于Ubuntu14.04) 1,首先,根据docker.com上的安装指导来安装docker,这里就不重复了,参考: https://docs.docker.com/i ...

  8. ImageList图标左边有黑色竖线

    ImageList图标左边有黑色竖线, 原因 ImageList颜色深度太小引起的,解决方案,把颜色深度调成Depth32Bit 默认: 修改为: 结果: 备注:根据文件获得文件的系统图标: Icon ...

  9. Attribute

    Attribute介绍 咱们来说Attribute,他是一个类,所以自定义的Attribute都是继承自System.Attribute,一般命名的时候都是以Attribute结尾.在使用的时候我们可 ...

  10. 简述Session 、Cookie、cache 区别

    区别: 1.session是把数据保存在服务器上,每一个用户都有自己的session. 2.cookie是保存在客户端(也就是本机电脑). 3.cache是保存在服务器上,每个用户都可以访问的对象. ...