SpringBoot配置druid连接池连接mysql数据库

导读:本篇文章讲解 SpringBoot配置druid连接池连接mysql数据库,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

SpringBoot配置druid连接池连接mysql数据库


前言

druid是阿里巴巴开源的一款数据库连接池,性能很好,也有可视化的查询语句执行查看面板。

在pom.xml文件中添加druid的依赖,添加log4j的依赖,不加的话启动会报错,druid有用到它。


没有废话直接开始

  1. pom.xml文件中添加druid连接池依赖,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.jmccms</groupId>
    <artifactId>Jmccms</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Jmccms</name>
    <url>https://repo.spring.io/milestone</url>
    <description>CYJ:ChenYongJia 服务提供者Jmccms</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <!-- 项目属性配置信息 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <mysql-connector>5.1.39</mysql-connector>
        <junit-version>4.12</junit-version>
        <druid>1.1.20</druid>
        <log4j>1.2.17</log4j>
    </properties>

    <dependencies>

        <!-- 引入web依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- 引入AOP -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <!-- Spring Boot JPA 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!-- 引入test测试依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Junit 单元测试 依赖 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>

        <!-- 引入redis数据库依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <!-- Druid依赖 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>${druid}</version>
		</dependency>
		
		<!-- log4j 不加此依赖Druid的filters拦截会报错 在properties配置文件中filters中有体现 -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>${log4j}</version>
		</dependency>

        <!-- 引入mysql链接依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-connector}</version>
        </dependency>

        <!-- 引入 Lombok 依赖 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- 使用httpclient获取天气信息 -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.8</version>
        </dependency>
    
    </dependencies>
        
    <build>
        <plugins>
            <!-- SpringBoot 项目打jar包的Maven插件 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <!-- SpringBoot项目打包jar名称 -->
        <finalName>jmccms</finalName>
    </build>

</project>
  1. resource包下创建application.yml,加入以下内容:
server:
  #项目端口号
  port: 8066
  servlet:
    context-path: /Jmccms # 项目访问路径
  tomcat:# 优化tomcat
    max-connections: 200
    max-http-post-size: 0
    max-threads: 300
    min-spare-threads: 0
    uri-encoding: UTF-8

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/jmccms?useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
    password: root
  #集中解决各种编码问题
  banner:
    charset: UTF-8
  http:
    encoding:
      charset: UTF-8
      enabled: true
      force: true
  messages:
    encoding: UTF-8
  #     spring mvc 视图解析器
  mvc:
    view:
      prefix: /
      suffix: .html
  # 时间格式化
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    # 时区设置
    time-zone: GMT+8

大家自行测试,我的数据库密码是root,数据库名是jmccms


最后

  • 更多参考精彩博文请看这里:《陈永佳的博客》

  • 喜欢博主的小伙伴可以加个关注、点个赞哦,持续更新嘿嘿!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/97583.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!