最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot?+?Druid?+?MyBatis?Plus整合配置實(shí)現(xiàn)

 更新時(shí)間:2025年10月24日 08:31:09   作者:匆匆忙忙游刃有余  
本文主要介紹了SpringBoot?+?Druid?+?MyBatis?Plus整合配置實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1. 依賴配置

首先,在 pom.xml 中添加所需依賴:

<!-- Spring Boot Starter -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- MyBatis Plus Starter -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.2</version>
</dependency>

<!-- Druid 連接池 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.16</version>
</dependency>

<!-- 數(shù)據(jù)庫驅(qū)動(dòng) -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

<!-- Lombok 簡(jiǎn)化開發(fā) -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

2. 數(shù)據(jù)源配置 (application.yml)

application.yml 中配置數(shù)據(jù)庫連接和Druid連接池:

spring:
  datasource:
    # Druid 連接池配置
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      # 數(shù)據(jù)庫連接信息
      url: jdbc:mysql://localhost:3306/test_db?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
      username: root
      password: 123456
      driver-class-name: com.mysql.cj.jdbc.Driver
      
      # 初始化大小、最小、最大連接數(shù)
      initial-size: 5
      min-idle: 5
      max-active: 20
      
      # 配置獲取連接等待超時(shí)的時(shí)間
      max-wait: 60000
      
      # 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒
      time-between-eviction-runs-millis: 60000
      
      # 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒
      min-evictable-idle-time-millis: 300000
      
      # 用來檢測(cè)連接是否有效的sql,要求是一個(gè)查詢語句
      validation-query: SELECT 1
      
      # 建議配置為true,不影響性能,并且保證安全性
      test-while-idle: true
      
      # 申請(qǐng)連接時(shí)執(zhí)行validationQuery檢測(cè)連接是否有效
      test-on-borrow: false
      
      # 歸還連接時(shí)執(zhí)行validationQuery檢測(cè)連接是否有效
      test-on-return: false
      
      # 打開PSCache,并且指定每個(gè)連接上PSCache的大小
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      
      # 配置監(jiān)控統(tǒng)計(jì)攔截的filters
      filters: stat,wall,log4j2
      
      # 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
      connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
      
      # 合并多個(gè)DruidDataSource的監(jiān)控?cái)?shù)據(jù)
      use-global-data-source-stat: true
      
      # 監(jiān)控配置
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        login-username: admin
        login-password: admin
        # 允許重置統(tǒng)計(jì)數(shù)據(jù)
        reset-enable: true
        # IP白名單
        allow: 127.0.0.1
        # IP黑名單
        deny: 192.168.1.100
      
      # WebStatFilter配置
      web-stat-filter:
        enabled: true
        url-pattern: /*
        exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"

# MyBatis Plus 配置
mybatis-plus:
  # 指定Mapper XML文件位置
  mapper-locations: classpath*:mapper/**/*.xml
  # 指定實(shí)體類包路徑
  type-aliases-package: com.example.entity
  # 配置駝峰命名規(guī)則
  configuration:
    map-underscore-to-camel-case: true
    # 打印SQL日志
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  # 全局配置
  global-config:
    db-config:
      # 主鍵策略
      id-type: AUTO
      # 邏輯刪除配置
      logic-delete-field: deleted
      logic-delete-value: 1
      logic-not-delete-value: 0

3. 主啟動(dòng)類配置

在主啟動(dòng)類上添加 @MapperScan 注解,掃描Mapper接口:

package com.example;

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

@SpringBootApplication
@MapperScan("com.example.mapper") // 掃描Mapper接口所在包
public class SpringbootDruidMybatisPlusApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootDruidMybatisPlusApplication.class, args);
    }

}

4. Druid 配置類(可選)

如果需要更復(fù)雜的Druid配置,可以創(chuàng)建一個(gè)配置類:

package com.example.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;

@Configuration
public class DruidConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.druid")
    public DataSource druidDataSource() throws SQLException {
        DruidDataSource druidDataSource = new DruidDataSource();
        // 可以在這里進(jìn)行額外的配置
        return druidDataSource;
    }
}

5. MyBatis Plus 配置類(可選)

如需自定義MyBatis Plus的配置,可以創(chuàng)建配置類:

package com.example.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyBatisPlusConfig {

    /**
     * 配置分頁插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 添加分頁插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

6. 實(shí)體類示例

使用MyBatis Plus的注解配置實(shí)體類:

package com.example.entity;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;

@Data
@TableName("user") // 指定表名
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO) // 主鍵配置
    private Long id;

    @TableField("username") // 指定字段名
    private String username;

    @TableField("password")
    private String password;

    @TableField("email")
    private String email;

    @TableField(value = "create_time", fill = FieldFill.INSERT) // 自動(dòng)填充
    private LocalDateTime createTime;

    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE) // 自動(dòng)填充
    private LocalDateTime updateTime;

    @TableField("deleted")
    @TableLogic // 邏輯刪除
    private Integer deleted;
}

7. Mapper 接口示例

創(chuàng)建繼承BaseMapper的Mapper接口:

package com.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.entity.User;
import org.springframework.stereotype.Repository;

@Repository
public interface UserMapper extends BaseMapper<User> {
    // 可以添加自定義的SQL方法
}

8. Service 層示例

使用MyBatis Plus的IService接口:

package com.example.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.entity.User;

public interface UserService extends IService<User> {
    // 可以添加自定義的業(yè)務(wù)方法
}

Service實(shí)現(xiàn)類:

package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.User;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    // 實(shí)現(xiàn)自定義的業(yè)務(wù)方法
}

9. 訪問Druid監(jiān)控頁面

配置完成后,可以通過以下地址訪問Druid監(jiān)控頁面:

  • URL: http://localhost:8080/druid/login.html
  • 用戶名/密碼: admin/admin(根據(jù)application.yml中的配置)

10. 常見問題及解決方案

  1. 監(jiān)控頁面無法訪問

    • 檢查 stat-view-servlet.enabled 是否設(shè)置為 true
    • 檢查Spring Security是否攔截了Druid相關(guān)URL
  2. 數(shù)據(jù)庫連接失敗

    • 檢查數(shù)據(jù)庫地址、用戶名、密碼是否正確
    • 確認(rèn)數(shù)據(jù)庫服務(wù)是否正常運(yùn)行
  3. MyBatis Plus無法掃描到Mapper接口

    • 檢查 @MapperScan 注解的包路徑是否正確
    • 確保Mapper接口添加了 @Repository 注解
  4. 自動(dòng)填充功能不生效

    • 需要實(shí)現(xiàn) MetaObjectHandler 接口并注冊(cè)為Bean

通過以上配置,您可以成功在SpringBoot項(xiàng)目中整合Druid連接池和MyBatis Plus,享受連接池管理和ORM框架帶來的便利。

到此這篇關(guān)于SpringBoot + Druid + MyBatis Plus整合配置實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot Druid MyBatisPlus配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot整合Redis時(shí)遇到連接問題的解決方法

    SpringBoot整合Redis時(shí)遇到連接問題的解決方法

    在使用Spring Boot整合Redis的過程中,經(jīng)常會(huì)遇到連接問題,尤其是當(dāng)Redis服務(wù)部署在遠(yuǎn)程服務(wù)器上時(shí),所以本文給大家介紹了SpringBoot整合Redis遇到連接問題的解決方法,需要的朋友可以參考下
    2024-11-11
  • 解決SpringBoot整合ElasticSearch遇到的連接問題

    解決SpringBoot整合ElasticSearch遇到的連接問題

    這篇文章主要介紹了解決SpringBoot整合ElasticSearch遇到的連接問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Windows下RabbitMQ安裝及配置詳解

    Windows下RabbitMQ安裝及配置詳解

    本文主要介紹了Windows下RabbitMQ安裝及配置詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • String實(shí)例化及static final修飾符實(shí)現(xiàn)方法解析

    String實(shí)例化及static final修飾符實(shí)現(xiàn)方法解析

    這篇文章主要介紹了String實(shí)例化及static final修飾符實(shí)現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Java迭代器實(shí)現(xiàn)Python中的range代碼實(shí)例

    Java迭代器實(shí)現(xiàn)Python中的range代碼實(shí)例

    這篇文章主要介紹了Java迭代器實(shí)現(xiàn)Python中的range代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • idea不自動(dòng)生成target問題及解決

    idea不自動(dòng)生成target問題及解決

    文章講述了在使用IDEA開發(fā)Maven項(xiàng)目時(shí)遇到的常見問題及其解決方法,主要包括項(xiàng)目導(dǎo)入問題、資源文件未打包問題以及清理緩存的技巧
    2026-03-03
  • Java中實(shí)現(xiàn)漢字生成拼音首拼和五筆碼

    Java中實(shí)現(xiàn)漢字生成拼音首拼和五筆碼

    這篇文章主要介紹了Java中實(shí)現(xiàn)漢字生成拼音首拼和五筆碼方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • SpringBoot自動(dòng)配置原理,你真的懂嗎?(簡(jiǎn)單易懂)

    SpringBoot自動(dòng)配置原理,你真的懂嗎?(簡(jiǎn)單易懂)

    這篇文章主要介紹了SpringBoot自動(dòng)配置原理,你真的懂嗎?本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-05-05
  • java編譯時(shí)出現(xiàn)使用了未經(jīng)檢查或不安全的操作解決方法

    java編譯時(shí)出現(xiàn)使用了未經(jīng)檢查或不安全的操作解決方法

    這篇文章主要介紹了java編譯時(shí)出現(xiàn)使用了未經(jīng)檢查或不安全的操作的解決方法,需要的朋友可以參考下
    2014-03-03
  • springboot項(xiàng)目中分頁查詢的使用示例解析

    springboot項(xiàng)目中分頁查詢的使用示例解析

    本文詳細(xì)講解SpringBoot中兩種主流分頁方案:MyBatis+PageHelper和MyBatisPlus的實(shí)現(xiàn)方式、原理、優(yōu)缺點(diǎn)及實(shí)際應(yīng)用場(chǎng)景,內(nèi)容涵蓋配置、代碼示例、注意事項(xiàng)和對(duì)比總結(jié),感興趣的朋友一起看看吧
    2025-04-04

最新評(píng)論

中江县| 清苑县| 儋州市| 库伦旗| 崇义县| 尼木县| 丁青县| 建始县| 乐清市| 桑日县| 嘉鱼县| 班玛县| 乐平市| 勐海县| 原平市| 长丰县| 泸州市| 同德县| 三门峡市| 上思县| 昂仁县| 上栗县| 修水县| 大方县| 神木县| 营口市| 隆德县| 多伦县| 会宁县| 奇台县| 原阳县| 敖汉旗| 奉节县| 南昌县| 巴马| 正镶白旗| 越西县| 于都县| 云林县| 桓台县| 长治市|