搞过Java的码农都知道,在J2EE开发中一个(确切地说,应该是一类)很重要的框架,那就是ORM(Object Relational Mapping,对象关系映射)。它把Java中的类和数据库中的表关联起来,可以像操作对象那样操作数据表,十分方便。给码农们节约了大量的时间去摸鱼。其实它的本质一点都不复杂,而最核心的就是怎么实现对象和表之间的转换。之前对反射和注解有了一点了解,所以就试着来实现咱们自己的缝合怪。
首先,需要建立一个「表格」:
1 2 3 4 5 6 7 8 9 10 | /** * 类注解,将类注解成数据库表 * * @author xiangwang */ @Target (ElementType.TYPE) @Retention (RetentionPolicy.RUNTIME) public @interface DBTable { String name() default "" ; } |
然后,定义需要的数据库数据类型:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /** * 字段类型枚举 * * @author xiangwang */ public enum Type { CHAR, STRING, BOOLEAN, INTEGER, LONG, FLOAT, DOUBLE, DATETIME } /** * 数据库字段类型 * * @author xiangwang */ @Target (ElementType.FIELD) @Retention (RetentionPolicy.RUNTIME) public @interface ColumnType { Type value() default Type.INTEGER; } |
再来完善字段相关信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /** * 字段信息 * * @author xiangwang */ @Target (ElementType.FIELD) @Retention (RetentionPolicy.RUNTIME) public @interface ExtraInfo { String name() default "" ; int length() default 0 ; } /** * 明确字段约束 * * @author xiangwang */ @Target (ElementType.FIELD) @Retention (RetentionPolicy.RUNTIME) public @interface Constraints { boolean primaryKey() default false ; boolean allowNull() default true ; boolean unique() default false ; // 还可以增加默认值 } |
把他们拼起来,成为完整的字段描述:
1 2 3 4 5 6 7 8 9 10 11 12 | /** * 拼装注解,形成完整的字段嵌套注解 * * @author xiangwang */ @Target (ElementType.FIELD) @Retention (RetentionPolicy.RUNTIME) public @interface TableColumn { ColumnType columntype() default @ColumnType ; ExtraInfo extrainfo() default @ExtraInfo ; Constraints constraints() default @Constraints ; } |
最后,创建实体类,应用刚才写好的这些注解:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | /** * 用户实体类 * * @author xiangwang */ @DBTable (name = "User" ) public class User { @TableColumn ( columntype = @ColumnType (Type.INTEGER), extrainfo = @ExtraInfo (name = "id" , length = 4 ), constraints = @Constraints (primaryKey = true )) private String id; @TableColumn ( columntype = @ColumnType (Type.STRING), extrainfo = @ExtraInfo (name = "name" , length = 32 ), constraints = @Constraints (primaryKey = false , allowNull = false , unique = true )) private String name; @TableColumn ( columntype = @ColumnType (Type.INTEGER), extrainfo = @ExtraInfo (name = "age" , length = 4 ), constraints = @Constraints (primaryKey = false )) private Integer age; public String getId() { return id; } public void setId(String id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this .age = age; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + "]" ; } } |
来看看ORM是怎么工作的吧:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | /** * 解析类型注解 */ private static String getColumnType(ColumnType columntype) { String type = "" ; switch (columntype.value()) { case CHAR: type += "CHAR" ; break ; case STRING: type += "VARCHAR" ; break ; case BOOLEAN: type += "BIT" ; break ; case INTEGER: type += "INT" ; break ; case LONG: type += "BIGINT" ; break ; case FLOAT: type += "FLOAT" ; break ; case DOUBLE: type += "DOUBLE" ; break ; case DATETIME: type += "DATETIME" ; break ; default : type += "VARCHAR" ; break ; } return type; } /** * 解析信息注解 */ private static String getExtraInfo(ExtraInfo extrainfo) { String info = "" ; if ( null != extrainfo.name()) { info = extrainfo.name(); } else { return null ; } if ( 0 |
做了那么多的铺垫,终于到了临门一脚了,实现一个缝合怪了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /** * 临门一脚:实现一个缝合怪 */ private static void createTable(List<string> list) { for (String className : list) { Class> clazz; try { clazz = Class.forName(className); DBTable dbTable = clazz.getAnnotation(DBTable. class ); if (dbTable == null ) { // 无DBTable注解 continue ; } // 转大写 String tableName = clazz.getSimpleName().toUpperCase(); StringBuilder sql = new StringBuilder( "CREATE TABLE " + tableName + "(" ); for (Field field : clazz.getDeclaredFields()) { // 反射得到注解 Annotation[] anns = field.getDeclaredAnnotations(); if (anns.length </string> |
验证效果的时候到了:
1 2 3 4 5 6 7 | public static void main(String[] args) { Class> clazz = User. class ; List<string> list = new ArrayList(); list.add(clazz.getName()); createTable(list); }</string> |
当然,实际的运营于生产环境中的ORM框架可要比这个小玩意复杂多了。但千变万变,原理不变,ORM的核心——反射+ 注解——就是这么玩的。
到此这篇关于Java注解实现自己的ORM的文章就介绍到这了,更多相关Java注解ORM内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!