微服务解决方案 -- 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

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

@TableId

属性 类型 必须指定 默认值 描述 备注
value String “” 主键字段名
type Enum IdType.NONE 主键类型

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

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

@TableField

属性 类型 必须指定 默认值 描述
value String “” 数据库字段名
exist boolean true 是否为数据库表字段

特别说明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");
  }
}

猜你喜欢

转载自blog.csdn.net/weixin_42126468/article/details/106889290