基于springboot配置多数据源,同时支持jpa和mybatis

大致的步骤分为以下几部:

一、搭建springboot环境,这里不做介绍。

二、假设需要同时支持mysql和oracle数据库,我的配置文件如下:

基于springboot配置多数据源,同时支持jpa和mybatis_第1张图片

三、通过spring加载多数据源,代码如下:

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 javax.sql.DataSource;

@Configuration

public class DataSourceConfig {

@Bean(name ="jpaDataSource")

@Qualifier("jpaDataSource")

@Primary

    @ConfigurationProperties(prefix ="spring.datasource.mysql")

public DataSource jpaDataSource() {

return DataSourceBuilder.create().build();

}

@Bean(name ="mybatisDataSource")

@Qualifier("mybatisDataSource")

@ConfigurationProperties(prefix ="spring.datasource.mysql")

public DataSource mybatisDataSource() {

return DataSourceBuilder.create().build();

}

}

至此,jpa和mybatis均能加载,接下来分表配置jap和mybatis各自的启动类。

四、jpa启动类


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.beans.factory.config.BeanPostProcessor;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Primary;

import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import org.springframework.orm.jpa.JpaTransactionManager;

import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;

import org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor;

import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

import org.springframework.transaction.PlatformTransactionManager;

import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;

import javax.persistence.EntityManagerFactory;

import javax.sql.DataSource;

/**

* Created by dingwei.

**/

@Configuration

@EnableJpaRepositories(entityManagerFactoryRef ="jpaEntityManagerFactory",

transactionManagerRef ="jpaTransactionManager",

basePackages ="com.ztsmart.search.core.repository")

@EnableTransactionManagement

public class JPAConfig {

@Autowired

    @Qualifier("jpaDataSource")

private DataSourcejpaDataSource;

@Bean(name ="jpaEntityManager")

@Primary

    public EntityManager entityManager() {

return entityManagerFactory().getObject().createEntityManager();

}

@Bean(name ="jpaEntityManagerFactory")

@Primary

    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

HibernateJpaVendorAdapter japVendor =new HibernateJpaVendorAdapter();

japVendor.setGenerateDdl(false);

LocalContainerEntityManagerFactoryBean entityManagerFactory =new LocalContainerEntityManagerFactoryBean();

entityManagerFactory.setDataSource(jpaDataSource);

entityManagerFactory.setJpaVendorAdapter(japVendor);

entityManagerFactory.setPackagesToScan("com.ztsmart.search.*.entity");

return entityManagerFactory;

}

@Bean(name ="jpaTransactionManager")

@Primary

    public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {

JpaTransactionManager manager =new JpaTransactionManager();

manager.setEntityManagerFactory(entityManagerFactory);

return manager;

}

@Bean

    public BeanPostProcessor persistenceTranslation() {

return new PersistenceAnnotationBeanPostProcessor();

}

}

注:basePackages为你的repository所在包,可配置多个包;PackagesToScan必须扫描到实体类。

四、mybatis启动类


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.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

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 org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration

@MapperScan(basePackages ="com.ztsmart.search.*.mapper", sqlSessionFactoryRef ="mybatisSessionFactory")

@EnableTransactionManagement

public class MybatisDataSourceConfig {

private static final StringMAPPER_LOCATION ="classpath*:mybatis/*.xml";

@Autowired

    @Qualifier("mybatisDataSource")

private DataSourcemybatisDataSource;

@Bean(name ="mybatisTransactionManager")

public DataSourceTransactionManager masterTransactionManager() {

return new DataSourceTransactionManager(mybatisDataSource);

}

@Bean(name ="mybatisSessionFactory")

public SqlSessionFactory mybatisSessionFactory()

throws Exception {

//解决查询返回结果含null没有对应字段值问题

        org.apache.ibatis.session.Configuration configuration =new org.apache.ibatis.session.Configuration();

configuration.setCallSettersOnNulls(true);

//configuration.setMapUnderscoreToCamelCase(true);

        final SqlSessionFactoryBean sessionFactory =new SqlSessionFactoryBean();

sessionFactory.setDataSource(mybatisDataSource);

sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()

.getResources(MybatisDataSourceConfig.MAPPER_LOCATION));

sessionFactory.setConfiguration(configuration);

return sessionFactory.getObject();

}

@Bean

    public SqlSessionTemplate sqlSessionTemplate()throws Exception {

return new SqlSessionTemplate(mybatisSessionFactory());

}

}

注:这里basePackages需扫描到相关的mapper,可配置多个。相关的xml文件必须放置到resources/mybatis文件中,这样各模块下的xml均能扫描。

至此,支持jap和mybatis配置完成,如果需同时支持多数源需修改第三步加载配置源启动类即可。

你可能感兴趣的:(基于springboot配置多数据源,同时支持jpa和mybatis)