资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

MybatisPlus的基础使用方法

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

南丰ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!

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/")
      @Validated
      public class AdminForumController {
      
      }
      
      
      @Service
      public class ForumServiceImpl extends ServiceImpl implements ForumService {
      
      
      }
      
      
      @Mapper
      public 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的基础使用方法

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


本文题目:MybatisPlus的基础使用方法
网页链接:http://cdkjz.cn/article/jsipji.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220