2, configure a number of data sources (H2 database). 3, configure a number of data sources (Mysql database). 【 attach source 】Spring Boot actual combat series – four, log management 【 attach source 】Spring Boot actual combat series – five, transaction abstraction 【 attach source 】Spring Boot actual combat series – six, currency storage database processing

Introducing Mysql dependencies

  • Mysql driver
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
Copy the code
  • Mybatis persistence layer framework
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.4</version>
</dependency>
Copy the code

The code structure

Data source DB1 configuration file

Db1Config.java

@Slf4j
@Configuration
@MapperScan(basePackages = "com.qiuyi.springdemo.dao.db1", sqlSessionTemplateRef = "db1SqlSessionTemplate")
public class Db1Config {
    @Value("${spring.datasource.db1.jdbc-url}")
    private String db1JdbcUrl;


    /** * The spring.datasource. Db1 configuration in application.properties is valid *@return* /
    @Bean(name = "db1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.db1")
    @Primary
    public DataSource db1DataSource() {
        log.info(db1JdbcUrl);
        return DataSourceBuilder.create().build();
    }


    /** * transaction manager, inject the master library db1 * at instantiation time@param dataSource
     * @return* /
    @Bean(name = "db1TransactionManager")
    @Primary
    public DataSourceTransactionManager db1TransactionManager(@Qualifier("db1DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }


    /** * Inject configuration information into SqlSessionFactoryBean *@param DataSource Database connection information *@return
     * @throws Exception* /
    @Bean(name = "db1SqlSessionFactory")
    @Primary
    public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/db1/*.xml"));


        // Database field mapping to class field, underline style to hump style
        Objects.requireNonNull(bean.getObject()).getConfiguration().setMapUnderscoreToCamelCase(true);
        return bean.getObject();
    }


    /** * SqlSessionTemplate is thread-safe *@param sqlSessionFactory
     * @return
     * @throws Exception* /
    @Bean(name = "db1SqlSessionTemplate")
    @Primary
    public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
   
Copy the code

The data source DB2 configuration file is similar, see source code.

Data access layer

dao/db1/NewsDao.java

  • Through annotation
  • Through XML
@Repository
@Mapper
public interface NewsDao {
    @Select("select * from news")
    List<News> getAllNews();


    List<News> queryNewsAll();
}
Copy the code

The XML method

resources/mapper/db1/NewsDao.xml

<? xml version="1.0" encoding="UTF-8"? > <! DOCTYPE mapper PUBLIC"- / / mybatis.org//DTD Mapper / 3.0 / EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace="com.qiuyi.springdemo.dao.db1.NewsDao">
    <! -- Query all information -->
    <select id="queryNewsAll" resultType="com.qiuyi.springdemo.model.News">
      SELECT * FROM `news`
    </select>
</mapper>
Copy the code

Enable SQL statement printing

logging.level.com.qiuyi.springdemo.dao.*=DEBUG
Copy the code

The results

Source code download address

Github.com/qiulanzhu/S…