spring4(springboot)的多数据源配置
1,用切面的方式注解区别,在切面中切换执行的数据源
2,不同的文件夹不同的mapper扫描,不同的mapper不同的数据源模板
下面是2实例:
1 使用mybatis注解需要的配置。如下面的代码所示,使用@MapperScan来扫描注册mybatis数据库接口类,其中basePackages属性表明接口类所在的包,sqlSessionTemplateRef表明接口类使用的SqlSessionTemplate。如果项目需要配置两个数据库,@MapperScan也需要分别配置。
@Configuration
@MapperScan(basePackages = "com.tigerobo.mysteel.du.table.mapper.index", sqlSessionTemplateRef = "indexSqlSessionTemplate")
public class IndexMybatisConfiguration {
@Bean(name = "indexSqlSessionTemplate")
@Primary
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("indexSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean(name = "indexSqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("indexDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setConfigLocation(resolver.getResource("classpath:mybatis/mybatis-config.xml"));
bean.setMapperLocations(resolver.getResources("classpath:mybatis/mapper/index/*.*"));
return bean.getObject();
}
@Bean(name = "indexDataSource")
@ConfigurationProperties(prefix = "db.index")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
}
package com.tigerobo.mysteel.du.table.config;
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 javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.tigerobo.mysteel.du.table.mapper.news", sqlSessionTemplateRef = "newsSqlSessionTemplate")
public class NewsMybatisConfiguration {
@Bean(name = "newsSqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("newsSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean(name = "newsSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("newsDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setConfigLocation(resolver.getResource("classpath:mybatis/mybatis-config.xml"));
bean.setMapperLocations(resolver.getResources("classpath:mybatis/mapper/news/*.xml"));
return bean.getObject();
}
@Bean(name = "newsDataSource")
@ConfigurationProperties(prefix = "db.news")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
}
2 mybatis注解介绍
mybatis3支持多个java注解,下面介绍几个基本的注解。
@Insert
@Insert("insert into t_user(name,age,gender) values (#{name},#{age},#{gender})")
public int addUser(User user) throws DataAccessException;
如上面的代码所示,使用@Insert注解可以在()中直接写sql语句,传参可以用#{}实现,其中name字段名直接对应User类中的name字段,肿么样,有没有很直观方便:)
@Select
@Select("select age,gender from t_user where name={name}")
public User queryUser(String name) throws DataAccessException;
@Select 注解所查询出的字段可以直接绑定到User类中对应的字段中,由于在类中可以import对应的类,免去了在xml中配置结果集的麻烦。
@SelectKey
@SelectKey(statement = "select max(id) from t_id", before = true, resultType = long.class, keyProperty = "id")
@Insert("insert into t_user(id,name,age,gender) values (#{id},#{name},#{age},#{gender})")
public int addUser(User user) throws DataAccessException;
@SelectKey注解用于查询一下需要的前置字段,如上面的代码所示先查出id字段的值,然后插入到t_user表中,一般配合@Insert和@Update
注解使用。