> 文章列表 > Mybatis Plus整合Oracle字段为date类型,实现新增与修改操作 --柚子真好吃

Mybatis Plus整合Oracle字段为date类型,实现新增与修改操作 --柚子真好吃

Mybatis Plus整合Oracle字段为date类型,实现新增与修改操作 --柚子真好吃

文章思路参考:https://blog.csdn.net/Tanganling/article/details/125259479

一、功能效果

  1. 封装好service中直接调用save或updateById方法即可新增或修改date类型数据;

  2. 数据库结构如下:
    Mybatis Plus整合Oracle字段为date类型,实现新增与修改操作 --柚子真好吃

  3. service中直接调用方法即可,无需手写to_date语句

     @Overridepublic Integer insertOne(User user) {this.save(user);return 1;}
    

二、快速使用(2步)

首先保证项目已整合Mybatis Plus 本人用的版本为3.2.0
  1. 粘贴以下配置类
    重写Insert

    package com.ryan.project.config;import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.core.enums.SqlMethod;
    import com.baomidou.mybatisplus.core.injector.AbstractMethod;
    import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
    import com.baomidou.mybatisplus.core.metadata.TableInfo;
    import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
    import com.baomidou.mybatisplus.core.toolkit.StringUtils;
    import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
    import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
    import org.apache.ibatis.executor.keygen.KeyGenerator;
    import org.apache.ibatis.executor.keygen.NoKeyGenerator;
    import org.apache.ibatis.mapping.MappedStatement;
    import org.apache.ibatis.mapping.SqlSource;import java.lang.reflect.Field;
    import java.util.List;
    import java.util.Objects;public class MyInsert extends AbstractMethod {@Overridepublic MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {KeyGenerator keyGenerator = new NoKeyGenerator();SqlMethod sqlMethod = SqlMethod.INSERT_ONE;String columnScript = SqlScriptUtils.convertTrim(tableInfo.getAllInsertSqlColumnMaybeIf(), "(", ")", (String) null, ",");String valuesScript = SqlScriptUtils.convertTrim(tableInfo.getAllInsertSqlPropertyMaybeIf((String) null), "(", ")", (String) null, ",");String keyProperty = null;String keyColumn = null;List<TableFieldInfo> tableFieldInfoList = tableInfo.getFieldList();for (TableFieldInfo tableFieldInfo : tableFieldInfoList) {try {Field field = modelClass.getDeclaredField(tableFieldInfo.getProperty());OracleDate annotation = field.getAnnotation(OracleDate.class);if (Objects.nonNull(annotation)){valuesScript = valuesScript.replace("#{" + tableFieldInfo.getProperty()+ "}", "to_date('${"+tableFieldInfo.getProperty()+"}','"+annotation.value()+"')");}} catch (NoSuchFieldException e) {throw new RuntimeException("【获取字段失败】" + modelClass.getName() + "获取字段" + tableFieldInfo.getProperty() + "失败");}}if (StringUtils.isNotEmpty(tableInfo.getKeyProperty())) {if (tableInfo.getIdType() == IdType.AUTO) {keyGenerator = new Jdbc3KeyGenerator();keyProperty = tableInfo.getKeyProperty();keyColumn = tableInfo.getKeyColumn();} else if (null != tableInfo.getKeySequence()) {keyGenerator = TableInfoHelper.genKeyGenerator(tableInfo, this.builderAssistant, sqlMethod.getMethod(), this.languageDriver);keyProperty = tableInfo.getKeyProperty();keyColumn = tableInfo.getKeyColumn();}}String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), columnScript, valuesScript);SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, modelClass);return this.addInsertMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource, (KeyGenerator) keyGenerator, keyProperty, keyColumn);}
    }

    ②重写 UpdateById

    package com.ryan.project.config;import com.baomidou.mybatisplus.core.enums.SqlMethod;
    import com.baomidou.mybatisplus.core.injector.AbstractMethod;
    import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
    import com.baomidou.mybatisplus.core.metadata.TableInfo;
    import org.apache.ibatis.mapping.MappedStatement;
    import org.apache.ibatis.mapping.SqlSource;import java.lang.reflect.Field;
    import java.util.List;
    import java.util.Objects;public class MyUpdateById extends AbstractMethod {@Overridepublic MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {boolean logicDelete = tableInfo.isLogicDelete();SqlMethod sqlMethod = SqlMethod.UPDATE_BY_ID;String additional = this.optlockVersion() + tableInfo.getLogicDeleteSql(true, false);String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlSet(logicDelete, false, tableInfo, false, "et", "et."), tableInfo.getKeyColumn(), "et." + tableInfo.getKeyProperty(), additional);List<TableFieldInfo> tableFieldInfoList = tableInfo.getFieldList();for (TableFieldInfo tableFieldInfo : tableFieldInfoList) {try {Field field = modelClass.getDeclaredField(tableFieldInfo.getProperty());OracleDate annotation = field.getAnnotation(OracleDate.class);if (Objects.nonNull(annotation)){sql = sql.replace("#{et." + tableFieldInfo.getProperty()+ "}", "to_date('${et."+tableFieldInfo.getProperty()+"}','"+annotation.value()+"')");}} catch (NoSuchFieldException e) {throw new RuntimeException("【获取字段失败】" + modelClass.getName() + "获取字段" + tableFieldInfo.getProperty() + "失败");}}SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, modelClass);return this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);}
    }

    ③编写自定义注解 OracleDate(可自行修改)

    package com.ryan.project.config;import java.lang.annotation.*;@Documented
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface OracleDate {String value() default "YYYY-MM-DD HH24:MI:SS";
    }

    ④替换MP原有接口集合

    package com.ryan.project.config;import com.baomidou.mybatisplus.core.injector.AbstractMethod;
    import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
    import com.baomidou.mybatisplus.core.injector.methods.Insert;
    import com.baomidou.mybatisplus.core.injector.methods.UpdateById;
    import org.springframework.stereotype.Component;import java.util.List;@Component
    public class MySqlInjector extends DefaultSqlInjector {@Overridepublic List<AbstractMethod> getMethodList(Class<?> mapperClass) {List<AbstractMethod> methodList = super.getMethodList(mapperClass);for (int i = 0; i < methodList.size(); i++) {if (methodList.get(i) instanceof Insert) {methodList.set(i, new MyInsert());}if (methodList.get(i) instanceof UpdateById) {methodList.set(i, new MyUpdateById());}}return methodList;}
    }
  2. 字段上增加注解即可,实例代码如下:

    package com.ryan.project.entity.db;import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.annotation.TableId;
    import com.baomidou.mybatisplus.annotation.TableName;
    import com.ryan.project.config.OracleDate;
    import lombok.AllArgsConstructor;
    import lombok.Builder;
    import lombok.Data;
    import lombok.NoArgsConstructor;@TableName("STUDENT")
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    public class User {@TableId(type = IdType.UUID)private String id;private String username;private String password;private Integer age;@OracleDateprivate String startDate;@OracleDateprivate String endDate;
    }