引入druid
添加依赖
pom.xml中增加以下依赖:
<!-- Mysql驱动 -->
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.31</version>
</dependency>
<!-- druid -->
<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.9</version>
</dependency>
拓展:一般来说,较新版本的 mysql-connector-java 与较新版本的 MySQL 数据库兼容。例如,如果你使用的是 MySQL 8.0,你应该使用 mysql-connector-java 的 8.0 版本或更高版本。
application.yml配置
application.yml增加以下配置:
spring:datasource:# MySQL 5.x 或更早版本,使用 com.mysql.jdbc.Driverdriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/aged?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghaiusername: rootpassword: gusy1234druid:initial-size: 5min-idle: 5max-active: 20test-on-borrow: truevalidation-query: SELECT '*' FROM DUALfilters: stat,wall,slf4j
拓展:官方文档:druid内置Filter的别名 · alibaba/druid Wiki · GitHub
配置密码加密
进入druid jar包所在目录,控制台执行下面代码,以获取公钥和加密后的密码
# 版本改成自己下载的版本
java -cp druid-1.2.9.jar com.alibaba.druid.filter.config.ConfigTools gusy1234
执行结果:
privateKey:MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAi+4eW3wFcgsBfRrkJteLNfF2xhIytFyXJSb8BkxCyjmavXJYpfkJNRjYqjfLUdaROhYRD1Aqk4N2sklzd6jgXQIDAQABAkAMAgso3qO4/ajLC5u4mZagsm/232jqgWeXEJlgEPtKTzZDGqerWc15GqP+UFa2YeR8iAk3UCbVzcV3nEMht8ghAiEA3y2cjjlrA0U8hxMOb6OAmpkHGl0K1P9HcxMmvAN96u8CIQCgglHXQpwwC+UwxfCvfpqoaGil4kbgDOx60aybJEkZcwIhAKIcC3/zjRvPCuxCl1XYOe4SAerQcrHMzCV2X8hp8OjZAiA8TNlMQoUE8+MVsLdcZvbZX+g4scqor5JEDfOHkVk5CwIhANZe9QIwkPaac3lCoHolOxfG7j8jY4dZjcMwFkxeUqYI
publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIvuHlt8BXILAX0a5CbXizXxdsYSMrRclyUm/AZMQso5mr1yWKX5CTUY2Ko3y1HWkToWEQ9QKpODdrJJc3eo4F0CAwEAAQ==
password:Th1qqa5DXqDh6dUXtEnFoIRr+j0EsEab74NbbCzQdv46Gy8bVRdK5Mwy1OQeJVeUSr3URTBG5pZ9bAyy02gOLQ==
application.yml文件中增加及修改相关配置:
spring:datasource:# 修改,数据库密码为加密后密码password: Th1qqa5DXqDh6dUXtEnFoIRr+j0EsEab74NbbCzQdv46Gy8bVRdK5Mwy1OQeJVeUSr3URTBG5pZ9bAyy02gOLQ==druid:# 新增, 配置 connection-properties,启用加密,配置公钥。connection-properties: config.decrypt=true;config.decrypt.key=${publickey}# 修改,filters中加入 configfilters: config,stat,wall,slf4jfilter:# 新增,启动ConfigFilterconfig:# 启动ConfigFilterenabled: true
# 新增,值为生成的公钥
publickey: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIvuHlt8BXILAX0a5CbXizXxdsYSMrRclyUm/AZMQso5mr1yWKX5CTUY2Ko3y1HWkToWEQ9QKpODdrJJc3eo4F0CAwEAAQ==
拓展:官方文档:如何在Spring Boot中配置数据库密码加密? · alibaba/druid Wiki · GitHub
引入mybatis、mybatisPlus
添加依赖
pom.xml中增加以下依赖:
<!-- mybatis -->
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.3</version>
</dependency>
<!-- mybatis-plus -->
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.1.0</version>
</dependency>
application.yml配置
application.yml文件加入以下配置:
mybatis:mapper-locations: classpath:mapper/*.xml
启动类配置
SpringApplication.java中加入@MapperScan注解:
@SpringBootApplication
@MapperScan({"com.gusy.**.dao", "com.gusy.**.mapper"})
public class SpringApplication {public static void main(String[] args) {SpringApplication.run(SpringApplication.class, args);}
}
拓展:mybatisPlus官方文档