H__D  

  本例介绍SpringBoot与数据源的使用

JDBC

  1、新建一个项目SpringBoot Web项目,引入JDBC相关jar包。SpringBoot版本是:2.1.8.RELEASE,pom文件如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>com.test</groupId>
 8     <artifactId>test-springboot-jdbc</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10 
11     <parent>
12         <groupId>org.springframework.boot</groupId>
13         <artifactId>spring-boot-starter-parent</artifactId>
14         <version>2.1.8.RELEASE</version>
15     </parent>
16 
17     <properties>
18 
19         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21         <java.version>1.8</java.version>
22     </properties>
23 
24     <dependencies>
25 
26         <dependency>
27             <groupId>org.springframework.boot</groupId>
28             <artifactId>spring-boot-starter-web</artifactId>
29         </dependency>
30 
31         <!-- mysql -->
32         <dependency>
33             <groupId>org.springframework.boot</groupId>
34             <artifactId>spring-boot-starter-jdbc</artifactId>
35         </dependency>
36 
37         <!-- mysql -->
38         <dependency>
39             <groupId>mysql</groupId>
40             <artifactId>mysql-connector-java</artifactId>
41             <version>8.0.12</version>
42         </dependency>
43 
44         <dependency>
45             <groupId>org.springframework.boot</groupId>
46             <artifactId>spring-boot-starter-test</artifactId>
47             <scope>test</scope>
48         </dependency>
49 
50     </dependencies>
51 
52 
53     <!-- SpringBoot打包插件,可以将代码打包成一个可执行的jar包 -->
54     <build>
55         <plugins>
56             <plugin>
57                 <groupId>org.springframework.boot</groupId>
58                 <artifactId>spring-boot-maven-plugin</artifactId>
59             </plugin>
60         </plugins>
61     </build>
62 </project>

  2、配置数据源

1 spring:
2   datasource:
3     username: admin
4     password: 123456
5     url: jdbc:mysql://127.0.0.1:3306/test
6     driver-class-name: com.mysql.jdbc.Driver

  3、编辑测试JDBC代码,如下:

 1 package com.test.springboot;
 2 
 3 import com.mysql.jdbc.Driver;
 4 import org.junit.Test;
 5 import org.junit.runner.RunWith;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.boot.test.context.SpringBootTest;
 8 import org.springframework.jdbc.core.JdbcTemplate;
 9 import org.springframework.test.context.junit4.SpringRunner;
10 
11 import javax.sql.DataSource;
12 import java.sql.Connection;
13 import java.sql.PreparedStatement;
14 import java.sql.ResultSet;
15 import java.sql.SQLException;
16 import java.util.List;
17 import java.util.Map;
18 
19 @RunWith(SpringRunner.class)
20 @SpringBootTest
21 public class TestApplication {
22 
23     @Autowired
24     private DataSource dataSource;
25     
26     @Autowired
27     private JdbcTemplate jdbcTemplate;
28 
29     @Test
30     public void context() throws SQLException {
31         System.out.println("========");
32         System.out.println("dataSource ====" + dataSource);
33 
34         Connection connection = dataSource.getConnection();
35         System.out.println("connecttion ====" + connection);
36 
37         List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from user");
38         System.out.println(maps);
39     }
40 
41 }

  4、测试: 

    

    效果: 默认是用com.zaxxer.hikari.HikariDataSource作为数据源; 数据源的相关配置都在DataSourceProperties里面;

 数据自动配置原理

  通过参考org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration类,可以看到自动配置内容

  1、参考DataSourceConfiguration,根据配置创建数据源,默认使用HikariDataSource连接池;可以使用 spring.datasource.type指定自定义的数据源类型;

  2、SpringBoot默认可以支持:

    org.apache.tomcat.jdbc.pool.DataSource、HikariDataSource、BasicDataSource

  3、自定义数据源类型

 1 /**
 2  * Generic DataSource configuration.
 3  */
 4 @Configuration
 5 @ConditionalOnMissingBean(DataSource.class)
 6 @ConditionalOnProperty(name = "spring.datasource.type")
 7 static class Generic {
 8 
 9     @Bean
10     public DataSource dataSource(DataSourceProperties properties) {
11 
12         //使用DataSourceBuilder创建数据源,利用反射创建响应type的数据源,并且绑定相关属性
13         return properties.initializeDataSourceBuilder().build();
14     }
15 
16 }

  4、操作数据库:自动配置了JdbcTemplate操作数据库

整合Druid数据源

  1、引入druid数据源依赖

<!--引入druid数据源-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.8</version>
</dependency>

  2、编辑Druid数据源配置类

 1 package com.test.springboot.config;
 2 
 3 import com.alibaba.druid.pool.DruidDataSource;
 4 import com.alibaba.druid.support.http.StatViewServlet;
 5 import com.alibaba.druid.support.http.WebStatFilter;
 6 import org.springframework.boot.context.properties.ConfigurationProperties;
 7 import org.springframework.boot.web.servlet.FilterRegistrationBean;
 8 import org.springframework.boot.web.servlet.ServletRegistrationBean;
 9 import org.springframework.context.annotation.Bean;
10 import org.springframework.context.annotation.Configuration;
11 
12 import javax.sql.DataSource;
13 import java.util.Arrays;
14 import java.util.HashMap;
15 import java.util.Map;
16 
17 @Configuration
18 public class DruidConfig {
19 
20     @ConfigurationProperties(prefix = "spring.datasource")
21     @Bean
22     public DataSource druid(){
23        return  new DruidDataSource();
24     }
25 
26     //配置Druid的监控
27     //1、配置一个管理后台的Servlet
28     @Bean
29     public ServletRegistrationBean statViewServlet(){
30         ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
31         Map<String,String> initParams = new HashMap<>();
32 
33         // 后台用户名
34         initParams.put("loginUsername","admin");
35         // 后台密码
36         initParams.put("loginPassword","123456");
37         //默认就是允许所有访问
38         initParams.put("allow","");
39         // 拒接访问
40         initParams.put("deny","192.168.1.3");
41 
42         bean.setInitParameters(initParams);
43         return bean;
44     }
45 
46 
47     //2、配置一个web监控的filter
48     @Bean
49     public FilterRegistrationBean webStatFilter(){
50         FilterRegistrationBean bean = new FilterRegistrationBean();
51         bean.setFilter(new WebStatFilter());
52 
53         Map<String,String> initParams = new HashMap<>();
54         initParams.put("exclusions","*.js,*.css,*.png,*.jpg,/druid/*");
55 
56         bean.setInitParameters(initParams);
57 
58         bean.setUrlPatterns(Arrays.asList("/*"));
59 
60         return  bean;
61     }
62 }

  3、启动web项目。浏览器输入地址:http://localhost:8080/druid/,进行访问

    

 

 

posted on 2020-03-03 22:31  H__D  阅读(2852)  评论(0编辑  收藏  举报