1.首先在pom.xml中添加依赖:
<!-- MySQL Driver --><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><!-- Druid连接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.18</version></dependency>
2.基础配置
数据库连接配置:
spring:datasource:url: jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=utf8username: rootpassword: root
Druid连接池配置:
spring:datasource:druid:initial-size: 5min-idle: 5max-active: 20
3.测试
@Autowired
private JdbcTemplate jdbcTemplate;@Test
public void test0(){String sql="update sm_user_tb set user_name='李某' where user_code='1001'";jdbcTemplate.execute(sql);
}
补充 复杂连接配置
datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/fengxue?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghaiusername: rootpassword: rootdruid:# 初始连接数initial-size: 5# 最小连接数min-idle: 5# 最大连接数max-active: 20# 获取连接超时时间max-wait: 60000# 连接有效性检测时间time-between-eviction-runs-millis: 60000# 连接在池中最小生存的时间min-evictable-idle-time-millis: 300000# 连接在池中最大生存的时间max-evictable-idle-time-millis: 900000# 检测连接是否有效validation-query: SELECT 1# 建议配置为true,不影响性能,并且保证安全性test-while-idle: truetest-on-borrow: falsetest-on-return: false# 打开PSCache,并且指定每个连接上PSCache的大小pool-prepared-statements: truemax-pool-prepared-statement-per-connection-size: 20# 配置监控统计拦截的filtersfilters: stat,wall# 通过connectProperties属性来打开mergeSql功能;慢SQL记录connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000# 配置DruidStatFilterweb-stat-filter:enabled: trueurl-pattern: /*exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"# 配置DruidStatViewServletstat-view-servlet:enabled: trueurl-pattern: /druid/*reset-enable: falselogin-username: adminlogin-password: adminallow: 127.0.0.1