赞
踩
1.在application配置文件中增加如下配置
jdbc.driverClassName=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/warehouse?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai jdbc.username=root jdbc.password=123456 spring.datasource.other.driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver spring.datasource.other.url: jdbc:sqlserver://localhost:1433;DatabaseName=A spring.datasource.other.username: SA spring.datasource.other.password: Aa123456 spring.datasource.other.initialSize: 1 spring.datasource.other.minIdle: 5 spring.datasource.other.maxActive: 100 spring.datasource.other.maxWait: 60000 spring.datasource.other.testWhileIdle: true spring.datasource.other.testOnReturn: false spring.datasource.other.testOnBorrow: false spring.datasource.other.validationQuery: SELECT 1s spring.datasource.other.removeAbandoned: true spring.datasource.other.removeAbandonedTimeout: 1800 spring.datasource.other.logAbandoned: true spring.datasource.other.filters: mergeStat spring.datasource.other.timeBetweenEvictionRunsMillis: 60000 spring.datasource.other.minEvictableIdleTimeMillis: 25200000
2.增加mysql的两个配置类
import com.alibaba.druid.pool.DruidDataSource; 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.context.annotation.PropertySource; import javax.sql.DataSource; @Configuration @PropertySource(value = {"classpath:application.properties"})//也可以使用@ConfigurationProperties直接注入属性 public class DataSourceConfig { @Value("${jdbc.url}") private String jdbcUrl; @Value("${jdbc.driverClassName}") private String jdbcDriverClassName; @Value("${jdbc.username}") private String jdbcUsername; @Value("${jdbc.password}") private String jdbcPassword; /** * 配置数据库 * @return */ @Bean(name = "dataSource") public DataSource dataSource(){ DruidDataSource datasource = new DruidDataSource(); // 数据库驱动 datasource.setDriverClassName(jdbcDriverClassName); // 相应驱动的jdbcUrl datasource.setUrl(jdbcUrl); // 数据库的用户名 datasource.setUsername(jdbcUsername); // 数据库的密码 datasource.setPassword(jdbcPassword); // 每个分区最大的连接数 datasource.setMaxActive(20); // 每个分区最小的连接数 datasource.setMinIdle(5); return datasource; } }
import com.github.pagehelper.PageHelper; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.type.TypeHandler; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 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.core.io.support.ResourcePatternResolver; import javax.sql.DataSource; import java.util.Properties; @MapperScan(basePackages = "com.wenying.mapper", sqlSessionFactoryRef = "sqlSessionFactory") @Configuration public class MyBatisConfig { @Qualifier("dataSource") @Autowired private DataSource dataSource; @Bean(name="sqlSessionFactory") public SqlSessionFactory sqlSessionFactory(PageHelper pageHelper) { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); try { // 设置数据源 sqlSessionFactoryBean.setDataSource(dataSource); Properties properties = new Properties(); properties.setProperty("mapUnderscoreToCamelCase","true"); //全局映射器启用缓存 properties.setProperty("cacheEnabled","true"); properties.setProperty("useGeneratedKeys","true"); properties.setProperty("defaultExecutorType","REUSE"); sqlSessionFactoryBean.setConfigurationProperties(properties); sqlSessionFactoryBean.setPlugins(new Interceptor[] { pageHelper }); // 设置mybatis的主配置文件 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); //设置mybatis扫描的mapper.xml文件的路径(非常重要,否则找不到mapper.xml文件) sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:com/wenying/mapper/*.xml")); // 设置别名包,便于在mapper.xml文件中ParemeType和resultType不要写完整的包 sqlSessionFactoryBean.setTypeAliasesPackage("com/wenying/dto"); return sqlSessionFactoryBean.getObject(); }catch (Exception e) { e.printStackTrace(); throw new RuntimeException(); } } @Bean public PageHelper pageHelper() { PageHelper pageHelper = new PageHelper(); Properties p = new Properties(); p.setProperty("offsetAsPageNum", "true"); p.setProperty("rowBoundsWithCount", "true"); p.setProperty("reasonable", "true"); p.setProperty("dialect", "mysql"); pageHelper.setProperties(p); return pageHelper; } }
3.配置sqlserver的一个配置文件
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.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.PropertySource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; /** * sqlerver数据库配置 * @auth swy **/ @PropertySource(value = {"classpath:application.properties"}) @Configuration @MapperScan(basePackages = "com.wenying.sqlservermapper", sqlSessionFactoryRef = "otherSqlSessionFactory") public class MybatisDbOtherConfig { @Bean(name = "otherDataSource") @ConfigurationProperties(prefix = "spring.datasource.other") public DataSource dataSource(){ return DataSourceBuilder.create().build(); } @Bean(name = "otherTransactionManager") public DataSourceTransactionManager transactionManager(@Qualifier("otherDataSource") DataSource dataSource){ return new DataSourceTransactionManager(dataSource); } @Bean(name = "otherSqlSessionFactory") public SqlSessionFactory basicSqlSessionFactory(@Qualifier("otherDataSource") DataSource basicDataSource) throws Exception{ SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(basicDataSource); factoryBean.setTypeAliasesPackage("com/wenying/sqlserverdto"); factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/wenying/sqlservermapper/*Mapper.xml")); return factoryBean.getObject(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。