Mybatis(拦截器实现)通用mapper及全ORM实现(三)

因为需要知道数据库表、字段和实体类的对应关系,那么下面就介绍下在mybatisext中如何实现的

  1. 注解定义

    表注解@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();
    }
    

     

  2. 枚举类型定义

    package cw.frame.mybatisext.base.entity;
    
    public interface BaseExtEnum {
    
        public Object getValue();
    
        public String getName();
    
    }

     

  3. 字段信息类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类(为后续读取、赋值使用)

  4. 表信息类Tabl

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值