In the first article, I briefly introduced some things about springboot, and now I slowly enter the advanced things. Generally simple, only one database is used for relatively small projects, and multiple tables are used to process business. But when the business is a little more complex, you may need to use multiple data sources. Due to business requirements, you have to use different data sources. I have seen complex projects using more than ten data sources, but generally two or three are OK. Alright, let's start our code.
package microservice.com.config;
import java.io.InputStream;
import java.util.Properties;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
/**
* 数据源配置
*
* @author 肖云鹤
* 2016年12月10日
*/
@Configuration
public class DataSourceConfig {
/**
* ͬwhite数据源
*/
@Bean(name="whiteDataSource", destroyMethod="close")
@Qualifier("whiteDataSource")
@Primary //使用多数据源时需要加上这个注解
public DruidDataSource whiteDataSource() throws Exception {
InputStream is = null;
Properties props = null;
try {
is = this.getClass().getClassLoader().getResourceAsStream("datasource.white.properties");
props = new Properties();
props.load(is);
} finally {
if (is != null) is.close();
}
DruidDataSource ds = (DruidDataSource) DruidDataSourceFactory.createDataSource(props);
return ds;
}
/**
* me数据源
*/
@Bean(name="meDataSource", destroyMethod="close")
@Qualifier("meDataSource")
public DruidDataSource meDataSource() throws Exception {
InputStream is = null;
Properties props = null;
try {
is = this.getClass().getClassLoader().getResourceAsStream("datasource.me.properties");
props = new Properties();
props.load(is);
} finally {
if (is != null) is.close();
}
DruidDataSource ds = (DruidDataSource) DruidDataSourceFactory.createDataSource(props);
return ds; * Exam supervision data source /**
}
*/
@Bean(name="xyDataSource", destroyMethod="close")
@Qualifier("xyDataSource")
public DruidDataSource xyDataSource() throws Exception {
InputStream is = null;
Properties props = null;
try {
is = this.getClass().getClassLoader().getResourceAsStream("datasource.xy.properties");
props = new Properties();
props.load(is);
} finally {
if (is != null) is.close();
}
DruidDataSource ds = (DruidDataSource) DruidDataSourceFactory.createDataSource(props);
return ds;
}
/**
* white Jdbc
*/
@Bean(name="whiteJdbc")
public JdbcTemplate whiteJdbc(@Qualifier("whiteDataSource")DruidDataSource dataSource){
return new JdbcTemplate(dataSource);
}
/**
* 书城Jdbc
*/
@Bean(name="meJdbc")
public JdbcTemplate meJdbc(@Qualifier("meDataSource")DruidDataSource dataSource){
return new JdbcTemplate(dataSource);
}
/**
* 校园易商城Jdbc
*/
@Bean(name="xyJdbc")
public JdbcTemplate xyJdbc(@Qualifier("xyDataSource")DruidDataSource dataSource){
return new JdbcTemplate(dataSource);
}
}
The above is the jdbctemple that comes with spring. I use Alibaba's data source, which has its own encryption tool. Inject the datasource. It should be noted that the @Primary annotation may have several different implementation classes for the same interface, and one implementation is adopted by default.
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/white
username=root
password=bNVOqb7WKLX5Bjnw+LMv92taj25KOxDimXxILPQjw42wgv+1lHzOH8kr97xDwWdhpY67QuYCS7sWN4W46YbkFA==
initialSize=1
maxActive=10
minIdle=3
maxWait=60000
removeAbandoned=true
removeAbandonedTimeout=180
timeBetweenEvictionRunsMills=60000
minEvictableIdleTimeMills=300000
validationQuery=select 1 from dual
testWhileIdle=true
testOnBorrow=false
testOnReturn=false
poolPreparedStatements=true
maxPoolPreparedStatementPerConnectionSize=50
filters=config,stat
connectionProperties=config.decrypt=true
This is my data source configuration, you can modify it yourself. You can also use simple configuration such as account and password.
Now to use Spring's JDBCtemple
@Component
public class meDao {
@Autowired
@Qualifier("meJdbc")
private JdbcTemplate jdbc;
}
JDBC is injected here, which is very convenient to use. It encapsulates basic additions, deletions, and changes to check, batch query, and so on.
/**依据所有的查询用户*/
public List<User> queryUserAll(){
List <User> list = new ArrayList<User>();
List<Map <String, Object>> result = jdbc.queryForList(allSql);
for (Map <String, Object> map :result){
User user = new User();
user.setAddress((String) map.get("address"));
user.setBuyername((String) map.get("buyername"));
user.setEmail((String) map.get("email"));
user.setRealname((String) map.get("realname"));
user.setBuyerjf((int) map.get("buyerjf"));
list.add(user);
}
return list;
}
All users are queried here, which is similar to how you usually use native JDBC operations. If you want to know more about jdbcTemple, you can take a look at Api.
Basically the above is a configuration of multiple data sources.