开发者学堂课程【SpringBoot快速掌握 - 核心技术:整合基本 JDBC 与数据源】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/612/detail/9274
整合基本 JDBC 与数据源
内容介绍
一、实操演示
二、课堂笔记
一、实操演示
启动器部分代码展示:
package com. atguigu.springboot;
import….
@Runwith(SpringRunner.class)@SpringBootTest
public class $pringBoot06DataJdbcApplicationTests {
@Autowired
DataSource;
@Test
public void contextLoads() throws SQLException {
//org.apache.tomcat.jdbc.pool.DataSource
system.out.println(dataSource.getClass());
Connection = dataSource.getConnection();System.out.print1n(connection);
connection.close();
}
}
Application.yml
部分代码展示:
spring:
datasource:
username: root
password: 123456
uri: jdbc : mysql://192.168.15.22:3306/jdbc
driver-c1ass-name:com.mysql.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSource
initia1Size: 5minIdle: 5maxActive: 20maxwait: 60000
timeBetweenEvictionRunsMi1lis:60000minEvictableIdleTimeMillis: 3000
validationQuery: SELECT1 FROM DUALtestWhileIdle: true
test0nBorrow: falsetestOnReturn: false
poo1PreparedStatements: true
#配置监控统计拦截的 filters,去掉后监控界面 sqL 无法统计,'wall' 用于防火墙 fi1ters: stat,wall ,log4j
maxPoo1preparedStatementPerConnectionsize:20useGloba1DataSourceStat: true
connectionProperties:druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
#schema:
#- classpath:department.sql
DruidConfig
类部分代码展示:
package com.atguigu.springboot.config;
import com.alibaba.druid.pool. DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class DruidConfig {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druid()i
return new DruidDataSource();
}
//配置 Druid 的监控
//配置一个管理后台的 servlet@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(),Map initParams = new HashMap<>();
initParams.put( "1oginUsername" , "admin" );
initParams.put( "1oginPassword" , "123456");
initParams.put( "a11ow" , "");
//默认就是允许所有访间initParams.put( "deny" ,"192.168.15.21");
bean.setInitParameters(initParams) ;
return bean;
}
//、配置一个 web 监控的 filter@Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();bean. setFilter(new webstatFilter());
Map initParams = new HashMap<>();
initParams.put( "exclusions", "*.js,*.css,/druid/*");
bean.setInitParameters(initParams);
bean.setUr1Patterns(Arrays.asList("/*"));
return bean;
}
}
二、课堂笔记
@configuration
public class Druidconfig {
@ConfigurationProperties(prefix= "spring.datasource")
@Bean
public Datasource druid()
return new DruidDatasource();}
//配置 Druid 的监控
//1、配置一个管理后台的 Servlet@Bean
public servletRegistrationBean statViewServlet(){
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(),
"/druid/*");
Map initParams - newlHashMap<>();
initParams.put("loginUsername" , "admin");
initParams.put("loginPassword" ,"123456");
initparams. put("allow”,"");
//默认就是允许所有访问 initParams.put("deny", "192.168. 15.21");
bean.setInitParameters(initParams);return bean;
}
//2、配置一个 web 监控的 filter@Bean
public FilterRegistrationBean webstatFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();bean.setFilter(new webstatFilter()) ;
Map initParams = new HashMap<>();initParams.put( "exclusions" ,"*.js ,*.css,/druid/*");
bean.setInitParameters( initParams);
bean.setUrlPatterns ( Arrays.asList("/*"));
returnbean;
}