微服務解決方案 -- 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");
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章