SpringBoot整合多資料來源
阿新 • • 發佈:2018-12-01
多資料來源就是連線多個數據庫
1、在application.properties中配置兩個資料庫
spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=root spring.datasource.test2.driverClassName=com.mysql.jdbc.Driver spring.datasource.test2.url=jdbc:mysql://localhost:3306/test2?characterEncoding=UTF-8 spring.datasource.test2.username=root spring.datasource.test2.password=root spring.thymeleaf.mode=HTML5 spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html #開發時關閉快取,不然沒法看到實時頁面 spring.thymeleaf.cache=false
2、建立兩個資料庫配置類(DataSourceConfig、DataSourceConfig2)
package com.cppdy.config; 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.Qualifier; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary; import org.springframework.jdbc.datasource.DataSourceTransactionManager; //配置mapper所在的包 @Configuration @MapperScan(basePackages="com.cppdy.mapper.test1",sqlSessionTemplateRef="testSqlSessionTemplate") public class DataSourceConfig { @Bean(name="testDataSource") @ConfigurationProperties(prefix="spring.datasource") //primary代表主庫 @Primary public DataSource testDateSource() { return DataSourceBuilder.create().build(); } @Bean(name="testSqlSessionFactory") @Primary public SqlSessionFactory testSqlSessionFactory(@Qualifier("testDataSource") DataSource dataSource) throws Exception{ SqlSessionFactoryBean bean=new SqlSessionFactoryBean(); bean.setDataSource(dataSource); return bean.getObject(); } @Bean(name="testTransactionManager") @Primary public DataSourceTransactionManager testTransactionManager(@Qualifier("testDataSource") DataSource dataSource){ return new DataSourceTransactionManager(dataSource); } @Bean(name="testSqlSessionTemplate") @Primary public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("testSqlSessionFactory") SqlSessionFactory sqlSessionFactory){ return new SqlSessionTemplate(sqlSessionFactory); } }
package com.cppdy.config; 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.Qualifier; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.DataSourceTransactionManager; //配置mapper所在的包 @Configuration @MapperScan(basePackages = "com.cppdy.mapper.test2", sqlSessionTemplateRef = "test2SqlSessionTemplate") public class DataSourceConfig2 { @Bean(name = "test2DataSource") @ConfigurationProperties(prefix = "spring.datasource.test2") public DataSource testDateSource() { return DataSourceBuilder.create().build(); } @Bean(name = "test2SqlSessionFactory") public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); return bean.getObject(); } @Bean(name = "test2TransactionManager") public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "test2SqlSessionTemplate") public SqlSessionTemplate testSqlSessionTemplate( @Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
3、在mapper資料夾下建立test1、tests2資料夾,並在兩個資料夾下分別建立(UserMapper、UserMapper2)
package com.cppdy.mapper.test1; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import com.cppdy.entity.User; @Mapper public interface UserMapper { @Select("select * from user where id=#{id}") User getUserById(@Param("id") int id); @Insert("insert into user values(NULL,#{name})") User insertUser(@Param("name") String name); @Update("update user set username=#{name} where id=#{id}") User update(@Param("id") int id,@Param("name") String name); @Delete("delete from user where id=#{id}") User delete(@Param("id") int id); }
package com.cppdy.mapper.test2; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import com.cppdy.entity.User; @Mapper public interface UserMapper2 { @Select("select * from user where id=#{id}") User getUserById(@Param("id") int id); }
4、在HelloWordController中建立測試方法
@RequestMapping("getUserById2") public User getUserById2() { return userMapper2.getUserById(1); }
5、分別呼叫getUserById和getUserById2方法,測試從兩個資料庫分別獲取使用者資訊