因为需要知道数据库表、字段和实体类的对应关系,那么下面就介绍下在mybatisext中如何实现的
- 注解定义
表注解@Table
package cw.frame.mybatisext.annotation; import java.lang.annotation.*; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Table { String value() default ""; }
列注解@Column
package cw.frame.mybatisext.annotation; import java.lang.annotation.*; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Column { String value() default ""; boolean primaryKey() default false; boolean generatedKey() default true; }
一对一注解@OneOne
package cw.frame.mybatisext.annotation; import cw.frame.mybatisext.base.entity.BaseExtEntity; import java.lang.annotation.*; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface OneOne { Class<? extends BaseExtEntity> type(); String foreignKey(); String propertyKey(); }
一对多注解@OneMany
package cw.frame.mybatisext.annotation; import cw.frame.mybatisext.base.entity.BaseExtEntity; import java.lang.annotation.*; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface OneMany { Class<? extends BaseExtEntity> type(); String foreignKey(); String propertyKey(); }
-
枚举类型定义
package cw.frame.mybatisext.base.entity; public interface BaseExtEnum { public Object getValue(); public String getName(); }
-
字段信息类ColumnInfo
package cw.frame.mybatisext.base.entity; import java.lang.reflect.Field; public class ColumnInfo { private TableInfo table; private String propertyName; private String columnName; private boolean isPrimaryKey = false; private boolean isGeneratedKey = true; private boolean isDbColumn = true; private Field field; public static ColumnInfo createDbColumn(TableInfo table, String propertyName, String columnName, boolean isPrimaryKey, boolean isGeneratedKey, Field field){ return new ColumnInfo(table, propertyName, columnName, isPrimaryKey, isGeneratedKey, field); } public static ColumnInfo createNormalColumn(TableInfo table, String propertyName, Field field){ return new ColumnInfo(table, propertyName, field); } private ColumnInfo(TableInfo table, String propertyName, String columnName, boolean isPrimaryKey, boolean isGeneratedKey, Field field){ this.table = table; this.propertyName = propertyName; this.columnName = columnName; this.isPrimaryKey = isPrimaryKey; this.isGeneratedKey = isGeneratedKey; this.isDbColumn = true; this.field = field; } private ColumnInfo(TableInfo table, String propertyName, Field field){ this.table = table; this.propertyName = propertyName; this.isDbColumn = false; this.field = field; } public TableInfo getTable() { return table; } public void setTable(TableInfo table) { this.table = table; } public String getColumnName() { return columnName; } public String getPropertyName() { return propertyName; } public boolean getIsPrimaryKey(){ return this.isPrimaryKey; } public boolean isGeneratedKey(){ return this.isGeneratedKey; } public boolean isDbColumn(){ return this.isDbColumn; } public Field getField() { return field; } public void setField(Field field) { this.field = field; } }
记录了实体类与数据库字段的信息:字段属性名、数据库列名、是否主键、是否自增、是否是数据库字段、对应Field类(为后续读取、赋值使用)
-
表信息类Tabl