在Springboot中配置多数据源
首先在application.properties文件中配置好两个数据源的地址
server.port=8080
server.context-path=/p2p-admin01
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
#p2padmin 两个数据源
spring.datasource.p2padminurl=jdbc:mysql://192.168.119.128:3306/p2padmin?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.p2padminusername=root
spring.datasource.p2padminpassword=123456
spring.datasource.p2padmindriver=com.mysql.jdbc.Driver
#p2padmin
spring.datasource.p2purl=jdbc:mysql://192.168.119.128:3306/p2p?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.p2pusername=root
spring.datasource.p2ppassword=123456
spring.datasource.p2pdriver=com.mysql.jdbc.Driver
#配置mybatis的配置文件路径
mybatis.mapper-locations=classpath:com/bjpowernode/p2p/admin/mapper/*.xml
第二步; 写一个类 读取到配置文件中的数据信息放在DataSourceConfig 对象中 一定要注意两个注解@Component
@ConfigurationProperties(prefix="spring.datasource") // 这个是通过读取配置文件中的前缀为spring.datasource的信息
package com.bjpowernode.p2p.admin.datasource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix="spring.datasource")
public class DataSourceConfig {
//#p2padmin
private String p2padminurl;
private String p2padminusername;
private String p2padminpassword;
private String p2padmindriver;
//#p2p
private String p2purl;
private String p2pusername;
private String p2ppassword;
private String p2pdriver;
public String getP2padminurl() {
return p2padminurl;
}
public void setP2padminurl(String p2padminurl) {
this.p2padminurl = p2padminurl;
}
public String getP2padminusername() {
return p2padminusername;
}
public void setP2padminusername(String p2padminusername) {
this.p2padminusername = p2padminusername;
}
public String getP2padminpassword() {
return p2padminpassword;
}
public void setP2padminpassword(String p2padminpassword) {
this.p2padminpassword = p2padminpassword;
}
public String getP2padmindriver() {
return p2padmindriver;
}
public void setP2padmindriver(String p2padmindriver) {
this.p2padmindriver = p2padmindriver;
}
public String getP2purl() {
return p2purl;
}
public void setP2purl(String p2purl) {
this.p2purl = p2purl;
}
public String getP2pusername() {
return p2pusername;
}
public void setP2pusername(String p2pusername) {
this.p2pusername = p2pusername;
}
public String getP2ppassword() {
return p2ppassword;
}
public void setP2ppassword(String p2ppassword) {
this.p2ppassword = p2ppassword;
}
public String getP2pdriver() {
return p2pdriver;
}
public void setP2pdriver(String p2pdriver) {
this.p2pdriver = p2pdriver;
}
}
第三步:写一个类创建一个数据源、一个sqlSessionFactory工厂、一个SqlSessionTemplate
再写一个类去配置另一个数据源
package com.bjpowernode.p2p.admin.datasource;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
@MapperScan(basePackages="com.bjpowernode.p2p.admin.mapper",sqlSessionTemplateRef="getP2PAdminSqlsessionTemplate")
public class P2PadminDataSource {
@Autowired
private DataSourceConfig dataSourceConfig;
/**
* 配置p2padmin的数据源
* @return
*/
@Bean
public DataSource getP2PAdminDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(dataSourceConfig.getP2padminurl());
dataSource.setPassword(dataSourceConfig.getP2padminpassword());
dataSource.setUsername(dataSourceConfig.getP2padminusername());
dataSource.setDriverClassName(dataSourceConfig.getP2padmindriver());
return dataSource;
}
/**
* 配置sqlSessionFactory工厂
* @return
* @throws Exception
*/
@Bean
public SqlSessionFactory getP2PAdminSqlSessionFactory() throws Exception {
SqlSessionFactoryBean SqlSessionFactory = new SqlSessionFactoryBean();
SqlSessionFactory.setDataSource(this.getP2PAdminDataSource());//通过this来将数据源配置进来
return SqlSessionFactory.getObject();
}
/**
* 配置SqlSessionTemplate
* @return
* @throws Exception
*/
@Bean
public SqlSessionTemplate getP2PAdminSqlsessionTemplate() throws Exception {
SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(this.getP2PAdminSqlSessionFactory());
return sqlSessionTemplate;
}
}
第四步: 要在启动时,加上//exclude = (DataSourceAutoConfiguration.class)排除掉自动配置的数据源 因为配置了两个数据源
@SpringBootApplication(exclude = (DataSourceAutoConfiguration.class))
----------------------------------------------------------------------------------
package com.bjpowernode.p2p.admin;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
//exclude = (DataSourceAutoConfiguration.class)排除掉自动配置的数据源 因为配置了两个数据源
@SpringBootApplication(exclude = (DataSourceAutoConfiguration.class))
public class P2pAdminApplication {
/**
* 定义一个log4j2的日志记录器
*/
private static final Logger logger = (Logger) LogManager.getLogger(P2pAdminApplication.class);
public static void main(String[] args) {
logger.info("spring boor 启动···················");
SpringApplication.run(P2pAdminApplication.class, args);
}
}