创建名为springboot_druid的新module,过程参考3.1节
5.1、引入相关依赖
注意:虽然本文使用的是 spring boot 2.7.18 和 MySQL 5.7 ,但是出于可移植性、可扩展性和兼容性方面的考虑,
druid 的启动器使用的是 spring boot 3 版本的,MySQL 的驱动使用的是 MySQL 8 版本的。
<!-- jdbc启动器的依赖(包括 jdbctemplate 和事务相关的依赖和配置)-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- druid启动器的依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-3-starter</artifactId>
<version>1.2.20</version>
</dependency>
<!-- MySQL驱动的依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
5.2、配置druid相关参数
注意:在 spring boot 2 中,类似 spring.datasource.druid.url 的参数名称可以省略 druid 写成 spring.datasource.url ,
但是在 spring boot 3 中 druid 不能省略,因此为了能同时适用 spring boot 2 和 3,本文配置示例没有 druid 。
# 连接池类型(核心配置)
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# 数据库驱动名称(核心配置)
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据库url(核心配置)
spring.datasource.druid.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
# 数据库用户名(核心配置)
spring.datasource.druid.username=root
# 数据库密码(核心配置)
spring.datasource.druid.password=root
# 初始化时建立物理连接的个数
spring.datasource.druid.initial-size=5
# 连接池的最小空闲数量
spring.datasource.druid.min-idle=5
# 连接池最大连接数量
spring.datasource.druid.max-active=20
# 获取连接时最大等待时间,单位毫秒
spring.datasource.druid.max-wait=60000
# 申请连接的时候检测,如果等待时间大于time-between-eviction-runs-millis,执行validation-query检测连接是否有效。
spring.datasource.druid.test-while-idle=true
# 既作为检测的间隔时间,又作为test-while-idle执行的依据
spring.datasource.druid.time-between-eviction-runs-millis=60000
# 销毁线程时,检测当前连接的最后活动时间与当前时间的差值,差值大于该值时关闭当前连接
spring.datasource.druid.min-evictable-idle-time-millis=30000
# 用来检测数据库连接是否有效的sql,必须是一个查询语句(oracle中为 select 1 from dual)
spring.datasource.druid.validation-query=select 1
# 申请连接时会执行validation-query检测连接是否有效,开启会降低性能,默认为true
spring.datasource.druid.test-on-borrow=false
# 归还连接时会执行validation-query检测连接是否有效,开启会降低性能,默认为true
spring.datasource.druid.test-on-return=false
# 是否缓存preparedStatement,也就是PSCache,
# PSCache对支持游标的数据库性能提升巨大,比如说oracle,但在mysql下建议关闭。
spring.datasource.druid.pool-prepared-statements=false
# 要启用PSCache,必须配置大于0,当大于0时,pool-prepared-statements自动触发修改为true,
# 在Druid中,不会存在Oracle的PSCache占用内存过多的问题,所以要启用PSCache时可以把这个数值配置大一些,比如说100
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=-1
# 合并多个DruidDataSource的监控数据
spring.datasource.druid.use-global-data-source-stat=true
5.3、创建实体类
package online.liaojy.pojo;
import java.io.Serializable;
/**
* @author liaojy
* @date 2023/12/21 - 6:24
*/
public class Employee implements Serializable {
private Integer empId;
private String empName;
private Integer age;
private String sex;
private String email;
public Employee() {
}
public Employee(Integer empId, String empName, Integer age, String sex, String email) {
this.empId = empId;
this.empName = empName;
this.age = age;
this.sex = sex;
this.email = email;
}
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Employee{" +
"empId=" + empId +
", empName='" + empName + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", email='" + email + '\'' +
'}';
}
}
5.4、使用JdbcTemplate查询数据
注意:在pom中,引入的是jdbc的启动器,除了有一系列业务情景相关的依赖之外,还会有对应的配置;
jdbcTemplate 也会自动被加载到ioc容器中,因此可以直接注入使用;
在编译阶段,因为没有手动使用注解将JdbcTemplate标识为一个bean组件,所以idea可能会提示(误报)找不到bean;
此外,还因为配置的是 druid 数据库连接池,所以 jdbcTemplate 也会自动使用 druid 数据库连接池
@Autowired
private JdbcTemplate jdbcTemplate;
@RequestMapping("/getAllEmployee")
public List<Employee> getAllEmployee(){
String sql = "select * from t_emp";
List<Employee> employeeList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Employee>(Employee.class));
return employeeList;
}
5.5、测试效果
- SpringBoot2 SpringBoot Duridspringboot2 springboot durid springboot2 springboot2 springboot springboot2 springboot swagger3 swagger springboot2 springboot mybatis springboot2 springboot lettuce redis springboot2 springboot spring3 spring springboot2 springboot后台 管理系统 springboot2 springcache springboot springboot2 springboot mybatis3 mybatis