MyBatis-Plus自动生成mapper service controller

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

官网https://mp.baomidou.com/guide/

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

MyBatis-Plus自动生成mapper service controller_第1张图片

全新的 MyBatis-Plus 3.0 版本基于 JDK8,提供了 lambda 形式的调用,所以安装集成 MP3.0 要求如下:

  • JDK 8+
  • Maven or Gradle

配置

maven工程中添加依赖

Spring Boot 中pom依赖


    com.baomidou
    mybatis-plus-boot-starter
    3.0.6

Spring MVC 中pom依赖


    com.baomidou
    mybatis-plus
    3.0.6

WARNING

引入 MyBatis-Plus 之后请不要再次引入 MyBatis 以及 MyBatis-Spring,以避免因版本差异导致的问题。

Spring Boot 工程:

配置 MapperScan 注解

@SpringBootApplication
@MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(QuickStartApplication.class, args);
    }

}

Spring MVC 工程:

配置 MapperScan


    

调整 SqlSessionFactory 为 MyBatis-Plus 的 SqlSessionFactory


    

通常来说,一般的简单工程,通过以上配置即可正常使用 MyBatis-Plus

 

代码生成器

package com.xuxu.common;

import java.util.Scanner;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

public class CodeGenerator {
	// 演示例子,执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
	    /**
	     * 

* 读取控制台内容 *

*/ 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 + "!"); } public static void main(String[] args) { // 代码生成器 AutoGenerator mpg = new AutoGenerator(); // 选择 freemarker 引擎,默认 Veloctiy mpg.setTemplateEngine(new FreemarkerTemplateEngine()); // 全局配置 GlobalConfig gc = new GlobalConfig(); gc.setOutputDir("C://Users/Administrator/Desktop/备份/ssm/src/main/java"); //生成文件的输出目录 gc.setAuthor("xuxu"); //作者 gc.setFileOverride(true); //是否覆蓋已有文件 默认值:false gc.setOpen(false); //是否打开输出目录 默认值:true // gc.setSwagger2(true); //开启 swagger2 模式 默认false gc.setBaseColumnList(true); //开启 baseColumnList 默认false gc.setBaseResultMap(true); //开启 BaseResultMap 默认false gc.setEntityName("%sEntity"); //实体命名方式 默认值:null 例如:%sEntity 生成 UserEntity gc.setMapperName("%sMapper"); //mapper 命名方式 默认值:null 例如:%sDao 生成 UserDao gc.setXmlName("%sMapper"); //Mapper xml 命名方式 默认值:null 例如:%sDao 生成 UserDao.xml gc.setServiceName("%sService"); //service 命名方式 默认值:null 例如:%sBusiness 生成 UserBusiness gc.setServiceImplName("%sServiceImpl"); //service impl 命名方式 默认值:null 例如:%sBusinessImpl 生成 UserBusinessImpl gc.setControllerName("%sController"); //controller 命名方式 默认值:null 例如:%sAction 生成 UserAction mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setDbType(DbType.MYSQL); //数据库类型 该类内置了常用的数据库类型【必须】 dsc.setUrl("jdbc:mysql://localhost:3306/mybatis?useUnicode=true&useSSL=false&characterEncoding=utf8"); // dsc.setSchemaName("public"); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("root"); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); // pc.setModuleName(scanner("模块名")); pc.setParent("com.xuxu.user"); mpg.setPackageInfo(pc); // 自定义配置 // InjectionConfig cfg = new InjectionConfig() { // @Override // public void initMap() { //注入自定义 Map 对象 // // to do nothing // } // }; // List 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)); // 策略配置 数据库表配置,通过该配置,可指定需要生成哪些表或者排除哪些表 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); //表名生成策略 strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略, 未指定按照 naming 执行 // strategy.setCapitalMode(true); // 全局大写命名 ORACLE 注意 // strategy.setTablePrefix("prefix"); //表前缀 // strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity"); //自定义继承的Entity类全称,带包名 // strategy.setSuperEntityColumns(new String[] { "test_id", "age" }); //自定义实体,公共字段 // strategy.setEntityLombokModel(true); //【实体】是否为lombok模型(默认 false strategy.setRestControllerStyle(true); //生成 @RestController 控制器 // strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController"); //自定义继承的Controller类全称,带包名 strategy.setInclude(scanner("表名")); //需要包含的表名,允许正则表达式(与exclude二选一配置) // strategy.setInclude(new String[] { "user" }); // 需要生成的表可以多张表 // strategy.setExclude(new String[]{"test"}); // 排除生成的表 strategy.setControllerMappingHyphenStyle(true); //驼峰转连字符 strategy.setTablePrefix(pc.getModuleName() + "_"); //是否生成实体时,生成字段注解 mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }

BaseMapper接口

Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能

/*
 * Copyright (c) 2011-2020, hubin ([email protected]).
 * 

* Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at *

* http://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.baomidou.mybatisplus.core.mapper; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Constants; import org.apache.ibatis.annotations.Param; import java.io.Serializable; import java.util.Collection; import java.util.List; import java.util.Map; /** *

* Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能 *

*

* 这个 Mapper 支持 id 泛型 *

* * @author hubin * @since 2016-01-23 */ public interface BaseMapper { /** *

* 插入一条记录 *

* * @param entity 实体对象 */ int insert(T entity); /** *

* 根据 ID 删除 *

* * @param id 主键ID */ int deleteById(Serializable id); /** *

* 根据 columnMap 条件,删除记录 *

* * @param columnMap 表字段 map 对象 */ int deleteByMap(@Param(Constants.COLUMN_MAP) Map columnMap); /** *

* 根据 entity 条件,删除记录 *

* * @param queryWrapper 实体对象封装操作类(可以为 null) */ int delete(@Param(Constants.WRAPPER) Wrapper queryWrapper); /** *

* 删除(根据ID 批量删除) *

* * @param idList 主键ID列表(不能为 null 以及 empty) */ int deleteBatchIds(@Param(Constants.COLLECTION) Collection idList); /** *

* 根据 ID 修改 *

* * @param entity 实体对象 */ int updateById(@Param(Constants.ENTITY) T entity); /** *

* 根据 whereEntity 条件,更新记录 *

* * @param entity 实体对象 (set 条件值,不能为 null) * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) */ int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper updateWrapper); /** *

* 根据 ID 查询 *

* * @param id 主键ID */ T selectById(Serializable id); /** *

* 查询(根据ID 批量查询) *

* * @param idList 主键ID列表(不能为 null 以及 empty) */ List selectBatchIds(@Param(Constants.COLLECTION) Collection idList); /** *

* 查询(根据 columnMap 条件) *

* * @param columnMap 表字段 map 对象 */ List selectByMap(@Param(Constants.COLUMN_MAP) Map columnMap); /** *

* 根据 entity 条件,查询一条记录 *

* * @param queryWrapper 实体对象封装操作类(可以为 null) */ T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper); /** *

* 根据 Wrapper 条件,查询总记录数 *

* * @param queryWrapper 实体对象封装操作类(可以为 null) */ Integer selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper); /** *

* 根据 entity 条件,查询全部记录 *

* * @param queryWrapper 实体对象封装操作类(可以为 null) */ List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper); /** *

* 根据 Wrapper 条件,查询全部记录 *

* * @param queryWrapper 实体对象封装操作类(可以为 null) */ List> selectMaps(@Param(Constants.WRAPPER) Wrapper queryWrapper); /** *

* 根据 Wrapper 条件,查询全部记录 * 注意: 只返回第一个字段的值 *

* * @param queryWrapper 实体对象封装操作类(可以为 null) */ List selectObjs(@Param(Constants.WRAPPER) Wrapper queryWrapper); /** *

* 根据 entity 条件,查询全部记录(并翻页) *

* * @param page 分页查询条件(可以为 RowBounds.DEFAULT) * @param queryWrapper 实体对象封装操作类(可以为 null) */ IPage selectPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper); /** *

* 根据 Wrapper 条件,查询全部记录(并翻页) *

* * @param page 分页查询条件 * @param queryWrapper 实体对象封装操作类 */ IPage> selectMapsPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper); }

IService接口

/*
 * Copyright (c) 2011-2016, hubin ([email protected]).
 * 

* Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at *

* http://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.baomidou.mybatisplus.extension.service; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.toolkit.SqlHelper; import java.io.Serializable; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.function.Function; /** *

* 顶级 Service *

* * @author hubin * @since 2018-06-23 */ public interface IService { /** *

* 插入一条记录(选择字段,策略插入) *

* * @param entity 实体对象 */ boolean save(T entity); /** *

* 插入(批量) *

* * @param entityList 实体对象集合 */ default boolean saveBatch(Collection entityList) { return saveBatch(entityList, 1000); } /** *

* 插入(批量) *

* * @param entityList 实体对象集合 * @param batchSize 插入批次数量 */ boolean saveBatch(Collection entityList, int batchSize); /** *

* 批量修改插入 *

* * @param entityList 实体对象集合 */ default boolean saveOrUpdateBatch(Collection entityList) { return saveOrUpdateBatch(entityList, 1000); } /** *

* 批量修改插入 *

* * @param entityList 实体对象集合 * @param batchSize 每次的数量 */ boolean saveOrUpdateBatch(Collection entityList, int batchSize); /** *

* 根据 ID 删除 *

* * @param id 主键ID */ boolean removeById(Serializable id); /** *

* 根据 columnMap 条件,删除记录 *

* * @param columnMap 表字段 map 对象 */ boolean removeByMap(Map columnMap); /** *

* 根据 entity 条件,删除记录 *

* * @param queryWrapper 实体包装类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} */ boolean remove(Wrapper queryWrapper); /** *

* 删除(根据ID 批量删除) *

* * @param idList 主键ID列表 */ boolean removeByIds(Collection idList); /** *

* 根据 ID 选择修改 *

* * @param entity 实体对象 */ boolean updateById(T entity); /** *

* 根据 whereEntity 条件,更新记录 *

* * @param entity 实体对象 * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper} */ boolean update(T entity, Wrapper updateWrapper); /** *

* 根据ID 批量更新 *

* * @param entityList 实体对象集合 */ default boolean updateBatchById(Collection entityList) { return updateBatchById(entityList, 1000); } /** *

* 根据ID 批量更新 *

* * @param entityList 实体对象集合 * @param batchSize 更新批次数量 */ boolean updateBatchById(Collection entityList, int batchSize); /** *

* TableId 注解存在更新记录,否插入一条记录 *

* * @param entity 实体对象 */ boolean saveOrUpdate(T entity); /** *

* 根据 ID 查询 *

* * @param id 主键ID */ T getById(Serializable id); /** *

* 查询(根据ID 批量查询) *

* * @param idList 主键ID列表 */ Collection listByIds(Collection idList); /** *

* 查询(根据 columnMap 条件) *

* * @param columnMap 表字段 map 对象 */ Collection listByMap(Map columnMap); /** *

* 根据 Wrapper,查询一条记录 *

* * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} */ default T getOne(Wrapper queryWrapper) { return getOne(queryWrapper, false); } /** *

* 根据 Wrapper,查询一条记录 *

* * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} * @param throwEx 有多个 result 是否抛出异常 */ T getOne(Wrapper queryWrapper, boolean throwEx); /** *

* 根据 Wrapper,查询一条记录 *

* * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} */ Map getMap(Wrapper queryWrapper); /** *

* 根据 Wrapper,查询一条记录 *

* * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} * @param mapper 转换函数 */ default V getObj(Wrapper queryWrapper, Function mapper) { return SqlHelper.getObject(listObjs(queryWrapper, mapper)); } /** *

* 根据 Wrapper 条件,查询总记录数 *

* * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} */ int count(Wrapper queryWrapper); /** *

* 查询总记录数 *

* * @see Wrappers#emptyWrapper() */ default int count() { return count(Wrappers.emptyWrapper()); } /** *

* 查询列表 *

* * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} */ List list(Wrapper queryWrapper); /** *

* 查询所有 *

* * @see Wrappers#emptyWrapper() */ default List list() { return list(Wrappers.emptyWrapper()); } /** *

* 翻页查询 *

* * @param page 翻页对象 * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} */ IPage page(IPage page, Wrapper queryWrapper); /** *

* 无条件翻页查询 *

* * @param page 翻页对象 * @see Wrappers#emptyWrapper() */ default IPage page(IPage page) { return page(page, Wrappers.emptyWrapper()); } /** *

* 查询列表 *

* * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} */ List> listMaps(Wrapper queryWrapper); /** *

* 查询所有列表 *

* * @see Wrappers#emptyWrapper() */ default List> listMaps() { return listMaps(Wrappers.emptyWrapper()); } /** *

* 查询全部记录 *

*/ default List listObjs() { return listObjs(Function.identity()); } /** *

* 查询全部记录 *

* * @param mapper 转换函数 */ default List listObjs(Function mapper) { return listObjs(Wrappers.emptyWrapper(), mapper); } /** *

* 根据 Wrapper 条件,查询全部记录 *

* * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} */ default List listObjs(Wrapper queryWrapper) { return listObjs(Wrappers.emptyWrapper(), Function.identity()); } /** *

* 根据 Wrapper 条件,查询全部记录 *

* * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} * @param mapper 转换函数 */ List listObjs(Wrapper queryWrapper, Function mapper); /** *

* 翻页查询 *

* * @param page 翻页对象 * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} */ IPage> pageMaps(IPage page, Wrapper queryWrapper); /** *

* 无条件翻页查询 *

* * @param page 翻页对象 * @see Wrappers#emptyWrapper() */ default IPage> pageMaps(IPage page) { return pageMaps(page, Wrappers.emptyWrapper()); } }

 

 

Mapper CRUD 接口 官方说明文档

说明:

  • 通用 CRUD 封装BaseMapper接口,为 Mybatis-Plus 启动时自动解析实体表关系映射转换为 Mybatis 内部对象注入容器
  • 泛型 T 为任意实体对象
  • 参数 Serializable 为任意类型主键 Mybatis-Plus 不推荐使用复合主键约定每一张表都有自己的唯一 id 主键
  • 对象 Wrapper 为 条件构造器

#insert

/**
 * 

* 插入一条记录 *

* * @param entity 实体对象 * @return 插入成功记录数 */ int insert(T entity);

#deleteById

/**
 * 

* 根据 ID 删除 *

* * @param id 主键ID * @return 删除成功记录数 */ int deleteById(Serializable id);

#deleteByMap

/**
 * 

* 根据 columnMap 条件,删除记录 *

* * @param columnMap 表字段 map 对象 * @return 删除成功记录数 */ int deleteByMap(@Param(Constants.COLUMN_MAP) Map columnMap);

#delete

/**
 * 

* 根据 entity 条件,删除记录 *

* * @param queryWrapper 实体对象封装操作类(可以为 null) * @return 删除成功记录数 */ int delete(@Param(Constants.WRAPPER) Wrapper queryWrapper);

#deleteBatchIds

/**
 * 

* 删除(根据ID 批量删除) *

* * @param idList 主键ID列表(不能为 null 以及 empty) * @return 删除成功记录数 */ int deleteBatchIds(@Param(Constants.COLLECTION) Collection idList);

#updateById

/**
 * 

* 根据 ID 修改 *

* * @param entity 实体对象 * @return 修改成功记录数 */ int updateById(@Param(Constants.ENTITY) T entity);

#update

/**
 * 

* 根据 whereEntity 条件,更新记录 *

* * @param entity 实体对象 (set 条件值,不能为 null) * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) * @return 修改成功记录数 */ int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper updateWrapper);

#selectById

/**
 * 

* 根据 ID 查询 *

* * @param id 主键ID * @return 实体 */ T selectById(Serializable id);

#selectBatchIds

/**
 * 

* 查询(根据ID 批量查询) *

* * @param idList 主键ID列表(不能为 null 以及 empty) * @return 实体集合 */ List selectBatchIds(@Param(Constants.COLLECTION) Collection idList);

#selectByMap

/**
 * 

* 查询(根据 columnMap 条件) *

* * @param columnMap 表字段 map 对象 * @return 实体集合 */ List selectByMap(@Param(Constants.COLUMN_MAP) Map columnMap);

#selectOne

/**
 * 

* 根据 entity 条件,查询一条记录 *

* * @param queryWrapper 实体对象 * @return 实体 */ T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper);

#selectCount

/**
 * 

* 根据 Wrapper 条件,查询总记录数 *

* * @param queryWrapper 实体对象 * @return 满足条件记录数 */ Integer selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper);

#selectList

/**
 * 

* 根据 entity 条件,查询全部记录 *

* * @param queryWrapper 实体对象封装操作类(可以为 null) * @return 实体集合 */ List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper);

#selectMaps

/**
 * 

* 根据 Wrapper 条件,查询全部记录 *

* * @param queryWrapper 实体对象封装操作类(可以为 null) * @return 字段映射对象 Map 集合 */ List> selectMaps(@Param(Constants.WRAPPER) Wrapper queryWrapper);

#selectObjs

/**
 * 

* 根据 Wrapper 条件,查询全部记录 * 注意: 只返回第一个字段的值 *

* * @param queryWrapper 实体对象封装操作类(可以为 null) * @return 字段映射对象集合 */ List selectObjs(@Param(Constants.WRAPPER) Wrapper queryWrapper);

#selectPage

/**
 * 

* 根据 entity 条件,查询全部记录(并翻页) *

* * @param page 分页查询条件(可以为 RowBounds.DEFAULT) * @param queryWrapper 实体对象封装操作类(可以为 null) * @return 实体分页对象 */ IPage selectPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper);

#selectMapsPage

/**
 * 

* 根据 Wrapper 条件,查询全部记录(并翻页) *

* * @param page 分页查询条件 * @param queryWrapper 实体对象封装操作类 * @return 字段映射对象 Map 分页对象 */ IPage> selectMapsPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper);

#Service CRUD 接口

说明:

  • 通用 Service CRUD 封装IService接口,进一步封装 CRUD 采用 get 查询单行 remove 删除list 查询集合 page 分页 前缀命名方式区分 Mapper 层避免混淆,
  • 泛型 T 为任意实体对象
  • 建议如果存在自定义通用 Service 方法的可能,请创建自己的 IBaseService 继承 Mybatis-Plus 提供的基类
  • 对象 Wrapper 为 条件构造器

#save

/**
 * 

* 插入一条记录(选择字段,策略插入) *

* * @param entity 实体对象 */ boolean save(T entity);

#saveBatch

/**
 * 

* 插入一条记录(选择字段,策略插入) *

* * @param entity 实体对象 */ boolean save(T entity);

#saveOrUpdateBatch

/**
 * 

* 批量修改插入 *

* * @param entityList 实体对象集合 */ boolean saveOrUpdateBatch(Collection entityList);

#saveOrUpdateBatch

/**
 * 

* 批量修改插入 *

* * @param entityList 实体对象集合 * @param batchSize 每次的数量 */ boolean saveOrUpdateBatch(Collection entityList, int batchSize);

#removeById

/**
 * 

* 根据 ID 删除 *

* * @param id 主键ID */ boolean removeById(Serializable id);

#removeByMap

/**
 * 

* 根据 columnMap 条件,删除记录 *

* * @param columnMap 表字段 map 对象 */ boolean removeByMap(Map columnMap);

#remove

/**
 * 

* 根据 entity 条件,删除记录 *

* * @param queryWrapper 实体包装类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} */ boolean remove(Wrapper queryWrapper);

#removeByIds

/**
 * 

* 删除(根据ID 批量删除) *

* * @param idList 主键ID列表 */ boolean removeByIds(Collection idList);

#updateById

/**
 * 

* 根据 ID 选择修改 *

* * @param entity 实体对象 */ boolean updateById(T entity);

#update

/**
 * 

* 根据 whereEntity 条件,更新记录 *

* * @param entity 实体对象 * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper} */ boolean update(T entity, Wrapper updateWrapper);

#updateBatchById

/**
 * 

* 根据ID 批量更新 *

* * @param entityList 实体对象集合 * @param batchSize 更新批次数量 */ boolean updateBatchById(Collection entityList, int batchSize);

#saveOrUpdate

/**
 * 

* TableId 注解存在更新记录,否插入一条记录 *

* * @param entity 实体对象 */ boolean saveOrUpdate(T entity);

#getById

/**
 * 

* 根据 ID 查询 *

* * @param id 主键ID */ T getById(Serializable id);

#listByIds

/**
 * 

* 查询(根据ID 批量查询) *

* * @param idList 主键ID列表 */ Collection listByIds(Collection idList);

#listByMap

/**
 * 

* 查询(根据 columnMap 条件) *

* * @param columnMap 表字段 map 对象 */ Collection listByMap(Map columnMap);

#getOne

/**
 * 

* 根据 Wrapper,查询一条记录 *

* * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} * @param throwEx 有多个 result 是否抛出异常 */ T getOne(Wrapper queryWrapper, boolean throwEx);

#getMap

/**
 * 

* 根据 Wrapper,查询一条记录 *

* * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} */ Map getMap(Wrapper queryWrapper);

#getObj

/**
 * 

* 根据 Wrapper,查询一条记录 *

* * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} */ Object getObj(Wrapper queryWrapper);

#count

/**
 * 

* 根据 Wrapper 条件,查询总记录数 *

* * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} */ int count(Wrapper queryWrapper);

#list

/**
 * 

* 查询列表 *

* * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} */ List list(Wrapper queryWrapper);

#page

/**
 * 

* 翻页查询 *

* * @param page 翻页对象 * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} */ IPage page(IPage page, Wrapper queryWrapper);

#listMaps

/**
 * 

* 查询列表 *

* * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} */ List> listMaps(Wrapper queryWrapper);

#listObjs

/**
 * 

* 根据 Wrapper 条件,查询全部记录 *

* * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} */ List listObjs(Wrapper queryWrapper);

#pageMaps

/**
 * 

* 翻页查询 *

* * @param page 翻页对象 * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper} */ IPage> pageMaps(IPage page, Wrapper queryWrapper);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(Mybatis)