1.SpringMVC整合MyBatis及多数据源配置
1.1pom.xml文件引入mybatis配置
mysql mysql-connector-java ${mysql.version}org.mybatis mybatis ${mybatis-version}org.mybatis mybatis-spring ${mybatis-spring-version}com.mchange c3p0 ${c3p0.version}
1.2 /resources目录添加mybatis-config.xml配置文件以及mybatis目录
这个工程我配置了两个数据源,所以mybatis目录下的Mapper文件根据不同数据源分别放在不同目录下,这样编译扫描Mapper文件。
mybatis-config.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
Mapper.xml文件和Dao类要一一对应
Mapper.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?> select channel,name from tb_channel where deleted = 0 and length(name) > 0 and p_id = 91 ORDER BY id
namespace要指定对应的Dao类。Dao类内容如下:
package com.test.dao.second;import com.test.domain.ProductChannel;import org.apache.ibatis.annotations.Mapper;import java.util.List;/** * Created by on 2017/12/27. */@Mapperpublic interface ChannelDao { List getChannelList();}
1.3 spring-context.xml配置数据源
SpringMVC工程当中数据源的配置是写在spring-context.xml文件当中的。
其中${admin-jdbc.url}这些参数是从jdbc.properties配置文件中读取的,具体的配置在我的文章《SpringMVC和SpringBoot开发环境、生产环境的多环境配置》中有详细介绍。
SpringMVC的多数据源配置很简单,只要按照上面的方式再创建一组bean就行了,如下所示:
2.SpringBoot整合Mybatis及多数据源配置
SpringBoot对各种常用软件的整合做了很多很好的封装,SpringBoot整合MyBatis网上有很多教程,这里只介绍一下多数据源的SpringBoot配置。
2.1 application.properties配置不同数据源的连接配置
Mapper文件和Dao类同样根据数据源进行分组
2.2 创建数据源配置类
不同数据源创建不同的配置类,如下
配置类内容如下:
package com.xxxx.xxx.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.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.Primary;import org.springframework.context.annotation.PropertySource;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;/** * Created by on 17/7/17. */@Configuration@MapperScan(basePackages = "com.xxxx.xxx.dao.source", sqlSessionTemplateRef = "fromSqlSessionTemplate")@PropertySource("classpath:application.properties")public class SourceDataSourceConfig { @Bean(name = "fromDataSource") @ConfigurationProperties(prefix = "jdbcFrom") @Primary public DataSource fromDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "fromSqlSessionFactory") @Primary public SqlSessionFactory fromSqlSessionFactory(@Qualifier("fromDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/source/*Mapper.xml")); return bean.getObject(); } @Bean(name = "fromTransactionManager") @Primary public DataSourceTransactionManager fromTransactionManager(@Qualifier("fromDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "fromSqlSessionTemplate") @Primary public SqlSessionTemplate fromSqlSessionTemplate(@Qualifier("fromSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); }}第二个数据源的配置类同上,只是bean的名称要取不一样的。SpringMVC和SpringBoot整合mybatis以及多数据源配置就讲解到这里,更多精彩文章敬请期待。