微服务解决方案 -- MyBatis-Plus 效率至上,为简化开发而生

MyBatis-Plus


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

1. 简介


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

愿景

我们的愿景是成为 MyBatis 最好的搭档,就像 魂斗罗 中的 1P、2P,基友搭配,效率翻倍。

依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus</artifactId>
    <version>3.3.2</version>
</dependency>

2. 快速入门


只需要引入mybatis-plus-boot-starter,即可。

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.2.0</version>
</dependency>

在启动类添加MapperScan,写上对应的包名。

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
// mapper层
@MapperScan("com.laoshiren.hello.mybatis.plus.mapper")
public class HelloMyBatisPlusApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloMyBatisPlusApplication.class,args);
    }
}

编写配置文件

spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/fly?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 12345678
    hikari:
      minimum-idle: 1
      idle-timeout: 600000
      maximum-pool-size: 5
      auto-commit: true
      pool-name: MyHikariCP
      max-lifetime: 1800000
      connection-timeout: 30000
      connection-test-query: SELECT 1

mapper 层继承 com.baomidou.mybatisplus.core.mapper.BaseMapper

3. 注解


@TableName

属性类型必须指定默认值备注
valueString“”最好指定表名
schemaString“”
@TableName(value = "tb_post")

@TableId

属性类型必须指定默认值描述备注
valueString“”主键字段名
typeEnumIdType.NONE主键类型

像我本人喜欢用UUID去做主键,所以我IdType会使用IdType.INPUT

@TableId(value = "post_guid", type = IdType.INPUT)

@TableField

属性类型必须指定默认值描述
valueString“”数据库字段名
existbooleantrue是否为数据库表字段

特别说明exist,我在开发过程中经常发现可能需要多一个字段去存前端发送给我的值,而我以前的做法是,在我的业务层里去继承我的实体类做成一个vo对象。现在通过这个可以直接在原来生成的实体类去加属性。这个还是比较好的。

4. CRUD


CRUD很简单调用对应的方法就可以了。

条件查询

QueryWapper相当于一个条件构造器,将对应的条件一个个传入就行了。

QueryWrapper<TbPost> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("post_status",1)
  					.eq("author","laoshiren1207")
  					// likeRight("name", "王")--->name like '王%'
  					.likeRight("tags","mybatis")

相对于tk.mybatisMP感觉更加简单易上手。

贴一段tk.mybatis的条件查询。

Example example = new Example(TbNews.class);
        example.createCriteria()
                .andLike("newsTitle","%"+keywords+"%");
        example.setOrderByClause("news_Addtime desc");

5. 分页查询

关于分页查询,我一直使用的是pagehelper-spring-boot-starter,所以MP的分页插件我也就不使用了。

<dependency>
  <groupId>com.github.pagehelper</groupId>
  <artifactId>pagehelper-spring-boot-starter</artifactId>
  <exclusions>
      <exclusion>
          <groupId>org.mybatis.spring.boot</groupId>
          <artifactId>mybatis-spring-boot-starter</artifactId>
      </exclusion>
  </exclusions>
</dependency>

pageHelper使用可以自行百度。

6. 多数据源

这个也是让我心动的原因,以后的开发打算放弃tk.mybatis了。首先引入如下依赖。

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

配置文件修改。

spring:
  datasource:
    dynamic:
      primary: master #设置默认的数据源或者数据源组,默认值即为master
      strict: false #设置严格模式,默认false不启动. 启动后在未匹配到指定数据源时候回抛出异常,不启动会使用默认数据源.
      datasource:
        master:
          url: jdbc:mysql://xx.xx.xx.xx:3306/dynamic
          username: root
          password: 12345678
          driver-class-name: com.mysql.jdbc.Driver
        slave_1:
          url: jdbc:mysql://xx.xx.xx.xx:3307/dynamic
          username: root
          password: 12345678
          driver-class-name: com.mysql.jdbc.Driver

只需要在方法或者类上添加一个注解@DS("xxx")就可以优雅地切换数据源。

@Service
@DS("slave")
public class UserServiceImpl implements UserService {

  @Autowired
  private JdbcTemplate jdbcTemplate;

  public List<Map<String, Object>> selectAll() {
    return  jdbcTemplate.queryForList("select * from user");
  }
  
  @Override
  @DS("slave_1")
  public List<Map<String, Object>> selectByCondition() {
    return  jdbcTemplate.queryForList("select * from user where age >10");
  }
}
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率。 愿景我们的愿景是成为 MyBatis 超好的搭档,就像 魂斗罗 中的 1P、2P,基友搭配,效率翻倍。 特性无侵入:只做增强不做改变,引入它不会对现有工程产影响,如丝般顺滑损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错支持主键自动成:支持多达 4 种主键策略(内含分布式唯一 ID 成器 - Sequence),可自由配置,完美解决主键问题支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )内置代码成器:采用代码或者 Maven 插件可快速成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库内置性能分析插件:可输出 Sql 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作 我们将通过理论与实操的方式来阐述 MyBatis-Plus 的强大功能,体验和学习MyBatis-Plus技术。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值