欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot中引入MyBatisPlus的常規(guī)操作

 更新時(shí)間:2020年11月02日 11:43:44   作者:IT小村  
這篇文章主要介紹了SpringBoot中引入MyBatisPlus的常規(guī)操作,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

一、前言

近來(lái)參與一個(gè)電力大數(shù)據(jù)項(xiàng)目,開(kāi)發(fā)小組決定在 DAO 層使用 MyBatisPlus
——國(guó)產(chǎn)、新穎、強(qiáng)大、輕量。

這里寫圖片描述 

官方API地址:http://mp.baomidou.com/#/?id=%E7%AE%80%E4%BB%8B

二、通用 CRUD

通過(guò)本項(xiàng)目(表少,數(shù)據(jù)量大,非常適合)
發(fā)現(xiàn) MyBatisPlus 在 單表CRUD 方面
比原來(lái)的的 MyBatis 的有著絕對(duì)優(yōu)勢(shì):

VS PS MyBatis MyBatisPlus
代碼生成器 數(shù)據(jù)庫(kù)有些表的字段發(fā)聲改變 ① 再次運(yùn)行逆向工程的代碼 ②生成一堆代碼,還最好不要?jiǎng)铀?/td> ①只需要修改 Bean 即可(反射實(shí)現(xiàn)的)② 生成少量的代碼,可以人為新增內(nèi)容
CRUD 無(wú)規(guī)律特殊時(shí) 手動(dòng)寫 Mapper 層 sql 通過(guò)條件構(gòu)造器,實(shí)現(xiàn)零 sql !

下面列出本文相關(guān)代碼,其他代碼如 Druid數(shù)據(jù)源配置、MyBatisPlus分頁(yè)配置、sql輸出配置,可以到 github 上查看:
https://github.com/larger5/SpringBoot_MybatisPlus.git

這里寫圖片描述

package com.cun.plus;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cun.plus.entity.User;
import com.cun.plus.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class PlusApplicationTests {

  @Autowired
  private UserMapper userMapper;

  /**
   * 1、增加 insert
   */
  @Test
  public void insertTest() {
    User user = new User();
    user.setUsername("綠茶");
    user.setPassword("lvcha");
    // 返回對(duì)數(shù)據(jù)庫(kù)影響操作數(shù):1
    Integer insert1 = userMapper.insert(user); // 非空屬性才會(huì)出現(xiàn)在 sql 中
    // 可以立刻獲取插入新記錄的 id
    System.out.println(user.getId());
    // 同上
    Integer insert2 = userMapper.insertAllColumn(user); // 所有屬性都會(huì)出現(xiàn)在 sql 中
    // 同上
    System.out.println(user.getId());
  }

  /**
   * 2、修改 update
   */
  @Test
  public void updateTest() {
    User user = new User();
    user.setId(45);
    user.setUsername("cun");
    //user.setPassword("666");
    // 返回對(duì)數(shù)據(jù)庫(kù)影響操作數(shù):1
    Integer integer1 = userMapper.updateById(user); // 屬性空則不修改
    System.out.println(integer1);
    // 同上
    Integer integer2 = userMapper.updateAllColumnById(user); // 屬性空則字段空
    System.out.println(integer2);
  }

  /**
   * 3、查詢 select
   */
  @Test
  public void selectTest() {
    // 根據(jù)id 查詢一條記錄
    User user = userMapper.selectById(46);
    System.out.println(user.getUsername());

    ArrayList<Integer> idList = new ArrayList<>();
    idList.add(61);
    idList.add(63);
    // 根據(jù)多個(gè)id 批量查詢
    List<User> users = userMapper.selectBatchIds(idList);
    System.out.println(users.get(0).getUsername() + users.get(1).getUsername());

    User user1 = new User();
    user1.setId(61);
    user1.setUsername("cun");
    // 根據(jù)多個(gè)條件返回一個(gè)對(duì)象,若有多個(gè)符合條件的記錄則將報(bào)錯(cuò)
    User user2 = userMapper.selectOne(user1);
    System.out.println(user2.getPassword());

    // 根據(jù)多個(gè)條件返回對(duì)象組,注意Map 中的key 必須和數(shù)據(jù)庫(kù)表字段一直
    HashMap<String, Object> columnMap = new HashMap<>();
    columnMap.put("username", "cun");
    List<User> users1 = userMapper.selectByMap(columnMap);
    System.out.println(users1.size());


  }

  /**
   * 4、刪除 delete
   */
  @Test
  public void deleteTest() {
    // 根據(jù)一個(gè)id 刪除,返回對(duì)數(shù)據(jù)庫(kù)影響操作數(shù):1
    Integer integer1 = userMapper.deleteById(65);// 根據(jù)id刪除一條記錄
    System.out.println(integer1);

    ArrayList<Integer> idList = new ArrayList<>(); // 根據(jù)id集合批量刪除
    idList.add(64);
    idList.add(66);
    // 根據(jù)多個(gè)id 批量刪除,返回對(duì)數(shù)據(jù)庫(kù)影響操作數(shù):2
    Integer integer2 = userMapper.deleteBatchIds(idList);
    System.out.println(integer2);

    HashMap<String, Object> columnMap = new HashMap<>();
    columnMap.put("username", "cun");
    // 根據(jù)多個(gè)條件刪除,返回對(duì)數(shù)據(jù)庫(kù)影響操作數(shù)
    Integer integer3 = userMapper.deleteByMap(columnMap);
    System.out.println(integer3);
  }

  /**
   * 5、偽分頁(yè)(獲取全部數(shù)據(jù)再分頁(yè))
   */
  @Test
  public void pageTest() {
    // 分頁(yè)查詢,注意如果設(shè)置了 PaginationInterceptor 分頁(yè)插件則會(huì)報(bào)錯(cuò),
    List<User> users2 = userMapper.selectPage(new Page<User>(1, 2), null); //當(dāng)前頁(yè)、每頁(yè)大小
    System.out.println(users2.get(0).getUsername() + users2.get(1).getUsername());
    System.out.println(users2.size());
  }


  /**
   * 6、條件構(gòu)造器
   */
  @Test
  public void wrapperTest(){
    List<User> users = userMapper.selectList(new EntityWrapper<User>()
        .eq("username", "linhongcun")
    );
    System.out.println(users.size());
  }

}

三、代碼生成器

上述的代碼通過(guò) MyBatisPlsus 自動(dòng)寫好的通用 Mapper 層,在 Service 層里邊寫相關(guān)的業(yè)務(wù)邏輯
其實(shí),使用了 MyBatisPlus 代碼生成器,自動(dòng)生成 Entity、Dao、Service、Controller 層!
我們通常是在 Controller 層里邊寫相關(guān)的業(yè)務(wù)邏輯,使用的方法和 Mapper 的類似。

package com.cun.plus;

import com.baomidou.mybatisplus.enums.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class MpG {
  public static void main(String[] args) {
    //1. 全局配置
    GlobalConfig config = new GlobalConfig();
    config.setActiveRecord(false) // 是否支持AR模式
        .setAuthor("linhongcun") // 作者
        .setOutputDir("C:\\data\\mp") // 生成路徑
        .setFileOverride(true) // 文件覆蓋
        .setIdType(IdType.AUTO) // 主鍵策略
        .setServiceName("%sService") // 設(shè)置生成的service接口的名字的首字母是否為I
        // IUserService
        .setBaseResultMap(true)
        .setBaseColumnList(true);

    //2. 數(shù)據(jù)源配置
    DataSourceConfig dsConfig = new DataSourceConfig();
    dsConfig.setDbType(DbType.MYSQL) // 設(shè)置數(shù)據(jù)庫(kù)類型
        .setDriverName("com.mysql.jdbc.Driver")
        .setUrl("jdbc:mysql://120.79.197.130:3307/testspring?useSSL=true&verifyServerCertificate=false&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8")
        .setUsername("root")
        .setPassword("123");

    //3. 策略配置
    StrategyConfig stConfig = new StrategyConfig();
    stConfig.setCapitalMode(true) //全局大寫命名
        .setDbColumnUnderline(true) // 指定表名 字段名是否使用下劃線
        .setNaming(NamingStrategy.underline_to_camel) // 數(shù)據(jù)庫(kù)表映射到實(shí)體的命名策略
        .setTablePrefix("tb_")
        .setInclude("tb_user"); // 生成的表

    //4. 包名策略配置
    PackageConfig pkConfig = new PackageConfig();
    pkConfig.setParent("com.cun.plus")
        .setMapper("mapper")
        .setService("service")
        .setController("controller")
        .setEntity("entity")
        .setXml("mapper");

    //5. 整合配置
    AutoGenerator ag = new AutoGenerator();
    ag.setGlobalConfig(config)
        .setDataSource(dsConfig)
        .setStrategy(stConfig)
        .setPackageInfo(pkConfig);

    //6. 執(zhí)行
    ag.execute();
  }
}

四、相關(guān)依賴

// 選擇 freemarker 引擎,默認(rèn) Veloctiy
  // mpg.setTemplateEngine(new FreemarkerTemplateEngine());
 <!-- mybatis-plus-->
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>2.3</version>
    </dependency>
    <!-- 代碼生成器默認(rèn)使用如下模版引擎 -->
    <dependency>
      <groupId>org.apache.velocity</groupId>
      <artifactId>velocity-engine-core</artifactId>
      <version>2.0</version>
    </dependency>

五、其他

使用 MyBatis 的一個(gè)弊端,就是得依靠使用代碼生成器,使得邏輯基本寫在 controller 層,而不是 service 層,不合時(shí)宜。

多表+分頁(yè),詳見(jiàn):

最簡(jiǎn)單的 MyBatis Plus 的多表聯(lián)接、分頁(yè)查詢實(shí)現(xiàn)方法

到此這篇關(guān)于SpringBoot中引入MyBatisPlus的常規(guī)操作的文章就介紹到這了,更多相關(guān)SpringBoot引入MyBatisPlus內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解SpringBoot和Mybatis配置多數(shù)據(jù)源

    詳解SpringBoot和Mybatis配置多數(shù)據(jù)源

    本篇文章主要介紹了詳解SpringBoot和Mybatis配置多數(shù)據(jù)源,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05
  • SpringCloud入門實(shí)驗(yàn)環(huán)境搭建

    SpringCloud入門實(shí)驗(yàn)環(huán)境搭建

    這篇文章主要介紹了SpringCloud入門實(shí)驗(yàn)環(huán)境搭建的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用SpringCloud,感興趣的朋友可以了解下
    2021-04-04
  • Java實(shí)戰(zhàn)之醫(yī)院管理系統(tǒng)的實(shí)現(xiàn)

    Java實(shí)戰(zhàn)之醫(yī)院管理系統(tǒng)的實(shí)現(xiàn)

    這篇文章主要介紹了如何利用Java實(shí)現(xiàn)醫(yī)院管理系統(tǒng),文中用到的技術(shù)有:SpringBoot、Layui、Freemaker等,感興趣的同學(xué)可以了解一下
    2022-04-04
  • 一文詳解Java攔截器與過(guò)濾器的使用

    一文詳解Java攔截器與過(guò)濾器的使用

    這篇文章主要為大家詳細(xì)介紹了Java中攔截器與過(guò)濾器的使用與區(qū)別,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定參考價(jià)值,需要的可以參考一下
    2022-04-04
  • Java多線程——基礎(chǔ)概念

    Java多線程——基礎(chǔ)概念

    這篇文章主要介紹了java多線程編程實(shí)例,分享了幾則多線程的實(shí)例代碼,具有一定參考價(jià)值,加深多線程編程的理解還是很有幫助的,需要的朋友可以參考下,希望可以幫到你
    2021-07-07
  • SpringCloud中的Eureka集群配置方法

    SpringCloud中的Eureka集群配置方法

    這篇文章主要介紹了SpringCloud中的Eureka集群配置,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-09-09
  • SpringBoot項(xiàng)目的多文件兼多線程上傳下載

    SpringBoot項(xiàng)目的多文件兼多線程上傳下載

    本文主要介紹了SpringBoot項(xiàng)目的多文件兼多線程上傳下載,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Java數(shù)組擴(kuò)容實(shí)例代碼

    Java數(shù)組擴(kuò)容實(shí)例代碼

    這篇文章主要介紹了Java數(shù)組擴(kuò)容實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2017-11-11
  • java文件讀寫工具類分享

    java文件讀寫工具類分享

    這篇文章主要為大家詳細(xì)介紹了java文件讀寫工具類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • Spring Boot 啟動(dòng)、停止、重啟、狀態(tài)腳本

    Spring Boot 啟動(dòng)、停止、重啟、狀態(tài)腳本

    今天給大家分享Spring Boot 項(xiàng)目腳本(啟動(dòng)、停止、重啟、狀態(tài)),通過(guò)示例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2021-06-06

最新評(píng)論