directory

1. Introduce it all at once

2. Configure the configuration at a time

3. Data source configuration class 1

4. Data source configuration class 2

5. Summarise all at once


1. Introduce it all at once

<! <dependency> <groupId>org.mybatis. Boot </groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis-spring-boot}</version> </dependency> <! --> <dependency> <groupId>com.alibaba</groupId> <artifactId> Druid </artifactId> The < version > 1.0.13 < / version > < / dependency > <! --> <dependency> <groupId> MySQL </groupId> <artifactId>mysql-connector-java</artifactId> </dependency>Copy the code

 

2. Configure the configuration at a time

# # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- more data source configuration -- -- -- -- -- -- -- -- -- -- -- -- -- -- - # # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- # datasource1 configuration -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- # local environment ds1.datasource.url=jdbc:mysql://localhost:3306/xx001? useUnicode=true&characterEncoding=utf-8 ds1.datasource.username=root ds1.datasource.password=123456 ds1.datasource.driverClassName=com.mysql.jdbc.Driver ds1.datasource.initialSize=20 ds1.datasource.minIdle=20 ds1.datasource.maxActive=200 ds1.datasource.maxWait=60000 ds1.datasource.timeBetweenEvictionRunsMillis=60000 ds1.datasource.minEvictableIdleTimeMillis=300000 ds1.datasource.testWhileIdle=true ds1.datasource.testOnBorrow=false ds1.datasource.testOnReturn=false ds1.datasource.poolPreparedStatements=true ds1.datasource.maxPoolPreparedStatementPerConnectionSize=20 # # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- # datasource21 configuration -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Ds2. The datasource. Url = JDBC: mysql: / / 10.10.10.10:3306 / xx00? useUnicode=true&characterEncoding=utf-8 ds2.datasource.username=root ds2.datasource.password=123456 ds2.datasource.driverClassName=com.mysql.jdbc.Driver ds2.datasource.initialSize=20 ds2.datasource.minIdle=20 ds2.datasource.maxActive=200 ds2.datasource.maxWait=60000 ds2.datasource.timeBetweenEvictionRunsMillis=60000 ds2.datasource.minEvictableIdleTimeMillis=300000 ds2.datasource.testWhileIdle=true ds2.datasource.testOnBorrow=false ds2.datasource.testOnReturn=false ds2.datasource.poolPreparedStatements=true ds2.datasource.maxPoolPreparedStatementPerConnectionSize=20Copy the code

 

3. Data source configuration class 1

package com.superman.globaldao; import javax.sql.DataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import com.alibaba.druid.pool.DruidDataSource; @Configuration @MapperScan(basePackages = Datasource1Config.PACKAGE, SqlSessionFactoryRef = "ds1SqlSessionFactory") public class Datasource1Config { With other data sources in order to isolate the static final String PACKAGE = "com. Seen superman. Globaldao. Ds1"; static final String MAPPER_LOCATION = "classpath:mybatis/mapper/ds1/*.xml"; @Value("${ds1.datasource.url}") private String url; @Value("${ds1.datasource.username}") private String user; @Value("${ds1.datasource.password}") private String password; @Value("${ds1.datasource.driverClassName}") private String driverClass; @Value("${ds1.datasource.maxActive}") private Integer maxActive; @Value("${ds1.datasource.minIdle}") private Integer minIdle; @Value("${ds1.datasource.initialSize}") private Integer initialSize; @Value("${ds1.datasource.maxWait}") private Long maxWait; @Value("${ds1.datasource.timeBetweenEvictionRunsMillis}") private Long timeBetweenEvictionRunsMillis; @Value("${ds1.datasource.minEvictableIdleTimeMillis}") private Long minEvictableIdleTimeMillis; @Value("${ds1.datasource.testWhileIdle}") private Boolean testWhileIdle; @Value("${ds1.datasource.testWhileIdle}") private Boolean testOnBorrow; @Value("${ds1.datasource.testOnBorrow}") private Boolean testOnReturn; @Bean(name = "ds1DataSource") @Primary public DataSource ds1DataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driverClass); dataSource.setUrl(url); dataSource.setUsername(user); dataSource.setPassword(password); // Connectionpool configuration datasource.setMaxActive (maxActive); dataSource.setMinIdle(minIdle); dataSource.setInitialSize(initialSize); dataSource.setMaxWait(maxWait); dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); dataSource.setTestWhileIdle(testWhileIdle); dataSource.setTestOnBorrow(testOnBorrow); dataSource.setTestOnReturn(testOnReturn); dataSource.setValidationQuery("SELECT 'x'"); dataSource.setPoolPreparedStatements(true); dataSource.setMaxPoolPreparedStatementPerConnectionSize(20); return dataSource; } @Bean(name = "ds1TransactionManager") @Primary public DataSourceTransactionManager ds1TransactionManager() { return new DataSourceTransactionManager(ds1DataSource()); } @Bean(name = "ds1SqlSessionFactory") @Primary public SqlSessionFactory ds1SqlSessionFactory(@Qualifier("ds1DataSource") DataSource ds1DataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(ds1DataSource); sessionFactory.setTypeAliasesPackage("nc.edu.nuc.Test.entity"); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources(Datasource1Config.MAPPER_LOCATION)); return sessionFactory.getObject(); }}Copy the code

 

4. Data source configuration class 2

package com.superman.globaldao; import javax.sql.DataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import com.alibaba.druid.pool.DruidDataSource; @Configuration @MapperScan(basePackages = Datasource2Config.PACKAGE, SqlSessionFactoryRef = "ds2SqlSessionFactory") public class Datasource2Config {sqlSessionFactoryRef = "ds2SqlSessionFactory") public class Datasource2Config { With other data sources in order to isolate the static final String PACKAGE = "com. Seen superman. Globaldao. Ds2"; static final String MAPPER_LOCATION = "classpath:mybatis/mapper/ds2/*.xml"; @Value("${ds2.datasource.url}") private String url; @Value("${ds2.datasource.username}") private String user; @Value("${ds2.datasource.password}") private String password; @Value("${ds2.datasource.driverClassName}") private String driverClass; @Value("${ds2.datasource.maxActive}") private Integer maxActive; @Value("${ds2.datasource.minIdle}") private Integer minIdle; @Value("${ds2.datasource.initialSize}") private Integer initialSize; @Value("${ds2.datasource.maxWait}") private Long maxWait; @Value("${ds2.datasource.timeBetweenEvictionRunsMillis}") private Long timeBetweenEvictionRunsMillis; @Value("${ds2.datasource.minEvictableIdleTimeMillis}") private Long minEvictableIdleTimeMillis; @Value("${ds2.datasource.testWhileIdle}") private Boolean testWhileIdle; @Value("${ds2.datasource.testWhileIdle}") private Boolean testOnBorrow; @Value("${ds2.datasource.testOnBorrow}") private Boolean testOnReturn; @Bean(name = "ds2DataSource") public DataSource ds2DataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driverClass); dataSource.setUrl(url); dataSource.setUsername(user); dataSource.setPassword(password); // Connectionpool configuration datasource.setMaxActive (maxActive); dataSource.setMinIdle(minIdle); dataSource.setInitialSize(initialSize); dataSource.setMaxWait(maxWait); dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); dataSource.setTestWhileIdle(testWhileIdle); dataSource.setTestOnBorrow(testOnBorrow); dataSource.setTestOnReturn(testOnReturn); dataSource.setValidationQuery("SELECT 'x'"); dataSource.setPoolPreparedStatements(true); dataSource.setMaxPoolPreparedStatementPerConnectionSize(20); return dataSource; } @Bean(name = "ds2TransactionManager") public DataSourceTransactionManager ds2TransactionManager() { return new DataSourceTransactionManager(ds2DataSource()); } @Bean(name = "ds2SqlSessionFactory") public SqlSessionFactory ds2SqlSessionFactory(@Qualifier("ds2DataSource") DataSource ds2DataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(ds2DataSource); sessionFactory.setTypeAliasesPackage("nc.edu.nuc.Test.entity"); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources(Datasource2Config.MAPPER_LOCATION)); return sessionFactory.getObject(); }}Copy the code

 

5. Summarise all at once

  • This ds2/*.xml for MAPPER_LOCATION is a big deal
  • This configuration class 1 and configuration class 2 make this clearly distinguishable
  • Then write dao interface and xxoo.xml in the corresponding directory to complete
  • You don’t need to configure anything else to boot, so you can use it

 

 

ok

 

 

 

 

Continuously updated