1. 配置文件:java
# 多數據源配置 # 後臺用DB spring.datasource.management.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.management.jdbc-url=jdbc:mysql://localhost:3306/management?serverTimezone=GMT%2B8&useUnicode=true&zeroDateTimeBehavior=convertToNull&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true spring.datasource.management.username=root spring.datasource.management.password=password # 教材中心用DB spring.datasource.jczx.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.jczx.jdbc-url=jdbc:mysql://localhost:3306/jczx?serverTimezone=GMT%2B8&useUnicode=true&zeroDateTimeBehavior=convertToNull&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true spring.datasource.jczx.username=root spring.datasource.jczx.password=password ############################################################################################## # 統一使用配置類進行配置實現; ## # application.properties中的數據源配置,spring加載時默認是單數據源配置, ## # 因此相關的配置都註釋掉,統一使用Config配置類進行配置! ## # mybatis ## #mybatis.type-aliases-package=com.wyait.manage.pojo ## #mybatis.mapper-locations=classpath:mapper/*.xml ## # 開啓駝峯映射 ## #mybatis.configuration.map-underscore-to-camel-case=true ## ##############################################################################################
2. java配置類:mysql
/** * Title: JczxDataSourceConfig.java * Description: * Company: 長江數字 * @author JIMO * @date 2019年4月26日 * @version 1.0 */ package com.cjsz.management.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.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; 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; /** * Title: JczxDataSourceConfig * Description: * @author JIMO * @date 2019年4月26日 */ @Configuration //指明瞭掃描dao層,而且給dao層注入指定的SqlSessionTemplate @MapperScan(basePackages = "com.cjsz.management.jczx.mybatis.mapper", sqlSessionTemplateRef = "jczxSqlSessionTemplate") public class JczxDataSourceConfig { /** * 建立datasource對象 * @return */ @Bean(name = "jczxDataSource") @ConfigurationProperties(prefix = "spring.datasource.jczx")// prefix值必須是application.properteis中對應屬性的前綴 //@Primary public DataSource jczxDataSource() { return DataSourceBuilder.create().build(); } /** * 建立sql工程 * @param dataSource * @return * @throws Exception */ @Bean(name = "jczxSqlSessionFactory") //@Primary public SqlSessionFactory jczxSqlSessionFactory(@Qualifier("jczxDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); //對應mybatis.type-aliases-package配置 bean.setTypeAliasesPackage("com.cjsz.management.jczx.mybatis.model"); //對應mybatis.mapper-locations配置 bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:jczxMapping/*.xml")); //開啓駝峯映射 bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true); return bean.getObject(); } /** * 配置事務管理 * @param dataSource * @return */ @Bean(name = "jczxTransactionManager") //@Primary public DataSourceTransactionManager jczxTransactionManager(@Qualifier("jczxDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } /** * sqlSession模版,用於配置自動掃描pojo實體類 * @param sqlSessionFactory * @return * @throws Exception */ @Bean(name = "jczxSqlSessionTemplate") //@Primary public SqlSessionTemplate jczxSqlSessionTemplate(@Qualifier("jczxSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
/** * Title: DataSourceConfig.java * Description: * Company: 長江數字 * @author JIMO * @date 2019年4月26日 * @version 1.0 */ package com.cjsz.management.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.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; 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; /** * Title: ManagementDataSourceConfig * Description: management數據庫配置 * @author JIMO * @date 2019年4月26日 */ @Configuration //指明瞭掃描dao層,而且給dao層注入指定的SqlSessionTemplate @MapperScan(basePackages = "com.cjsz.management.mybatis.mapper", sqlSessionTemplateRef = "managementSqlSessionTemplate") public class ManagementDataSourceConfig { /** * 建立datasource對象 * @return */ @Bean(name = "managementDataSource") @ConfigurationProperties(prefix = "spring.datasource.management")// prefix值必須是application.properteis中對應屬性的前綴 @Primary public DataSource managementDataSource() { return DataSourceBuilder.create().build(); } /** * 建立sql工程 * @param dataSource * @return * @throws Exception */ @Bean(name = "managementSqlSessionFactory") @Primary public SqlSessionFactory managementSqlSessionFactory(@Qualifier("managementDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); //對應mybatis.type-aliases-package配置 bean.setTypeAliasesPackage("com.cjsz.management.mybatis.model"); //對應mybatis.mapper-locations配置 bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:managementMapping/*.xml")); //開啓駝峯映射 bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true); return bean.getObject(); } /** * 配置事務管理 * @param dataSource * @return */ @Bean(name = "managementTransactionManager") @Primary public DataSourceTransactionManager managementTransactionManager(@Qualifier("managementDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } /** * sqlSession模版,用於配置自動掃描pojo實體類 * @param sqlSessionFactory * @return * @throws Exception */ @Bean(name = "managementSqlSessionTemplate") @Primary public SqlSessionTemplate managementSqlSessionTemplate(@Qualifier("managementSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
注:若是xml文件在其餘工程或者jar包中,須要在classpath後加上*號:spring
//對應mybatis.mapper-locations配置 bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:managementMapping/*.xml"));