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

Mybatis-Plus3.x的創(chuàng)建步驟及使用教程

 更新時(shí)間:2023年10月16日 09:50:59   作者:愚人釗呀  
MyBatis-Plus是一個(gè)?MyBatis?的增強(qiáng)工具,在?MyBatis?的基礎(chǔ)上只做增強(qiáng)不做改變,為?簡化開發(fā)、提高效率而生,這篇文章主要介紹了Mybatis-Plus3.x的使用,需要的朋友可以參考下

MyBatis-Plus(簡稱 MP)是一個(gè) MyBatis 的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為 簡化開發(fā)、提高效率而生。

一、引入

創(chuàng)建步驟:

1.創(chuàng)建Spring Boot工程

2.添加依賴

引入 Spring Boot Starter 父工程:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.7.RELEASE</version>
    <relativePath/>
</parent>

引入相關(guān)其他依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.3</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--lombok-->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

mybatis-plus相關(guān)依賴

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.0</version>
</dependency>
<!-- 引入mysql依賴 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

配置application.yml:

添加mysql相關(guān)配置

mybatis-plus:
  type-aliases-package: com.hz.entity #類型別名所在的包
  #控制臺打印sql語句
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: false #駝峰映射
  global-config:
    db-config:
      logic-delete-field: delFlag  #全局邏輯刪除字段值 3.3.0開始支持,詳情看下面。
      logic-delete-value: 1        #邏輯已刪除值(默認(rèn)為 1)
      logic-not-delete-value: 0    #邏輯未刪除值(默認(rèn)為 0)
#數(shù)據(jù)庫鏈接
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
    username: root
    driver-class-name: com.mysql.cj.jdbc.Driver
    password: 123456
#靜態(tài)資源
  resources:
    static-locations: classpath:/templates,classpath:/static/

在 Spring Boot 啟動類中添加 @MapperScan 注解

編碼:

mapper類中繼承Basemapper

測試: 

注意:

1.數(shù)據(jù)庫字段若為駝峰命名,則需要開啟

mybatis-plus:configuration:map-underscoreto-camel-case: false #駝峰映射

2. UserMapper 中的 selectList() 方法的參數(shù)為 MP 內(nèi)置的條件封裝器 Wrapper ,所以 不填寫就是無任何條件

3.若需要自定義DAO接口,則需要在yml中讀取mapper文件,

mybatis-plus:mapperlocations: classpath:mappers/*.xml

二、通用crud接口

        CRUD是指在做計(jì)算處理時(shí)的增加(Create)、檢索(Retrieve)、更新(Update)和刪除(Delete)幾個(gè)單詞的首字母簡寫。CRUD主要被用在描述軟件系統(tǒng)中數(shù)據(jù)庫或者持久層的基本操作功能。 

service層

        如 UserService 用戶業(yè)務(wù)接口有自定義的方法,可創(chuàng)建 UserService 并繼承 IService ,接口實(shí)現(xiàn)類 UserServiceImpl 可繼承 ServiceImpl<BaseMapper<t>, T> ,實(shí)現(xiàn) UserService 接口并 實(shí)現(xiàn)接口定義方法。具體代碼如下:

繼承 IService 接口

三、分頁插件:

1.分頁插件

配置插件

//Spring boot方式
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {
    /**
    * 分頁插件定義
    * @return
    */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new
        PaginationInterceptor();
    // 設(shè)置請求的頁面大于最大頁后操作, true調(diào)回到首頁,false 繼續(xù)請求 默認(rèn)false
    // paginationInterceptor.setOverflow(false);
    // 設(shè)置最大單頁限制數(shù)量,默認(rèn) 500 條,-1 不受限制
    // paginationInterceptor.setLimit(500);
    return paginationInterceptor;
}
}

使用分頁查詢

@Test
public void testPageList() {
    Page<User> page = new Page<User>(1, 2);
    userMapper.selectPage(page, null);
    // 輸出page對象分頁查詢信息
    System.out.println("總條數(shù):" + page.getTotal());
    System.out.println("每頁顯示條數(shù):" + page.getSize());
    System.out.println("總頁數(shù):" + page.getPages());
    System.out.println("當(dāng)前頁:" + page.getCurrent());
    System.out.println("是否有上一頁:" + page.hasPrevious());
    System.out.println("是否有下一頁:" + page.hasNext());
    System.out.println("查詢結(jié)果:" + page.getRecords());
}

四、邏輯刪除

1. application.yml 加入配置(如果你的默認(rèn)值和mp默認(rèn)的一樣,該配置可無):

#以下為mybatis-plus配置
mybatis-plus:
    global-config:
        db-config:
            logic-delete-field: flag #全局邏輯刪除字段值 3.3.0開始支持,詳情看下面。
            logic-delete-value: 1 # 邏輯已刪除值(默認(rèn)為 1)
            logic-not-delete-value: 0 # 邏輯未刪除值(默認(rèn)為 0)

3. 在數(shù)據(jù)庫中加入邏輯刪除的字段,并且對應(yīng)實(shí)體類中,該映射字段需要加入注解 @TableLogic

@Data
@TableName(value = "user") // 對應(yīng)數(shù)據(jù)庫表名
public class User {
    ......省略
    /**
    * 邏輯刪除字段
    */
    @TableField(value="delete_flag")
    @TableLogic
    private Integer deleteFlag;
}

到此這篇關(guān)于Mybatis-Plus3.x的使用的文章就介紹到這了,更多相關(guān)Mybatis-Plus3.x使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論