设为首页 加入收藏

TOP

Java注解(2):实现自己的ORM(一)
2023-07-25 21:23:11 】 浏览:36
Tags:Java 注解 ORM

搞过Java的码农都知道,J2EE开发中一个(确切地说,应该是一类)很重要的框架,那就是ORM(Object Relational Mapping,对象关系映射)。它把Java中的类和数据库中的表关联起来,可以像操作对象那样操作数据表,十分方便。给码农们节约了大量的时间去摸鱼。其实它的本质一点都不复杂,而最核心的就是怎么实现对象和表之间的转换。之前对反射和注解有了一点了解,所以就试着来实现咱们自己的缝合怪。

首先,需要建立一个「表格」:

/**
 * 类注解,将类注解成数据库表
 *
 * @author xiangwang
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface DBTable {
   String name() default "";
}

 

 

然后,定义需要的数据库数据类型:

/**
 * 字段类型枚举
 *
 * @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;
}

 

 

再来完善字段相关信息:

/**
 * 字段信息
 *
 * @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;
    // 还可以增加默认值
}

 

 

把他们拼起来,成为完整的字段描述:

/**
 * 拼装注解,形成完整的字段嵌套注解
 *
 * @author xiangwang
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TableColumn {
   ColumnType columntype() default @ColumnType;
   ExtraInfo extrainfo() default @ExtraInfo;
   Constraints constraints() default @Constraints;
}

 

 

最后,创建实体类,应用刚才写好的这些注解:

/**
 * 用户实体类
 *
 * @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是怎么工作的吧:

/**
 * 解析类型注解
 */
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";
         b
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇关于在 Mybatis 中使用 record 关.. 下一篇emqx启用JWT令牌认证(包含hmac-b..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目