已有的springcloud+mybatis项目升级为mybatis-plus的方法

 更新时间:2021年03月11日 14:13:34   作者:Call me 男神  
这篇文章主要介绍了已有的springcloud+mybatis项目升级为mybatis-plus,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

已有的springcloud+mybatis项目升级为mybatis-plus

项目模块目录

在这里插入图片描述

在这里插入图片描述

将mybatis依赖替换为mybatis-plus

在这里插入图片描述

修改配置文件

在这里插入图片描述

实体类如果与数据库不同名需要加上@TableName

@Data
@TableName("project_base")
public class ProjectBase {
 @TableId(value = "id", type = IdType.UUID)//id看具体项目要求如果是后台生成则不需要type属性,如果不是后台生成不管是自增还是唯一键还是填入都需type属性
  private String id;
	 private String prjid;

  private String ccode;

  private String cname;

  private String orgbuild;

  @TableField(fill = FieldFill.INSERT_UPDATE)、//自动填充时间需要一个继承MetaObjectHandler的类,下一个
  private Date createtime;
  @TableField(fill = FieldFill.UPDATE)
  private Date updatetime;
  @TableLogic//需要配置文件开启逻辑删除
  private Boolean del;

  @Version//版本字段数据库不是一定为version只需要在版本字段上加上注解就可以
  private Integer version;
package com.itpm.server.Handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Slf4j
public class MyMateHandler implements MetaObjectHandler {
  @Override
  public void insertFill(MetaObject metaObject) {
    this.setFieldValByName("createtime",new Date(),metaObject);
    this.setFieldValByName("updatetime",new Date(),metaObject);
  }

  @Override
  public void updateFill(MetaObject metaObject) {
    this.setFieldValByName("updatatime",new Date(),metaObject);
  }
}

继承BaseMapper

原有接口可以不变,也可以把同名的接口名改了,比如plus的insert和原有的insert同名

package com.itpm.server.mapper.project;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itpm.server.domain.project.ProjectBase;
import com.itpm.server.domain.project.ProjectBaseExample;
import com.itpm.server.dto.project.ProjectBaseDto;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

public interface ProjectBaseMapper extends BaseMapper<ProjectBase> {
  long countByExample(ProjectBaseExample example);

  int deleteByExample(ProjectBaseExample example);


  int deleteByPrimaryKey(String id);

  int insertlist(List<ProjectBase> list);

  int insert(ProjectBase record);

  int insertSelective(ProjectBase record);

  List<ProjectBaseDto> selectByExample(ProjectBaseExample example);

  ProjectBaseDto selectByPrimaryKey(String id);

  int updateByExampleSelective(@Param("record") ProjectBase record, @Param("example") ProjectBaseExample example);

  int updateByExample(@Param("record") ProjectBase record, @Param("example") ProjectBaseExample example);

  int updateByPrimaryKeySelective(ProjectBase record);

  int updateByPrimaryKey(ProjectBase record);

  ProjectBaseDto selectByPrjid(Map map);

  List<ProjectBaseDto> selectByprojectoverview(String prjid);

  List<ProjectBaseDto> selectProjectByExample(ProjectBaseExample example);

  List<ProjectBaseDto> selectProjectByparams(@Param("record") Map record);

}

Service层

service层可以继承IService。如果想都自己写不继承也可以

代码生成器

与之前的mybatis代码生成器不冲突,可以选择也可以一起用
我的要生成在公共模块server下

 <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-generator</artifactId>
        <version>3.0.6</version>
      </dependency>
 <dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
        <version>2.3.29</version>
      </dependency>

```默认的是freemaker模板可以用其他的,需要导入其他依赖并且代码设置如下,当然可以做成一个util方便,mapper.xml默认生成在mapper层下xml包下,如果需要在resouce下生成需要自定义输出位置

package com.itpm.generator.server;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ProjectKing {
  public static void main(String[] args) {
    //需要构建一个代码自动生成器对象
    AutoGenerator autoGenerator = new AutoGenerator();
    //配置策略
    //1.全局配置
    GlobalConfig globalConfig = new GlobalConfig();
    File file = new File("server");
    String path = file.getAbsolutePath();
//    String property = System.getProperty("user.dir");
    globalConfig.setOutputDir(path + "/src/main/java");
    globalConfig.setAuthor("蒋磊");
    globalConfig.setOpen(false);
    globalConfig.setBaseResultMap(true);
    globalConfig.setBaseColumnList(true);
    globalConfig.setFileOverride(false);//是否覆盖
    globalConfig.setServiceName("%sService");//去service的i前缀
    globalConfig.setIdType(IdType.UUID);
    globalConfig.setDateType(DateType.ONLY_DATE);
    globalConfig.setSwagger2(true);
    autoGenerator.setGlobalConfig(globalConfig);
    //2设置数据源
    DataSourceConfig dataSourceConfig = new DataSourceConfig();
    dataSourceConfig.setUrl("jdbc:mysql://itpm.itycu.com/itpm?characterEncoding=UTF8&autoReconnect=true&&allowMultiQueries=true");
    dataSourceConfig.setDriverName("com.mysql.jdbc.Driver");
    dataSourceConfig.setUsername("root");
    dataSourceConfig.setPassword("Itycu.8594");
    dataSourceConfig.setDbType(DbType.MYSQL);
    autoGenerator.setDataSource(dataSourceConfig);
    //包的配置
    PackageConfig packageConfig = new PackageConfig();
    packageConfig.setModuleName("server");
    String a="project";
    packageConfig.setParent("com.itpm");
//    packageConfig.setEntity("entity");
//    packageConfig.setMapper("mapper");
//    packageConfig.setService("service");
//    packageConfig.setController("controller");
    packageConfig.setEntity("domain."+a);
    packageConfig.setMapper("mapper."+a);
    packageConfig.setService("service."+a);
    packageConfig.setServiceImpl("service."+a+".impl");
    packageConfig.setController("controller."+a);
   // 自定义配置
    InjectionConfig cfg = new InjectionConfig() {
      @Override
      public void initMap() {
        // to do nothing
      }
    };
    // 模板引擎 freemarker
    String templatePath = "/templates/mapper.xml.ftl";
    // 模板引擎 velocity
    // String templatePath = "/templates/mapper.xml.vm";

    // 自定义输出配置
    List<FileOutConfig> focList = new ArrayList<>();
    // 自定义配置会被优先输出
    focList.add(new FileOutConfig(templatePath) {
      @Override
      public String outputFile(TableInfo tableInfo) {
        // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
        return path + "/src/main/resources/mapper/" + a
            + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
      }
    });
    /*
    cfg.setFileCreate(new IFileCreate() {
      @Override
      public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
        // 判断自定义文件夹是否需要创建
        checkDir("调用默认方法创建的目录,自定义目录用");
        if (fileType == FileType.MAPPER) {
          // 已经生成 mapper 文件判断存在,不想重新生成返回 false
          return !new File(filePath).exists();
        }
        // 允许生成模板文件
        return true;
      }
    });
    */
    cfg.setFileOutConfigList(focList);
    autoGenerator.setCfg(cfg);
    templateConfig.setXml(null);
    autoGenerator.setTemplate(templateConfig);

    autoGenerator.setPackageInfo(packageConfig);
    //4策略配置
    StrategyConfig strategyConfig = new StrategyConfig();
    strategyConfig.setNaming(NamingStrategy.underline_to_camel);
    strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
    ArrayList<String> objects = new ArrayList<>();
//    objects.add("etest");
    objects.add("rtest");
    strategyConfig.setInclude(objects.toArray(new String[objects.size()]));//设置要映射的表名

//    strategyConfig.setSuperEntityClass("");
    strategyConfig.setEntityLombokModel(true);//自动lombok
    strategyConfig.setRestControllerStyle(true);
    strategyConfig.setLogicDeleteFieldName("deletedd");//逻辑删除字段
    //自动填充配置
    TableFill createtime = new TableFill("create_time", FieldFill.INSERT);
    TableFill updatetime = new TableFill("update_time", FieldFill.UPDATE);
    ArrayList<TableFill> tableFills = new ArrayList<>();
    strategyConfig.setTableFillList(tableFills);
    //乐观锁
    strategyConfig.setVersionFieldName("berv");
    strategyConfig.setRestControllerStyle(true);
    strategyConfig.setControllerMappingHyphenStyle(true);//localhost:8080/hello_id_2
    autoGenerator.setStrategy(strategyConfig);
    autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());
    //执行
    autoGenerator.execute();
 }
}

到此这篇关于已有的springcloud+mybatis项目升级为mybatis-plus的方法的文章就介绍到这了,更多相关springcloud+mybatis项目升级为mybatis-plus内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 详解Spring-Boot中如何使用多线程处理任务

    详解Spring-Boot中如何使用多线程处理任务

    本篇文章主要介绍了详解Spring-Boot中如何使用多线程处理任务,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-03-03
  • 创建Jersey REST 服务,基于Maven的实现

    创建Jersey REST 服务,基于Maven的实现

    下面小编就为大家带来一篇创建Jersey REST 服务,基于Maven的实现。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • springboot接口参数为List的问题

    springboot接口参数为List的问题

    这篇文章主要介绍了springboot接口参数为List的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • SpringBoot结合JSR303对前端数据进行校验的示例代码

    SpringBoot结合JSR303对前端数据进行校验的示例代码

    这篇文章主要介绍了SpringBoot结合JSR303对前端数据进行校验的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • Java并发编程Callable与Future的应用实例代码

    Java并发编程Callable与Future的应用实例代码

    这篇文章主要介绍了Java并发编程Callable与Future的应用实例代码,具有一定借鉴价值,需要的朋友可以参考下
    2018-01-01
  • Jmeter如何获取jtl文件中所有的请求报文详解

    Jmeter如何获取jtl文件中所有的请求报文详解

    JMeter的可以创建一个包含测试运行结果的文本文件,这些通常称为JTL文件,因为这是默认扩展名,但可以使用任何扩展名,这篇文章主要给大家介绍了关于Jmeter如何获取jtl文件中所有的请求报文的相关资料,需要的朋友可以参考下
    2021-09-09
  • Springboot多种情况yml配置代码实例

    Springboot多种情况yml配置代码实例

    这篇文章主要介绍了Springboot多种情况yml配置代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • java 中ThreadLocal本地线程和同步机制的比较

    java 中ThreadLocal本地线程和同步机制的比较

    这篇文章主要介绍了java 中ThreadLocal本地线程和同步机制的比较的相关资料,需要的朋友可以参考下
    2017-03-03
  • 基于Java实现PDF文本旋转倾斜

    基于Java实现PDF文本旋转倾斜

    这篇文章主要介绍了基于Java实现PDF文本旋转倾斜,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • Java实现斗地主的发牌功能

    Java实现斗地主的发牌功能

    这篇文章主要为大家详细介绍了Java实现斗地主的发牌功能,含按顺序发牌和玩家牌排序显示等功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-06-06

最新评论