练习二 数据库配置化

时间 2018/6/20 10:49:12 加载中...

在SQLHelper的getDataSource方法中,我们要使用的数据库是硬编码的。


HikariConfig config = new HikariConfig();

config.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/javablog?useUnicode=true&characterEncoding=utf8&useSSL=false");
config.setUsername("root");
config.setPassword("123456");
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");


我们需要将其配置化。


在 application.yml 文件中添加配置


datasource:
  mysql:
    JdbcUrl: jdbc:mysql://127.0.0.1:3306/javablog?useUnicode=true&characterEncoding=utf8&useSSL=false
    DriverClassName: com.mysql.jdbc.Driver
    Username: root
    Password: 123456
    useSSL: false


新建一个类来读取配置。

首先新建包 com.sqber.com.config ,并新建 MySQLDataSourceConfig.java 文件

package com.sqber.blog.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import com.zaxxer.hikari.HikariConfig;

@Component //自动注入
@ConfigurationProperties(prefix = "datasource.mysql")
public class MySQLDataSourceConfig extends HikariConfig{

}

通过 ConfigurationProperties 来指定从哪个配置来初始化对象 MySQLDataSourceConfig。


配置如何使用?

  1. 通过构造函数注入

  2. 通过 @Autowired 注解


我们采用第二种方法:


首先在 SQLHelper 中新增成员变量

@Autowired
private MySQLDataSourceConfig config1;

在SQLHelper上添加注解 @Component ,交给Spring Boot来管理,

这样config1才会赋值。


由于 SQLHelper 中用到配置的方法是静态的,我们还需要特殊处理一下。

private static MySQLDataSourceConfig config;
	
@PostConstruct
public void init() {
	config = config1;
}

通过 @PostConstruct 注解,将成员变量的值赋值给 静态变量即可。


这样我们就可以直接使用静态变量了。

	private static HikariDataSource getDataSource() throws SQLException {

//		HikariConfig config = new HikariConfig();
//
//		config.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/javablog?useUnicode=true&characterEncoding=utf8&useSSL=false");
//		config.setUsername("root");
//		config.setPassword("123456");
//		config.addDataSourceProperty("cachePrepStmts", "true");
//		config.addDataSourceProperty("prepStmtCacheSize", "250");
//		config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");

		return new HikariDataSource(config);
	}


修改后的 SQLHelper.java

package com.sqber.blog.base;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.MessageFormat;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sqber.blog.config.MySQLDataSourceConfig;
import com.zaxxer.hikari.HikariDataSource;

@Component 
public class SQLHelper {
	
	/*中间的略去*/
	
	@Autowired
	private MySQLDataSourceConfig config1;
	private static MySQLDataSourceConfig config;
	
	@PostConstruct
	public void init() {
		config = config1;
	}
	
	private static HikariDataSource getDataSource() throws SQLException {

//		HikariConfig config = new HikariConfig();
//
//		config.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/javablog?useUnicode=true&characterEncoding=utf8&useSSL=false");
//		config.setUsername("root");
//		config.setPassword("123456");
//		config.addDataSourceProperty("cachePrepStmts", "true");
//		config.addDataSourceProperty("prepStmtCacheSize", "250");
//		config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");

		return new HikariDataSource(config);
	}
}


参考

spring与springboot中,如何在static方法里用@Autowire或者@Resource注入的属性



版权说明
作者:SQBER
文章来源:http://sqber.com/articles/java-test2-db-config.html
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。