目录结构:dao目录下放mapper类,entity目录下放实体类,resources目录下mapper目录放对应xml文件
application.yml配置
spring: application: name: blog datasource: blog: driver-class-name: com.mysql.cj.jdbc.Driver jdbc-url: jdbc:mysql://localhost:3306/blog?characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC username: root password: password clock: driver-class-name: com.mysql.cj.jdbc.Driver jdbc-url: jdbc:mysql://localhost:3306/clock?characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC username: root password: password
数据源blog配置类
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; import javax.sql.DataSource; @Configuration @MapperScan(basePackages = "com.wgs.blog.dao.blog",sqlSessionTemplateRef = "blogSqlSessionTemplate") public class BlogDataSourceConfig { /** * 创建数据源 * @return DataSource */ @Bean(name = "blogDataSource") @ConfigurationProperties(prefix = "spring.datasource.blog") @Primary public DataSource blogDataSource() { return DataSourceBuilder.create().build(); } /** * 创建工厂 *@param dataSource *@throws Exception *@return SqlSessionFactory */ @Bean(name = "blogSqlSessionFactory") @Primary public SqlSessionFactory blogSqlSessionFactory(@Qualifier("blogDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/wgs/blog/mapper/blog/*.xml")); return bean.getObject(); } /** * 创建事务 *@param dataSource *@return DataSourceTransactionManager */ @Bean(name = "blogTransactionManager") @Primary public DataSourceTransactionManager blogDataSourceTransactionManager(@Qualifier("blogDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } /** * 创建模板 *@param sqlSessionFactory *@return SqlSessionTemplate */ @Bean(name = "blogSqlSessionTemplate") @Primary public SqlSessionTemplate blogSqlSessionTemplate(@Qualifier("blogSqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
数据源clock配置类
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; import javax.sql.DataSource; @Configuration @MapperScan(basePackages = "com.wgs.blog.dao.clock",sqlSessionTemplateRef = "clockSqlSessionTemplate") public class ClockDataSourceConfig { /** * 创建数据源 * @return DataSource */ @Bean(name = "clockDataSource") @ConfigurationProperties(prefix = "spring.datasource.clock") public DataSource clockDataSource() { return DataSourceBuilder.create().build(); } /** * 创建工厂 *@param dataSource *@throws Exception *@return SqlSessionFactory */ @Bean(name = "clockSqlSessionFactory") public SqlSessionFactory clockSqlSessionFactory(@Qualifier("clockDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/wgs/blog/mapper/clock/*.xml")); return bean.getObject(); } /** * 创建事务 *@param dataSource *@return DataSourceTransactionManager */ @Bean(name = "clockTransactionManager") public DataSourceTransactionManager clockDataSourceTransactionManager(@Qualifier("clockDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } /** * 创建模板 *@param sqlSessionFactory *@return SqlSessionTemplate */ @Bean(name = "clockSqlSessionTemplate") public SqlSessionTemplate clockSqlSessionTemplate(@Qualifier("clockSqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }