Spring Boot default data source

1. Use the Spring Boot default data source

http://blog.csdn.net/qq_35760213/article/details/73863252

1.1 introducing POM dependency

In pom files, we directly rely on the official spring boot start JDBC module or spring boot start data JPA module.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
  • 1
  • 2
  • 3
  • 4

1.2 database configuration

Spring Boot supports. Properties format and. YML format configuration files, which can be selected at will according to personal habits (the author recommends YML format, which is more readable). Create the application.properties file or application.yml file under the classpath path path. The two types of configurations are as follows

  • . yml profile
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost/test
    username: root
    password: 123456
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Note: for the. yml type file, there must be a space between the colon and the value after the attribute name. For example, username: root is in the correct format, but useranme:root is in the wrong format.

  • . properties file configuration
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  • 1
  • 2
  • 3
  • 4

2. Use other third-party data sources

If you do not want to use the default tomcat-jdbc data source, you can also choose other excellent data sources according to your needs, such as Druid, c3p0 and so on. This paper takes Druid as an example.

2.1 introducing POM dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jdbc</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- Introduce Druid rely on -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.0.29</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

2.2 database configuration

Property configuration is the same as 1.2

2.3 defining data sources

@ComponentScan
@Configuration
public class ApplicationConfig {

    @Autowired
    private Environment env;

    @Bean
    public DataSource getDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(env.getProperty("spring.datasource.url"));
        dataSource.setUsername(env.getProperty("spring.datasource.username"));
        dataSource.setPassword(env.getProperty("spring.datasource.password"));
        return dataSource;
    }   
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

Or you can:

@ComponentScan
@Configuration
@ConfigurationProperties(prefix="spring.datasource")
public class ApplicationConfig {

    private String url;
    private String username;
    private String password;

    @Bean
    public DataSource getDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(username);// user name
        dataSource.setPassword(password);// Password
        return dataSource;
    }

    public String getUrl() {
     return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

Tags: Spring JDBC MySQL Druid

Posted on Sun, 03 May 2020 05:14:25 -0400 by Mijii