Springboot集成MyBatisPlus

一、MyBatisPlus简介

       MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

1、功能

  1. 单表CURD(简单 +  批量)操作,自动完成;
  2. 分页插件,Count  查询 自动 或 自定义SQL 查询;
  3. Spring 根据不同环境加载不同配置支持。

2、特性

  1. 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑;
  2. 损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作;
  3. 强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求;
  4. 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错;
  5. 支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer2005、SQLServer 等多种数据库;
  6. 支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题;
  7. 支持 XML 热加载:Mapper 对应的 XML 支持热加载,对于简单的 CRUD 操作,甚至可以无 XML 启动;
  8. 支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作;
  9. 支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere );
  10. 支持关键词自动转义:支持数据库关键词(order、key......)自动转义,还可自定义关键词;
  11. 内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用;
  12. 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询;
  13. 内置性能分析插件:可输出 Sql 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询;
  14. 内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作;
  15. 内置 Sql 注入剥离器:支持 Sql 注入剥离,有效预防 Sql 注入攻击。

3、架构

4、代码及文档发布地址

二、MyBatis Plus

1、配置 MapperScan 注解

 启动类中配置:

@SpringBootApplication
@MapperScan("com.example.demo.*.mapper")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(QuickStartApplication.class, args);
    }
}

自定义配置类中配置: 

@EnableTransactionManagement
@Configuration
@MapperScan("com.example.demo.*.mapper")
public class MybatisPlusConfig {

}

2、Mapper CRUD 接口(Mapper接口需要继承BaseMapper<T>)

  • int insert(T entity); //插入一条记录,返回插入成功记录数
  • int deletebyId(Serializable id); //根据 ID 删除,返回删除成功记录数
  • int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);  //根据 columnMap 条件,删除记录,返回删除成功记录数
  • int delete(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); //根据 entity 条件,删除记录,返回删除成功记录数
  • int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); // 删除(根据ID 批量删除),返回删除成功记录数
  • int updateById(@Param(Constants.ENTITY) T entity); // 根据 ID 修改,返回修改记录数
  • int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper); // 根据 whereEntity 条件,更新记录,返回记录数
  • T selectById(Serializable id);  // 根据 ID 查询
  • List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); //  查询(根据ID 批量查询)
  • List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);// 查询(根据 columnMap 条件)
  • T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); //根据 entity 条件,查询一条记录
  • Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 根据 Wrapper 条件,查询总记录数
  • List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 根据 entity 条件,查询全部记录
  • List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); //根据 Wrapper 条件,查询全部记录
  • List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); //根据 Wrapper 条件,查询全部记录,只返回第一个字段的值
  • IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper); //根据 entity 条件,查询全部记录(并翻页)
  • IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 根据 Wrapper 条件,查询全部记录(并翻页)

3、Service CRUD 接口(Service接口需要继承IService<T>,实现类继承ServiceImp<TMapper,T>)

  • boolean save(T entity); // 插入一条记录(选择字段,策略插入)
  • boolean save(T entity); // 插入一条记录(选择字段,策略插入)
  • boolean saveOrUpdateBatch(Collection<T> entityList); //批量修改插入
  • boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize); //批量修改插入
  • boolean removeById(Serializable id); // 根据 ID 删除
  • boolean removeByMap(Map<String, Object> columnMap); //根据 columnMap 条件,删除记录
  • boolean remove(Wrapper<T> queryWrapper); // 根据 entity 条件,删除记录
  • boolean removeByIds(Collection<? extends Serializable> idList); //删除(根据ID 批量删除)
  • boolean updateById(T entity); // 根据 ID 选择修改
  • boolean update(T entity, Wrapper<T> updateWrapper); //根据 whereEntity 条件,更新记录
  • boolean updateBatchById(Collection<T> entityList, int batchSize); //根据ID 批量更新
  • boolean saveOrUpdate(T entity); //TableId 注解存在更新记录,否插入一条记录
  • T getById(Serializable id); //根据 ID 查询
  • Collection<T> listByIds(Collection<? extends Serializable> idList); //查询(根据ID 批量查询)
  • Collection<T> listByMap(Map<String, Object> columnMap); // 查询(根据 columnMap 条件)
  • T getOne(Wrapper<T> queryWrapper, boolean throwEx); //根据 Wrapper,查询一条记录
  • Map<String, Object> getMap(Wrapper<T> queryWrapper); //根据 Wrapper,查询一条记录
  • Object getObj(Wrapper<T> queryWrapper); // 根据 Wrapper,查询一条记录
  • int count(Wrapper<T> queryWrapper); //根据 Wrapper 条件,查询总记录数
  • List<T> list(Wrapper<T> queryWrapper); //查询列表
  • IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper); //  翻页查询
  • List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper); // 查询列表
  • List<Object> listObjs(Wrapper<T> queryWrapper); //根据 Wrapper 条件,查询全部记录
  • IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper); // 翻页查询

4、条件构造器(AbstractWrapper-->QueryWrapper&UpdateWrapper

  • allEq:全部eq(或个别isNull)    
  •      例1: allEq({id:1,name:"老王",age:null})--->id = 1 and name = '老王' and age is null
  •      例2: allEq({id:1,name:"老王",age:null}, false)--->id = 1 and name = '老王'
  • eq:等于 =
  •      例1: allEq((k,v) -> k.indexOf("a") > 0, {id:1,name:"老王",age:null})--->name = '老王' and age is null
  •      例2: allEq((k,v) -> k.indexOf("a") > 0, {id:1,name:"老王",age:null}, false)--->name = '老王'
  • ne:不等于 <>
  •      例: ne("name", "老王")--->name <> '老王'
  • gt:大于 >
  •      例: gt("age", 18)--->age > 18
  • ge:大于等于 >=
  •      例: ge("age", 18)--->age >= 18
  • lt:小于 <
  •      例: lt("age", 18)--->age < 18
  • le:小于等于 <=
  •      例: le("age", 18)--->age <= 18
  • between:BETWEEN 值1 AND 值2
  •      例: between("age", 18, 30)--->age between 18 and 30
  • notBetween:NOT BETWEEN 值1 AND 值2
  •      例: notBetween("age", 18, 30)--->age not between 18 and 30
  • like:LIKE '%值%'
  •       例: like("name", "王")--->name like '%王%'
  • notLike:NOT LIKE '%值%'
  •       例: notLike("name", "王")--->name not like '%王%'
  • likeLeft:LIKE '%值'
  •       例: likeLeft("name", "王")--->name like '%王'
  • likeRight:LIKE '值%'
  •       例: likeRight("name", "王")--->name like '王%'
  • isNull:字段 IS NULL
  •       例: isNull("name")--->name is null
  • isNotNull:字段 IS NULL
  •       例: isNotNull("name")--->name is not null
  • in:字段 IN (value.get(0), value.get(1), ...)
  •       例: in("age",{1,2,3})--->age in (1,2,3)
  • notIn:字段 IN (value.get(0), value.get(1), ...)
  •       例: notIn("age",{1,2,3})--->age not in (1,2,3)
  • inSql:字段 IN ( sql语句 )
  •       例: inSql("age", "1,2,3,4,5,6")--->age in (1,2,3,4,5,6)
  •       例: inSql("id", "select id from table where id < 3")--->id in (select id from table where id < 3)
  • notInSql:字段 NOT IN ( sql语句 )
  •       例: notInSql("age", "1,2,3,4,5,6")--->age not in (1,2,3,4,5,6)
  •       例: notInSql("id", "select id from table where id < 3")--->age not in (select id from table where id < 3)
  • groupBy:分组:GROUP BY 字段, ...
  •       例: groupBy("id", "name")--->group by id,name
  • orderByAsc:排序:ORDER BY 字段, ... ASC
  •       例: orderByAsc("id", "name")--->order by id ASC,name ASC
  • orderByDesc:排序:ORDER BY 字段, ... DESC
  •       例: orderByDesc("id", "name")--->order by id DESC,name DESC
  • orderBy:排序:ORDER BY 字段, ...
  •       例: orderBy(true, true, "id", "name")--->order by id ASC,name ASC
  • having:HAVING ( sql语句 )
  •       例: having("sum(age) > 10")--->having sum(age) > 10
  •       例: having("sum(age) > {0}", 11)--->having sum(age) > 11
  • or:拼接 OR 。注意事项:主动调用or表示紧接着下一个方法不是用and连接!(不调用or则默认为使用and连接)
  •       例: eq("id",1).or().eq("name","老王")--->id = 1 or name = '老王'
  •       例: or(i -> i.eq("name", "李白").ne("status", "活着"))--->or (name = '李白' and status <> '活着')
  • and:AND 嵌套
  •       例: and(i -> i.eq("name", "李白").ne("status", "活着"))--->and (name = '李白' and status <> '活着')
  • apply:拼接 sql 。 注意事项:该方法可用于数据库函数 动态入参的params对应前面sqlHaving内部的{index}部分.这样是不会有sql注入风险的,反之会有!
  •      例: apply("id = 1")--->having sum(age) > 10
  •      例: apply("date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")--->date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")
  •      例: apply("date_format(dateColumn,'%Y-%m-%d') = {0}", "2008-08-08")--->date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")
  • last:无视优化规则直接拼接到 sql 的最后。注意事项:只能调用一次,多次调用以最后一次为准 有sql注入的风险,请谨慎使用
  •      例: last("limit 1")
  • exists:拼接 EXISTS ( sql语句 )
  •      例: exists("select id from table where age = 1")--->exists (select id from table where age = 1)
  • notExists:拼接 NOT EXISTS ( sql语句 )
  •      例: notExists("select id from table where age = 1")--->not exists (select id from table where age = 1)
  • nested:正常嵌套 不带 AND 或者 OR
  •      例: nested(i -> i.eq("name", "李白").ne("status", "活着"))--->(name = '李白' and status <> '活着')

注意:  使用的是数据库字段,不是 Java 属性。

5、分页插件

spring XML方式配置:

<!-- spring xml 方式 -->
<plugins>
    <plugin interceptor="com.baomidou.mybatisplus.plugins.PaginationInterceptor">
        <property name="sqlParser" ref="自定义解析类、可以没有" />
        <property name="dialectClazz" value="自定义方言类、可以没有" />
    </plugin>
</plugins>

springboot Bean的配置方式:

@Bean
public PaginationInterceptor paginationInterceptor() {
   return new PaginationInterceptor();
}

6、逻辑删除

@Bean
public ISqlInjector sqlInjector() {
     return new LogicSqlInjector();
}

   逻辑删除:  并不会真正的从数据库中将数据删除掉,而是将当前被删除的这条数据中的一个逻辑删除字段置为删除状态。             即:tbl_user   logic_flag = 1    →  -1 

  • logicDeleteValue   逻辑删除全局值 
  • logicNotDeleteValue  逻辑未删除全局值
  • 在 POJO 的逻辑删除字段  添加  @TableLogic 注解
  • 会在 mp 自带查询和更新方法的 sql 后面,追加『逻辑删除字段』 = 『LogicNotDeleteValue默认值』 删除方法:deleteById()和其他 delete 方法,  底层 SQL 调用的是 update tbl_xxx  set  『逻辑删除字段』=『logicDeleteValue 默认值』

7、执行分析插件

@Bean
public PerformanceInterceptor performanceInterceptor() {
       PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
       performanceInterceptor.setFormat(true);
       return performanceInterceptor;
}

功能: 

  • SQL 执行分析拦截器,只支持 MySQL5.6.3 以上版本;
  • 该插件的作用是分析  DELETE   UPDATE 语句,防止小白或者恶意进行 DELETE  UPDATE 全表操作;
  • 只建议在开发环境中使用,不建议在生产环境使用;
  • 在插件的底层  通过 SQL 语句分析命令:Explain分析当前的 SQL 语句, 根据结果集中的 Extra 列来断定当前是否全表操作。

8、性能分析插件

spring XML配置方式:

<plugins>

    <!-- SQL 执行性能分析,开发环境使用,线上不推荐。 maxTime 指的是 sql 最大执行时长 -->
    <plugin interceptor="com.baomidou.mybatisplus.plugins.PerformanceInterceptor">
        <property name="maxTime" value="100" />
        <!--SQL是否格式化 默认false-->
        <property name="format" value="true" />
    </plugin>
</plugins>

springboot bean的方式:

@Bean
@Profile({"dev","test"})// 设置 dev test 环境开启
public PerformanceInterceptor performanceInterceptor() {
      return new PerformanceInterceptor();
}

功能: 

  • 性能分析拦截器,用于输出每条  SQL  语句及其执行时间 ;
  • SQL 性能执行分析,开发环境使用,超过指定时间,停止运行,有助于发现问题。

9、乐观锁插件

spring XML配置方式:

<bean class="com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor"/>

springboot bean配置方式:

@Bean
public OptimisticLockerInterceptor optimisticLockerInterceptor() {
    return new OptimisticLockerInterceptor();
}

乐观锁实现方式:

  • 取出记录时,获取当前version
  • 更新时,带上这个version
  • 执行更新时, set version = newVersion where version = oldVersion
  • 如果version不对,就更新失败
  • 注解实体字段 @Version 必须要的
  • 支持的数据类型只有:int,Integer,long,Long,Date,Timestamp,LocalDateTime

10、ActiveRecord(活动记录)

        Active  Record(活动记录),是一种领域模型模式,特点是一个模型类对应关系型数据库中的一个表,而模型类的一个实例对应表中的一行记录;

        ActiveRecord 一直广受动态语言(PHP 、Ruby等)的喜爱,而  Java  作为准静态语言,对于ActiveRecord往往只能感叹其优雅,所以 MP 也在  AR  道路上进行了一定的探索;

使用 AR 模式:仅仅需要让实体类继承 Model 类且实现主键指定方法,即可开启 AR 之旅。

public class User extends Model<User> {

    private Integer id;

    ......

    @Override
    protected Serializable pkVal() {
        return id;
    }
}

三、代码生成器

package com.example.demo.automatic;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import static com.baomidou.mybatisplus.generator.config.rules.DateType.ONLY_DATE;

/**
 * 类名:CodeGenerator.java
 * 功能:执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
 * 描述:
 * 创建人:typ
 * 创建时间:2018/11/20 15:34
 * 修改人:
 * 修改描述:
 * 修改时间:
 */
@Slf4j
public class CodeGenerator {

    /**
     * 方法名:
     * 功能:读取控制台内容
     * 描述:
     * 创建人:typ
     * 创建时间:2018/11/20 15:34
     * 修改人:
     * 修改描述:
     * 修改时间:
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + ":");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "!");
    }

    /**
     * 方法名:
     * 功能:代码生成
     * 描述:
     * 创建人:typ
     * 创建时间:2018/11/20 15:35
     * 修改人:
     * 修改描述:
     * 修改时间:
     */
    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 1.全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = "F:/workingExperience/workspace/workIeda/springboot-master/springboot-mybatisplus";
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("typ"); // 注释作者
        gc.setDateType(ONLY_DATE); // 注释时间类型
        gc.setOpen(false);
        gc.setActiveRecord(true); //是否需要ActiveRecord特性
        gc.setEnableCache(false); // mapper.xml中开启二级缓存
        gc.setBaseResultMap(true); // mapper.xml 中使用ResultMap
        gc.setBaseColumnList(true); // mapper.xml中使用columList
        gc.setKotlin(true); //是否生成kotlin代码
        gc.setControllerName("%sController"); //自定义controoler类的命名格式,列如:XxxController.java
        gc.setServiceName("%sService"); //自定义service接口的命名格式,列如:XxxService.java
        gc.setServiceImplName("%sServiceImpl"); //自定义service实现类的命名格式,列如:XxxServiceImpl.java
        gc.setMapperName("%sMapper"); //自定义mapper接口的命名格式,列如:XxxMapper.java
        gc.setXmlName("%sMapper"); //自定义mapper.xml的命名格式,列如:XxxMapper.xml
        mpg.setGlobalConfig(gc);

        // 2.数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL); // 配置数据库类型
        dsc.setUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("admin");
        mpg.setDataSource(dsc);

        // 4.包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(scanner("模块名")); // 控制台输入方式
        pc.setParent("com.example.demo");
//        pc.setController("controller");
//        pc.setEntity("entity");
//        pc.setService("service");
//        pc.setServiceImpl("impl");
        mpg.setPackageInfo(pc);

        // 5.自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };

        // 6.自定mapper.xml生成路径
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null));

        // 7.策略配置
        StrategyConfig strategy = new StrategyConfig();
//        strategy.setCapitalMode(true); // 全局大写命名
//        strategy.setTablePrefix(new String[]{"tb_","tbs_"}); // 数据库表前缀
        strategy.setNaming(NamingStrategy.underline_to_camel); // 表名生成策略
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setInclude(scanner("表名")); // 需要生成的表名
        strategy.setSuperEntityColumns("id");
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

}

 四、集成Springboot

1、添加依赖

特别说明:  Mybatis 及 Mybatis-Spring 依赖请勿加入项目配置,以免引起版本冲突。Mybatis-Plus 会自动帮你维护!

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.46</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
		</dependency>
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.0.1</version>
		</dependency>
		<!-- 多数据源配置 -->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
			<version>2.4.2</version>
		</dependency>
		<!-- velocity 模板引擎, 默认 -->
		<dependency>
			<groupId>org.apache.velocity</groupId>
			<artifactId>velocity-engine-core</artifactId>
			<version>2.0</version>
		</dependency>
		<!-- freemarker 模板引擎 -->
		<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
			<version>2.3.28</version>
		</dependency>
	</dependencies>

2、配置文件

application.yml

server:
  port: 8081

# mysql
spring:
    profiles:
        active: dev
    datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.jdbc.Driver
    jackson:
        time-zone: GMT+8
        date-format: yyyy-MM-dd HH:mm:ss

# 主键策略配置
mybatis-plus:
  global-config:
    db-config:
      id-type: auto

application-dev.yml

spring:
    datasource:
      url: jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&autoReconnect=true
      username: root
      password: admin
      #  连接池的配置信息
      initialSize: 10
      minIdle: 10
      maxActive: 100
      maxWait: 60000
      timeBetweenEvictionRunsMillis: 300000
      minEvictableIdleTimeMillis: 3600000
      validationQuery: SELECT 1 FROM DUAL
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      poolPreparedStatements: true
      maxPoolPreparedStatementPerConnectionSize: 20

3、MybatisPlus配置

package com.example.demo.config;

import com.baomidou.mybatisplus.extension.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;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * 路径:com.example.demo.config
 * 类名:
 * 功能:MybatisPlus配置文件
 * 备注:
 * 创建人:typ
 * 创建时间:2018/11/23 15:56
 * 修改人:
 * 修改备注:
 * 修改时间:
 */
@EnableTransactionManagement
@Configuration
@MapperScan("com.example.demo.*.mapper")
public class MybatisPlusConfig {

    /**
     * 方法名:
     * 功能:mybatis-plus分页插件
     * 描述:
     * 创建人:typ
     * 创建时间:2018/11/23 15:57
     * 修改人:
     * 修改描述:
     * 修改时间:
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    /**
     * 方法名:
     * 功能:打印 sql
     * 描述:SQL执行效率插件【生产环境可以关闭】
     * 创建人:typ
     * 创建时间:2018/11/27 9:58
     * 修改人:
     * 修改描述:
     * 修改时间:
     */
    @Bean
    public PerformanceInterceptor performanceInterceptor() {
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        performanceInterceptor.setFormat(true);
        return performanceInterceptor;
    }



}

4、编码

实体类

package com.example.demo.crud.entity;

import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;

import java.io.Serializable;

/**
 * 路径:com.example.demo.crud.entity
 * 类名:
 * 功能:1.使用mybatis plus的一般形式
 *      2.使用myBatis Plus继承Model的形式
 * 备注:
 * 创建人:typ
 * 创建时间:2018/11/23 14:09
 * 修改人:
 * 修改备注:
 * 修改时间:
 */
//1.使用mybatis plus的一般形式
/*@Data
public class User {

    private Integer id;

    private String username;

    private String password;

}*/

//2.使用myBatis Plus继承Model的形式
@Data
public class User extends Model<User> {

    private Integer id;

    private String username;

    private String password;

    @Override
    protected Serializable pkVal() {
        return id;
    }
}

mapper接口

package com.example.demo.crud.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.crud.entity.User;

/**
 * 路径:com.example.demo.crud.mapper
 * 类名:
 * 功能:《用一句描述一下》
 * 备注:
 * 创建人:typ
 * 创建时间:2018/11/23 14:09
 * 修改人:
 * 修改备注:
 * 修改时间:
 */
public interface UserMapper extends BaseMapper<User> {
}

service接口

package com.example.demo.crud.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.crud.entity.User;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 路径:com.example.demo.crud.service
 * 类名:
 * 功能:《用一句描述一下》
 * 备注:
 * 创建人:typ
 * 创建时间:2018/11/23 16:21
 * 修改人:
 * 修改备注:
 * 修改时间:
 */
public interface UserService extends IService<User> {
}

service实现类

package com.example.demo.crud.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.crud.entity.User;
import com.example.demo.crud.mapper.UserMapper;
import com.example.demo.crud.service.UserService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 路径:com.example.demo.crud.service.impl
 * 类名:
 * 功能:《用一句描述一下》
 * 备注:
 * 创建人:typ
 * 创建时间:2018/11/23 16:23
 * 修改人:
 * 修改备注:
 * 修改时间:
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
}

controller层

package com.example.demo.crud.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.crud.entity.User;
import com.example.demo.crud.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.security.PublicKey;

/**
 * 路径:com.example.demo.crud.controller
 * 类名:
 * 功能:《用一句描述一下》
 * 备注:
 * 创建人:typ
 * 创建时间:2018/11/23 16:29
 * 修改人:
 * 修改备注:
 * 修改时间:
 */
@Slf4j
@RestController
@RequestMapping("/crud")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/getById")
    public String getById(Integer id) {
        log.info("find start id:{}", id);
        User user = userService.getById(id);
        log.info("find end result:{}", JSON.toJSON(user));
        return user.toString();
    }

    @ResponseBody
    @GetMapping("/find")
    public IPage<User> find(Integer pageNum, Integer pageSize, String name) {
        log.info("find start pageNum:{},pageSize:{},name:{}", pageNum, pageSize, name);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", name);

        IPage<User> page = userService.page(new Page<>(pageNum, pageSize), queryWrapper);
        log.info("find end result:{}", JSON.toJSON(page));
        return page;
    }
}

5、MyBatisPlus功能测试

package com.example.demo.crud;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.crud.entity.User;
import com.example.demo.crud.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

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

/**
 * 路径:com.example.demo.crud
 * 类名:
 * 功能:使用mybatis plus的一般形式
 * 备注:
 * 创建人:typ
 * 创建时间:2018/11/23 14:13
 * 修改人:
 * 修改备注:
 * 修改时间:
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {

    @Autowired
    UserMapper userMapper;

    /**
     * 方法名:
     * 功能:查询
     * 描述:
     * 创建人:typ
     * 创建时间:2018/11/23 15:07
     * 修改人:
     * 修改描述:
     * 修改时间:
     */
    @Test
    public void  select(){
        //------------------selectById------------------------------
        User user = userMapper.selectById(1);
        System.out.println("user:"+user.toString());

        //--------------------selectList--------------------
        List<User> list = userMapper.selectList(null);
        System.out.println("list:"+ Arrays.toString(list.toArray()));

        //------------------------selectByMap----------------
        Map<String,Object> map = new HashMap<>();
        map.put("id",1);
        map.put("username","root");
        List<User> maps = userMapper.selectByMap(map);
        System.out.println("map:"+maps.toString());

        //------------------selectPage------------------------------------
        User u = new User();
        u.setUsername("AAA");
        QueryWrapper<User> query = new QueryWrapper<>(u);
        IPage<User> pages = userMapper.selectPage(new Page<>(0,10),query);
        System.out.println("pages:"+pages.getRecords());

        //---------------------------------selectMapsPage-------------------------------------
        User uMap = new User();
        uMap.setUsername("AAA");
        QueryWrapper<User> queryMap = new QueryWrapper<>(uMap);
        IPage<Map<String, Object>>  mapPages = userMapper.selectMapsPage(new Page<>(0,10),queryMap);
        System.out.println("mapPages:"+mapPages.getRecords());
    }

    /**
     * 方法名:
     * 功能:插入操作
     * 描述:
     * 创建人:typ
     * 创建时间:2018/11/23 14:41
     * 修改人:
     * 修改描述:
     * 修改时间:
     */
    @Test
    public void insert(){
        User user = new User();
        user.setUsername("AAA");
        user.setPassword("AAAAA");
        Integer result = userMapper.insert(user);
        // 插入成功放回值
        System.out.println("result:"+result);
    }

    /**
     * 方法名:
     * 功能:修改
     * 描述:
     * 创建人:typ
     * 创建时间:2018/11/23 15:01
     * 修改人:
     * 修改描述:
     * 修改时间:
     */
    @Test
    public void update(){
        User user = new User();
        user.setId(8);
        user.setUsername("AAA");
        user.setPassword("AAAAA");
        Integer result = userMapper.updateById(user);
        System.out.println("result:"+result);
    }

    /**
     * 方法名:
     * 功能:删除
     * 描述:
     * 创建人:typ
     * 创建时间:2018/11/23 15:05
     * 修改人:
     * 修改描述:
     * 修改时间:
     */
    @Test
    public void del(){
        Integer row = userMapper.deleteById(8);
        System.out.println("result:"+row);
    }

}
ActiveRecord特性测试,实体类需要集成Model<T>,泛型为当前实体类
package com.example.demo.crud;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.crud.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * 路径:com.example.demo.crud
 * 类名:
 * 功能:使用myBatis Plus继承Model的形式
 * 备注:
 * 创建人:typ
 * 创建时间:2018/11/27 10:04
 * 修改人:
 * 修改备注:
 * 修改时间:
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperModelTest {

    /**
     * 方法名:
     * 功能:insert
     * 描述:
     * 创建人:typ
     * 创建时间:2018/11/27 10:06
     * 修改人:
     * 修改描述:
     * 修改时间:
     */
    @Test
    public void testInsert(){
        User user = new User();
        //注意查询语法,与普通的有所不一样
        user.setId(12);
        user.setUsername("dhsfg");
        user.setPassword("11111");
        Boolean result = user.insert();
        System.out.println(result);
    }

    /**
     * 方法名:
     * 功能:select
     * 描述:
     * 创建人:typ
     * 创建时间:2018/11/27 10:06
     * 修改人:
     * 修改描述:
     * 修改时间:
     */
    @Test
    public void testSelect(){
        User user = new User();
        //注意查询语法,与普通的有所不一样
        user.setId(12);
        User result = user.selectById();
        System.out.println(result);
    }

    /**
     * 方法名:
     * 功能:selectPage
     * 描述:
     * 创建人:typ
     * 创建时间:2018/11/27 10:06
     * 修改人:
     * 修改描述:
     * 修改时间:
     */
    @Test
    public void testSelectPage(){
        User user = new User();
        //注意查询语法,与普通的有所不一样
        user.setId(12);
        IPage<User> result = user.selectPage(new Page<User>(1,2),null);
        System.out.println(result.getRecords());
    }
}

更多内容请阅读MyBatis-Plus官网!

 

欢迎关注

 

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: 在SpringBoot集成MybatisPlus需要完成以下步骤: 1. 添加依赖:在项目的pom.xml文件中添加MybatisPlus和Mybatis的依赖。 2. 配置数据源:在application.properties或application.yml文件中配置数据库连接信息和MybatisPlus的相关配置信息,比如Mapper映射文件的路径等。 3. 创建Mapper接口:定义一个Mapper接口,继承MybatisPlus提供的BaseMapper接口,并添加相应的注解。 4. 创建实体类:创建与数据库表对应的实体类,并使用注解来映射实体类属性和数据库表字段。 5. 编写业务代码:在Service层中编写相应的业务代码,包括调用Mapper接口中的方法来完成对数据库的操作。 6. 测试:编写测试代码来测试上述功能是否正常运行。 以上就是SpringBoot集成MybatisPlus的基本步骤,希望能对您有所帮助。 ### 回答2: Spring Boot集成MyBatis Plus是一种常见的组合方式,可以快速、简单地进行Java开发。下面我会简单介绍一下集成的步骤和优势。 首先,为了集成Spring Boot和MyBatis Plus,我们需要在pom.xml中添加相关依赖。这些依赖将负责将 MyBatis Plus 和 Spring Boot 连接起来。然后,我们需要在配置文件中配置数据库连接信息和MyBatis Plus的一些参数。 接下来,我们可以开始编写我们的实体类和Mapper接口。MyBatis Plus提供了很多方便的注解和接口,可以省去我们编写大量的CRUD操作代码。例如,使用@TableName注解来指定表名,使用@Mapper注解来标识Mapper接口等。 然后,我们可以在Service类中使用MyBatis Plus提供的方法来进行数据操作。例如,使用getById()方法来根据主键查询数据,使用insert()方法来插入数据等。MyBatis Plus还提供了更多的方法和条件查询的支持,可以根据具体的业务需求选择合适的方法来使用。 最后,可以使用Spring Boot提供的Web框架来暴露我们的接口,并进行测试和使用。可以使用Postman等工具来发送HTTP请求,验证我们的接口是否正常工作。 通过集成Spring Boot和MyBatis Plus,我们可以大大提高开发效率和代码质量。MyBatis Plus提供了许多便捷的功能,可以简化我们的开发过程,减少编码工作量。而Spring Boot则提供了一种快速、简单的开发框架,可以帮助我们更好地构建和管理我们的项目。 总结一下,Spring Boot集成MyBatis Plus是一种常用且优秀的开发方式。它可以帮助我们快速搭建项目、简化开发流程,同时提供了很多便捷的功能和工具,可以提高我们的开发效率和代码质量。希望这些信息对你有所帮助! ### 回答3: Spring Boot为我们提供了很多便利,可以简化我们的开发工作。MyBatis Plus是一个优秀的ORM框架,能够帮助我们更加轻松地操作数据库。 要在Spring Boot项目中集成MyBatis Plus,首先需要在pom.xml文件中添加相关的依赖。可以使用Maven或Gradle管理项目依赖,建议使用Maven。添加MyBatis Plus的依赖后,可以在项目中使用MyBatis Plus提供的各种功能,如分页查询、条件查询、条件更新等。 在Spring Boot中配置MyBatis Plus也非常简单。只需要在application.properties或application.yml文件中添加相应的配置项即可。配置项包括数据源信息、MyBatis Plus的配置信息等。在配置数据源信息时,可以使用Spring Boot提供的自动配置功能,根据配置文件中的相关配置自动创建数据源并注入到项目中。在配置MyBatis Plus信息时,可以自定义表名前缀、表名生成策略等。 在编写代码时,可以直接使用MyBatis Plus提供的各种查询方法,也可以使用自定义的SQL语句进行操作。MyBatis Plus还提供了实体类生成器工具,可以根据数据库表结构自动生成实体类,减少手动编写实体类的工作量。 总的来说,Spring Boot集成MyBatis Plus非常简单且方便。通过集成MyBatis Plus,我们可以更加轻松地操作数据库,提高开发效率。同时,MyBatis Plus在性能优化上也做了很多工作,对于大型系统的开发也有很好的支持。希望以上回答能够对您有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值