springboot+mybatisplus(使用总结,配置总结!!!干货...)

更新!!手把手教你搭建springboot+mybatisplus+lombok

通过代码生成器方式

写在前面的话

  • 首先搭建一个springboot项目
  • 数据库下文以mysql8为例
  • 数据库连接池采用阿里的druid

话不多说,正文开始

1.pom文件引入相关依赖(此处依赖只涉及mybatisplus相关方面)

    
    
      com.baomidou
      mybatis-plus-boot-starter
      2.2.0
    
    
    
      org.freemarker
      freemarker
    
    
      org.projectlombok
      lombok
      true
    
    
      com.baomidou
      mybatis-plus-core
      3.0.7.1
    
    
      com.baomidou
      mybatis-plus-extension
      3.0.7.1
    

2接下来就是propertises文件中相关的配置,这里面的注释应该是比较全的,总结了很多。其中的配置按需索取,没必要的配置大家注释掉就行。

#mysql
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/school?serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.dbcp2.min-idle=5
spring.datasource.dbcp2.initial-size=5
spring.datasource.dbcp2.max-total=5
spring.datasource.dbcp2.max-wait-millis=200
#mybatis-plus
# 如果是放在src/main/java目录下 classpath:/com/yourpackage/*/mapper/*Mapper.xml
# 如果是放在resource目录 classpath:/mapper/*Mapper.xml
mybatis-plus.mapper-locations=classpath:/com/springboot/study/mapper/*Mapper.xml
#实体扫描,多个package用逗号或者分号分隔
mybatis-plus.type-aliases-package=com.springboot.study.entity
#主键类型  0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
mybatis-plus.global-config.id-type=3
#字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
mybatis-plus.global-config.field-strategy=2
#驼峰下划线转换
mybatis-plus.global-config.db-column-underline=true
#mp2.3+ 全局表前缀 mp_
#mybatis-plus.global.table-prefix: mp_
#刷新mapper 调试神器
mybatis-plus.global-config.refresh-mapper=true
#数据库大写下划线转换
mybatis-plus.global-config.capital-mode=true
# Sequence序列接口实现类配置
mybatis-plus.global-config.key-generator=com.baomidou.mybatisplus.incrementer.OracleKeyGenerator
#逻辑删除配置(下面3个配置)
mybatis-plus.global-config.logic-delete-value=1
mybatis-plus.global-config.logic-not-delete-value=0
mybatis-plus.global-config.sql-injector=com.baomidou.springboot.MyMetaObjectHandler
#配置返回数据库(column下划线命名&&返回java实体是驼峰命名),自动匹配无需as(没开启这个,SQL需要写as: select user_id as userId)
mybatis-plus.configuration.map-underscore-to-camel-case=true
mybatis-plus.configuration.cache-enabled=false

#配置JdbcTypeForNull, oracle数据库必须配置
mybatis-plus.configuration.jdbc-type-for-null=null

3.将代码生成器(java文件)放置到项目中,一般放置到根目录下。下面代码粘过来,简单修改一下配置就可使用。其中jsp文件暂时生成不了,可以生成dao,mapper.xml,service,serviceimpl,到controller一整套的代码。其中基本的增删该查都已经封装好。

整理代码遇到的坑
1.mapper文件无法按照自己定义的路径生成,原因在于 /templates/mapper.xml.ftl。网上给出的有 /template/mapper.xml.vm、有
/templates/mapper.xml.vm 、有 /template/mapper.xml.ftl 等等。。。真的是让人很难选择,最后经过多番尝试确定为 /templates/mapper.xml.ftl。之后查了,这是freemarker模板引擎搞得鬼,有兴趣的同学可以查找资料了解一下。
2.还有一个和本文不是太重要的坑,mysql8连接需要指定时区,即在数据库连接url上拼上 ?serverTimezone=Hongkong
3.其他遇到的都是小坑了,具体都已经解决,代码中有相关注释说明

package com.springboot.study.mybatisplus.generator;

import com.baomidou.mybatisplus.enums.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 

* 代码生成器演示 *

*/ public class MpGenerator { public static void main(String[] args) { AutoGenerator mpg = new AutoGenerator(); //整合配置 全局配置+数据源配置+策略配置+包名策略配置 // 选择 freemarker 引擎,默认 Velocity 需要在配置文件引入依赖 mpg.setTemplateEngine(new FreemarkerTemplateEngine()); // 1全局配置 GlobalConfig gc = new GlobalConfig(); gc.setAuthor("zcf"); gc.setOutputDir("D://workspace/study/springboot_mybatisplus_lombok/src/main/java/"); gc.setFileOverride(false);// 是否覆盖同名文件,默认是false gc.setIdType(IdType.AUTO);// 主键策略 gc.setActiveRecord(true);// 不需要ActiveRecord特性的请改为false gc.setEnableCache(false);// XML 二级缓存 gc.setBaseResultMap(true);// XML ResultMap 生成基本的resultmap gc.setBaseColumnList(false);// XML columList 生成基本的sql片段 /* 自定义文件命名,注意 %s 会自动填充表实体属性! */ gc.setMapperName("%sDao"); gc.setXmlName("%sMapper"); gc.setServiceName("%sService"); gc.setServiceImplName("%sServiceImpl"); gc.setControllerName("%sController"); mpg.setGlobalConfig(gc); // 2数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setDbType(DbType.MYSQL); dsc.setTypeConvert(new MySqlTypeConvert() { // 自定义数据库表字段类型转换【可选】 @Override public DbColumnType processTypeConvert(String fieldType) { System.out.println("转换类型:" + fieldType); // 注意!!processTypeConvert 存在默认类型转换,如果不是你要的效果请自定义返回、非如下直接返回。 return super.processTypeConvert(fieldType); } }); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("password"); dsc.setUrl("jdbc:mysql://localhost:3306/school?serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf8"); mpg.setDataSource(dsc); // 3策略配置globalConfiguration中 StrategyConfig strategy = new StrategyConfig(); strategy.setEntityLombokModel(true);//实体类以lombok注解氏生产 strategy.setRestControllerStyle(true);//controller以restFule风格 // strategy.setCapitalMode(true);// 全局大写命名 ORACLE 注意 strategy.setTablePrefix(new String[]{"test_"});// 此处可以修改为您的表前缀 strategy.setNaming(NamingStrategy.nochange);// 表名生成策略 此处可以更换为underline_to_camel 下滑线转驼峰 strategy.setInclude(new String[]{"test_student"}); // 需要生成的表 // strategy.setExclude(new String[]{"test"}); // 排除生成的表 // 自定义实体父类 // strategy.setSuperEntityClass("com.baomidou.demo.TestEntity"); // 自定义实体,公共字段 // strategy.setSuperEntityColumns(new String[] { "test_id", "age" }); // 自定义 mapper 父类 // strategy.setSuperMapperClass("com.baomidou.demo.TestMapper"); // 自定义 service 父类 // strategy.setSuperServiceClass("com.baomidou.demo.TestService"); // 自定义 service 实现类父类 // strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl"); // 自定义 controller 父类 // strategy.setSuperControllerClass("com.baomidou.demo.TestController"); // 【实体】是否生成字段常量(默认 false) // public static final String ID = "test_id"; // strategy.setEntityColumnConstant(true); // 【实体】是否为构建者模型(默认 false) // public User setName(String name) {this.name = name; return this;} // strategy.setEntityBuilderModel(true); mpg.setStrategy(strategy); // 4包配置 修改包生成的名称 //pkConfig.setParent("com.imooc") // .setMapper("dao")//dao // .setService("service")//servcie // .setController("controller")//controller // .setEntity("entity") // .setXml("resource");//mapper.xml PackageConfig pc = new PackageConfig(); pc.setParent("com.springboot.study").setController("controller").setMapper("dao"); // pc.setModuleName("test"); mpg.setPackageInfo(pc); // 注入自定义配置,可以在 VM 中使用 cfg.abc 【可无】 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { Map map = new HashMap(); map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp"); this.setMap(map); } }; // 自定义 xxList.jsp 生成 List focList = new ArrayList<>(); /*focList.add(new FileOutConfig("/template/list.jsp.vm") { @Override public String outputFile(TableInfo tableInfo) { // 自定义输入文件名称 return "D://workspace/study/springboot_mybatisplus_lombok/src/main/webapp/" + tableInfo.getEntityName() + ".jsp"; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg);*/ // 调整 xml 生成目录演示 focList.add(new FileOutConfig("/templates/mapper.xml.ftl") { @Override public String outputFile(TableInfo tableInfo) { return "D://workspace/study/springboot_mybatisplus_lombok/src/main/resources/com/springboot/study/mapper/" + tableInfo.getEntityName()+"Mapper" + ".xml"; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); // 关闭默认 xml 生成,调整生成 至 根目录 TemplateConfig tc = new TemplateConfig(); tc.setXml(null); mpg.setTemplate(tc); // 自定义模板配置,可以 copy 源码 mybatis-plus/src/main/resources/templates 下面内容修改, // 放置自己项目的 src/main/resources/templates 目录下, 默认名称一下可以不配置,也可以自定义模板名称 // TemplateConfig tc = new TemplateConfig(); // tc.setController("..."); // tc.setEntity("..."); // tc.setMapper("..."); // tc.setXml("..."); // tc.setService("..."); // tc.setServiceImpl("..."); // 如上任何一个模块如果设置 空 OR Null 将不生成该模块。 // mpg.setTemplate(tc); // 执行生成 mpg.execute(); // 打印注入设置【可无】 // System.err.println(mpg.getCfg().getMap().get("abc")); } }

4.上述配置文件修改好配置之后就到了激动人心的时刻,生成代码文件,执行2步骤代码。

这是我的数据库表
这是我的数据库表
这是执行之后生成的项目目录结构,其中我把步骤2中的代码生成器放到了mybatisplus.generator中
springboot+mybatisplus(使用总结,配置总结!!!干货...)_第1张图片

5.基本使用

这是我写的controller用来测试使用,其中用到了分页插件new Page<>(1,10),其中1表示的是第几页,10表示的是我要取10条数据,是不是很方便,很easy,很舒服
springboot+mybatisplus(使用总结,配置总结!!!干货...)_第2张图片

6.结果展示

在这里插入图片描述
其中如果遇到total,pages配置项查出来的数据为0的情况,这种错误情况可能是由下面两种原因引起的.
其一: 项目的pom文件中引入了其他的分页插件,导致两个插件冲突。可以搜索一下自己的pom文件里面是否有下面的这个插件,有的话删除该依赖即可。

pagehelper-spring-boot-starter

其二: 需要在项目中添加如下配置类,重启项目即可。
springboot+mybatisplus(使用总结,配置总结!!!干货...)_第3张图片

 @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setDialectType("mysql");
        return  paginationInterceptor;
    };  

总结

最近将mybatisplus2.2.0升级到mybatisplus3.1.0,发现变化还是很大的,也踩了很多坑。记录下来分享给大家。
1.一定要添加下面这个依赖,且版本不能使用2.2.0,推荐使用3.1.0版本的,否则你会发现会一直报错找不到dao中的方法

MyBatis-Plus 从 `3.0.3` 之后移除了代码生成器与模板引擎的默认依赖,需要手动添加相关依赖:

- 添加 代码生成器 依赖 

  
      com.baomidou
      mybatis-plus-generator
      latest-version
  

2.修改生成器文件

//dsc.setDbType(DbType.MYSQL);  这行代码可以注释掉了,没测试不注释会不会报错
gc.setIdType(IdType.AUTO);// 主键策略  这里面的idType注意引入的是下面这个包

import com.baomidou.mybatisplus.annotation.IdType;

学习建议

最近我也是翻了许多相关博客内容,但是效果不是很理想。于是上mybatisplus官网上看了看,发现很多问题在官网上都可以解决,建议想要深入了解的同学移步https://mp.baomidou.com,源码包,及相关文档都可以找到。需要点耐心你一定能找到。找不到的同学可以留言评论。

2.2.0查询练习

Page chinesePage1 = chineseService.selectPage(new Page<>(1, 10),
                new EntityWrapper().setSqlSelect("chinese", "uni"));
        System.out.println(chinesePage1.toString());

        Page> mapPage = chineseService.selectMapsPage(new Page<>(1, 10),
                new EntityWrapper().setSqlSelect("chinese", "uni"));
        System.out.println(mapPage);

        List> chinese = chineseService.selectMaps(new EntityWrapper()
                .setSqlSelect("chinese"));
        System.out.println(chinese);

        Chinese chinese1 = new Chinese();
        chinese1.setId("f888b62dc1344a51aa120d8410c97160");

        /*多表分页查询*/
        Page> studentPage = chineseService.selectStudentListPage(new Page<>(1, 2), chinese1);
        System.out.println(studentPage);




/*//        查询练习之 分页查询
        Page chinesePage = chineseService.selectPage(new Page<>(1, 10));
        System.out.println(chinesePage.toString());
        System.out.println("--------------------------------------------------------------------");

//        查询练习之 自定义dao(通过@select标签)
        List chinese = chineseService.selectAll();
        System.out.println(chinese.toString());
        System.out.println("--------------------------------------------------------------------");

//        查询练习 selectOne
        List chinese2 = chineseDao.selectPage(new Page(1, 3), new EntityWrapper()
                .eq("id", "38244336880746cfbb46579032bd07ec"));
        System.out.println(chinese2.toString());


        System.out.println("--------------------------------------------------------------------");

//        查询练习之 自带byID查询
        Chinese chinese1 = chineseService.selectById("38244336880746cfbb46579032bd07ec");
        System.out.println(chinese1.toString());
        System.out.println("--------------------------------------------------------------------");*/



        return chinese3;

3.1.1查询练习

/*关联查询*/

        Surname surname = new Surname();
        surname.setId("f888b62dc1344a51aa120d8410c97160");

        Page> mapPage = surnameService.selectStudentListPage(new Page(1, 2), surname);
        System.out.println("多表分页查询:"+ mapPage);


        /*查询表中name列*/
        List name = surnameService.list(new QueryWrapper().select("name"));
        System.out.println("查询表中的某列值其他字段返回的都是null" + name);

        IPage> name1 = surnameService.pageMaps(
                new Page<>(1, 5), new QueryWrapper().select("name"));
        System.out.println("分页查询表中的某一列值,该种返回不会包括其他字段值:" + name1);

        /*getMap*/
        Map id1 = surnameService.getMap(new QueryWrapper()
                .eq("id", "0056623880974129a601ea39af211cda"));
        System.out.println("以map形式返回-->getMap:"+id1);

        List> maps = surnameService.listMaps();
        System.out.println("以list形式返回所有-->listMaps:" + maps);

        List id2 = surnameService.list(new QueryWrapper()
                .eq("id", "0056623880974129a601ea39af211cda"));
        System.out.println("以list形式返回单个-->listMaps:" + id2);

        List list = surnameService.list();
        System.out.println("以list形式返回所有-->list:" + list);

        List> maps1 = surnameService.listMaps(new QueryWrapper()
                .eq("id", "0056623880974129a601ea39af211cda")
                .or().eq("id", "01e8a582f50b4675974dde3c59b5161d"));
        System.out.println("查询id为他或她的集合对象:"+maps1);


        /*getOne*/
        Surname byId = surnameService.getById("0056623880974129a601ea39af211cda");
        System.out.println("通过自带id查询:"+byId);

        Surname id = surnameService.getOne(new QueryWrapper()
                .eq("id", "01e8a582f50b4675974dde3c59b5161d"));
        System.out.println("通过new QueryWrapper()条件构造器查询:"+id.toString());

        Surname one = surnameService.getOne(new QueryWrapper()
                .eq("name", "仝").eq("uni", "1"));
        System.out.println("条件构造器查询:"+one);


        /*分页查询 */
        IPage page = surnameService.page(new Page<>(1, 10));
        System.out.println("分页查询:"+page.toString());

MybatisPlusConfig.java 配置文件,分页插件,多扫描dao,sql语句展示(性能分析器等都在此设置)

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
//import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
//@MapperScan("com.baomidou.springboot.mapper*")//这个注解,作用相当于下面的@Bean MapperScannerConfigurer,2者配置1份即可
public class MybatisPlusConfig {

    /**
     * SQL输出拦截器,性能分析器,不建议生产环境使用,开发环境使用(方便)
     */
     @Bean
     public PerformanceInterceptor performanceInterceptor() {
     PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
     //sql格式化
     performanceInterceptor.setFormat(true);
     return performanceInterceptor;
     }


    /**
     * mybatis-plus分页插件
* 文档:http://mp.baomidou.com
*/ @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); //paginationInterceptor.setDialectType("mysql"); //指定数据库类型,不写自动匹配 return paginationInterceptor; }; //@Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer(); scannerConfigurer.setBasePackage("com.baomidou.springboot.mapper*"); return scannerConfigurer; } }

你可能感兴趣的:(后端)