千家信息网

Mybatis Plus的基础使用方法

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章主要介绍"Mybatis Plus的基础使用方法",在日常操作中,相信很多人在Mybatis Plus的基础使用方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答
千家信息网最后更新 2026年11月02日Mybatis Plus的基础使用方法

这篇文章主要介绍"Mybatis Plus的基础使用方法",在日常操作中,相信很多人在Mybatis Plus的基础使用方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Mybatis Plus的基础使用方法"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

Mybatis-Plus 是一款 Mybatis 动态 SQL 自动注入 Mybatis 增删改查 CRUD 操作中间件, 减少你的开发周期优化动态维护 XML 实体字段,无入侵全方位 ORM 辅助层让您拥有更多时间陪家人。

以下内容 以Mybatis-Plus 3.0.1版本 为蓝本;

详情见官方文档:

https://mp.baomidou.com/guide/#%E7%89%B9%E6%80%A7

springboot2.0 集成 mybatis-plus

  1. pom引入所需jar包

    1.                     com.baomidou            mybatis-plus-boot-starter            3.1.0                            com.baomidou            mybatis-plus-extension            3.1.0                                    com.baomidou            mybatis-plus-generator            3.1.0                                    org.freemarker            freemarker        


  2. 配置自动生成工具类

    1. package org.xx.xx.db.util;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.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.util.ArrayList;import java.util.List;/** * @Description: * @Auther: wuxw * @Date: 2019/9/30 14:27 */public class CodeGeneratorUtil {    public static void main(String[] args) {        //代码生成器        AutoGenerator mpg = new AutoGenerator();        //全局配置        GlobalConfig gc = new GlobalConfig();        String projectPath = System.getProperty("user.dir") + "/litemall-db/";        gc.setOutputDir(projectPath + "/src/main/java");        gc.setAuthor("wuxw");        gc.setServiceName("%sService");//自定义Service接口生成的文件名        gc.setOpen(false);        gc.setBaseResultMap(true);        gc.setDateType(DateType.ONLY_DATE);        mpg.setGlobalConfig(gc);        //数据源配置        DataSourceConfig dsc = new DataSourceConfig();        dsc.setUrl("jdbc:mysql://127.0.0.1:3306/litemall?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8");        dsc.setDriverName("com.mysql.cj.jdbc.Driver");        dsc.setUsername("root");        dsc.setPassword("123456");        mpg.setDataSource(dsc);        //包配置        PackageConfig pc = new PackageConfig();        pc.setParent("org.xxx.xxx.db")                .setMapper("dao");        mpg.setPackageInfo(pc);        //自定义配置        InjectionConfig cfg = new InjectionConfig() {            @Override            public void initMap() {                //to do nothing            }        };        //自定义输出配置        List focList = new ArrayList<>();        //自定义配置会优先输出        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {            @Override            public String outputFile(TableInfo tableInfo) {                // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!                return projectPath + "/src/main/resources/mappers/"                        + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;            }        });        cfg.setFileOutConfigList(focList);        mpg.setCfg(cfg);        // 配置模板        TemplateConfig templateConfig = new TemplateConfig();        // 配置自定义输出模板        //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别        // templateConfig.setEntity("templates/entity2.java");        // templateConfig.setService();        // templateConfig.setController();        templateConfig.setXml(null);        mpg.setTemplate(templateConfig);        //配置策略        StrategyConfig strategy = new StrategyConfig();        strategy.setNaming(NamingStrategy.underline_to_camel);        strategy.setColumnNaming(NamingStrategy.underline_to_camel);        //strategy.setSuperControllerClass("com.example.demo.model.BaseEntity");        strategy.setEntityLombokModel(false);//默认是false        //strategy.setRestControllerStyle(true);        //公共父类        //strategy.setSuperControllerClass("com.example.demo.controller.BaseController");        // 写于父类中的公共字段        //strategy.setSuperEntityColumns("id");        strategy.setInclude("tb_forum_replay"); // 仅生成单个表        strategy.setControllerMappingHyphenStyle(true);        strategy.setTablePrefix("tb_");        mpg.setStrategy(strategy);        mpg.setTemplateEngine(new FreemarkerTemplateEngine());        mpg.execute();        System.out.println(" --------------------------自动生成完毕------------------------");    }}


  3. 实际开发

    1. @Api(tags = "论坛主页")@RestController@RequestMapping("/admin/forum/")@Validatedpublic class AdminForumController {}@Servicepublic class ForumServiceImpl extends ServiceImpl implements ForumService {}@Mapperpublic interface ForumMapper extends BaseMapper {}@Data@TableName("tb_forum")public class Forum implements Serializable {}


实际开发使用

Select

第一种 selectCount

QueryWrapper qw = new QueryWrapper();qw.eq("user_id",userId);qw.eq("readed",0);baseMapper.selectCount(qw);

等同于

select count(*) from tb where use_id = #{userId} and readed =0

第二种selectOne

QueryWrapper qw = new QueryWrapper();qw.eq("user_id",userId);qw.eq("readed",0);qw.last("limit 1");baseMapper.selectOne(qw);

等同于

select count(*) from tb where use_id = #{userId} and readed =0 limit 1

update

第一种 set

UpdateWrapper uw = new UpdateWrapper();uw.eq("user_id",userId);uw.eq("id",id);Forum f = new Forum();f.setDeleted(1);return forumMapper.update(f,uw) > 0;

等同于

update forum set delete =1 where user_id = #{userId} and id = #{id}

第二种 insql

UpdateWrapper uw = new UpdateWrapper();String[] idsStr =new String["1","2","3"];String id =  StringUtils.strip(idsStr.toString(),"[]");uw.inSql("id",id);Forum f = new Forum();f.setDeleted(1);return forumMapper.update(f,uw) > 0;

等同于

update forum set deleted = 1 where id in ( 1 , 2 ,3)

太太太哪里, 具体还是看官方文档吧

条件构造器

各种sql语义,让你用mybatisPlus 溜的飞起

allEq

eq

ne

...

最最最主要的还是

MybatisX 快速开发插件

  • Java 与 XML 调回跳转

  • Mapper 方法自动生成 XML

到此,关于"Mybatis Plus的基础使用方法"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

配置 方法 生成 使用方法 基础 模板 学习 开发 输出 更多 自动生成 动态 字段 官方 实际 文件 文件名 文档 跟着 还是 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 哈尔滨大商圈互联网科技有限公司 没上市的科技互联网公司 互联网金融 金融科技 西安互联网科技 杭州互联网科技公司 中国互联网科技发展 宜安科技 互联网金融 深圳天众互联网科技公司 互联网科技实习生岗位职责 河南猎农互联网科技有限公司 互联网科技大奖都有哪些公司 内蒙古九一互联网科技 济南有互联网科技公司吗 上海钢联属于互联网科技股吗 互联网科技数字货币 互联网科技包装股份有限公司上市 广州燕处互联网科技有限公司 宁波跨境商互联网科技有限公司 福建互联网展示科技馆设计 青岛易易互联网科技 互联网科技工作委员会副主任毛军 互联网扶贫科技 嘟嘟瓜互联网科技 上海万想互联网科技 华服互联网科技官网 互联网软件和科技 21世纪互联网科技成就 互联网科技包装股份有限公司上市 长沙渺峰互联网科技有限公司 北京互联网科技类企业分布
0