創(chuàng)建名為springboot_druid的新module,過程參考3.1節(jié)
5.1、引入相關(guān)依賴
注意:雖然本文使用的是 spring boot 2.7.18 和 MySQL 5.7 ,但是出于可移植性、可擴(kuò)展性和兼容性方面的考慮,
druid 的啟動器使用的是 spring boot 3 版本的,MySQL 的驅(qū)動使用的是 MySQL 8 版本的。
<!-- jdbc啟動器的依賴(包括 jdbctemplate 和事務(wù)相關(guān)的依賴和配置)-->
<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驅(qū)動的依賴-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
5.2、配置druid相關(guān)參數(shù)
注意:在 spring boot 2 中,類似 spring.datasource.druid.url 的參數(shù)名稱可以省略 druid 寫成 spring.datasource.url ,
但是在 spring boot 3 中 druid 不能省略,因此為了能同時適用 spring boot 2 和 3,本文配置示例沒有 druid 。
# 連接池類型(核心配置)
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# 數(shù)據(jù)庫驅(qū)動名稱(核心配置)
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
# 數(shù)據(jù)庫url(核心配置)
spring.datasource.druid.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
# 數(shù)據(jù)庫用戶名(核心配置)
spring.datasource.druid.username=root
# 數(shù)據(jù)庫密碼(核心配置)
spring.datasource.druid.password=root
# 初始化時建立物理連接的個數(shù)
spring.datasource.druid.initial-size=5
# 連接池的最小空閑數(shù)量
spring.datasource.druid.min-idle=5
# 連接池最大連接數(shù)量
spring.datasource.druid.max-active=20
# 獲取連接時最大等待時間,單位毫秒
spring.datasource.druid.max-wait=60000
# 申請連接的時候檢測,如果等待時間大于time-between-eviction-runs-millis,執(zhí)行validation-query檢測連接是否有效。
spring.datasource.druid.test-while-idle=true
# 既作為檢測的間隔時間,又作為test-while-idle執(zhí)行的依據(jù)
spring.datasource.druid.time-between-eviction-runs-millis=60000
# 銷毀線程時,檢測當(dāng)前連接的最后活動時間與當(dāng)前時間的差值,差值大于該值時關(guān)閉當(dāng)前連接
spring.datasource.druid.min-evictable-idle-time-millis=30000
# 用來檢測數(shù)據(jù)庫連接是否有效的sql,必須是一個查詢語句(oracle中為 select 1 from dual)
spring.datasource.druid.validation-query=select 1
# 申請連接時會執(zhí)行validation-query檢測連接是否有效,開啟會降低性能,默認(rèn)為true
spring.datasource.druid.test-on-borrow=false
# 歸還連接時會執(zhí)行validation-query檢測連接是否有效,開啟會降低性能,默認(rèn)為true
spring.datasource.druid.test-on-return=false
# 是否緩存preparedStatement,也就是PSCache,
# PSCache對支持游標(biāo)的數(shù)據(jù)庫性能提升巨大,比如說oracle,但在mysql下建議關(guān)閉。
spring.datasource.druid.pool-prepared-statements=false
# 要啟用PSCache,必須配置大于0,當(dāng)大于0時,pool-prepared-statements自動觸發(fā)修改為true,
# 在Druid中,不會存在Oracle的PSCache占用內(nèi)存過多的問題,所以要啟用PSCache時可以把這個數(shù)值配置大一些,比如說100
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=-1
# 合并多個DruidDataSource的監(jiān)控數(shù)據(jù)
spring.datasource.druid.use-global-data-source-stat=true
5.3、創(chuàng)建實體類
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查詢數(shù)據(jù)
注意:在pom中,引入的是jdbc的啟動器,除了有一系列業(yè)務(wù)情景相關(guān)的依賴之外,還會有對應(yīng)的配置;
jdbcTemplate 也會自動被加載到ioc容器中,因此可以直接注入使用;
在編譯階段,因為沒有手動使用注解將JdbcTemplate標(biāo)識為一個bean組件,所以idea可能會提示(誤報)找不到bean;
此外,還因為配置的是 druid 數(shù)據(jù)庫連接池,所以 jdbcTemplate 也會自動使用 druid 數(shù)據(jù)庫連接池文章來源:http://www.zghlxwxcb.cn/news/detail-760656.html
@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、測試效果
文章來源地址http://www.zghlxwxcb.cn/news/detail-760656.html
到了這里,關(guān)于5、SpringBoot2之整合Durid的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!