//映射的过程:

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. C# 获取当前路径方法

    //获取包含清单的已加载文件的路径或 UNC 位置. public static string sApplicationPath = Assembly.GetExecutingAssembly ( ) ...

  2. 关于dvajs里effects的call和put

    call会把return 传回来 put把参数穿回来了 在effects里好像只有yield能触发put ,call暂时没定

  3. java socket 多线程通讯 使用mina作为服务端

    客户端代码不变,参照 http://www.cnblogs.com/Westfalen/p/6251473.html 服务端代码如下: import java.io.IOException; impo ...

  4. sys,os,模块-正则表达式

    # *__conding:utf-8__* """"我是注释""" sys,os模块 import sysimport os pr ...

  5. MySQl查询区分大小写的解决办法

    通过查询资料发现需要设置collate(校对) . collate规则: *_bin: 表示的是binary case sensitive collation,也就是说是区分大小写的 *_cs: ca ...

  6. ThinkPhp的搭建

    一般而言,环境的搭建都是很复杂的,但是说句实话,php的环境是我见过最简单的,首先下载一个XAMPP集成软件包(这种软件包还有好多,但是我还是喜欢这个啊),然后直接安装,一路NEXT下午就ok了. 这 ...

  7. Web App适配不同屏幕的几点建议

    安卓设备在屏幕尺寸和像素密度上差别很大,因此在使用WebView加载网页时就需要考虑到这种差别,对我们的网页做出精心的设计以在不同的屏幕上都能得到合适的展现.通常情况下,我们需要考虑到两个因素:1.视 ...

  8. error: could not read CFBundleIdentifier from Info.plist (null)解决方法之一

    出现这种错误的原因可能很多,以下是我遇到的一种情况: 项目移植到新的环境 编译报错:  error: could not read CFBundleIdentifier from Info.plist ...

  9. apache代理服务器为nodejs服务设置域名

    本机以apache为主,其中 在httpd.conf中先设置 <VirtualHost *:80> ServerName nodejs.cc ServerAlias www.nodejs. ...

  10. 第三天的学习知识HTML5常用的重要单词

    a:   a:猫     address:地址     alt:替用(一般是图片显示不出的提示) b:   b:粗体     br:换行     background:背景     border:边框 ...