数据库
package com.example.db.config;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class BaseDBConfig {
private String username;
private String url;
private String password;
private String driverClassName;
private boolean testOnBorrow;
private String validationQuery;
}
package com.example.db.config;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Getter
@Setter
@Configuration
@ConfigurationProperties(prefix = "spring.datasource")
public class DBDefaultConfig extends BaseDBConfig{
}
package com.example.db.config;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Getter
@Setter
@Configuration
@ConfigurationProperties(prefix = "db.dev")
public class DBDevConfig extends BaseDBConfig {
}
mybatis连接数据库
* @Primary 优先选择这个Bean,多数据源时必须通过@Primary配置主数据源
* @MapperScan 扫描 Mapper 接口,确定需要被扫描的包, 与其他数据进行区分
* sqlSessionFactoryRef 表示定义了 key ,表示一个唯一 SqlSessionFactory 实例
package com.example.db.config;
import com.alibaba.druid.pool.DruidDataSource;
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.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = {"com.example.db.dao.mybatis"},sqlSessionFactoryRef = "mybatisSqlSessionFactory")
public class DataSourceConfigMybtis {
@Autowired
DBDefaultConfig dbDefaultConfig;
@Primary
@Bean("mybatisDataSource")
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(dbDefaultConfig.getDriverClassName());
dataSource.setUsername(dbDefaultConfig.getUsername());
dataSource.setPassword(dbDefaultConfig.getPassword());
dataSource.setUrl(dbDefaultConfig.getUrl());
dataSource.setValidationQuery(dbDefaultConfig.getValidationQuery());
return dataSource;
}
@Bean("mybatisSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("mybatisDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*Mapper.xml"));
return sqlSessionFactoryBean.getObject();
}
@Bean("mybatisTransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("mybatisDataSource") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Bean("mybatisSqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("mybatisSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
JPA连接数据库
package com.example.db.config;
import com.alibaba.druid.pool.DruidDataSource;
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.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
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.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
@Configuration
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory",
transactionManagerRef = "jpaTransactionManager",
basePackages = {"com.example.db.dao.jpa"})
public class DataSourceConfigJPA {
@Autowired
private DBDevConfig dbDevConfig;
@Autowired
private JpaProperties jpaProperties;
@Bean("jpaDataSource")
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(dbDevConfig.getDriverClassName());
dataSource.setUsername(dbDevConfig.getUsername());
dataSource.setPassword(dbDevConfig.getPassword());
dataSource.setUrl(dbDefaultConfig.getUrl());
dataSource.setValidationQuery(dbDefaultConfig.getValidationQuery());
return dataSource;
}
@Bean("entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder entityManagerFactoryBuilder,
@Qualifier("jpaDataSource") DataSource dataSource){
return entityManagerFactoryBuilder.dataSource(dataSource)
.properties(jpaProperties.getProperties())
.packages("com.example.db.bean.jpa")
.build();
}
public EntityManager entityManager(@Qualifier("entityManagerFactory") LocalContainerEntityManagerFactoryBean entityManagerFactory){
return entityManagerFactory.getObject().createEntityManager();
}
@Bean("jpaTransactionManager")
public PlatformTransactionManager transactionManager(@Qualifier("entityManagerFactory") LocalContainerEntityManagerFactoryBean entityManagerFactory){
return new JpaTransactionManager(entityManagerFactory.getObject());
}
}
注意事项
- 在service层需要在@Transctional中添加transactionManager的名字,比如:@Transctional(transactionManager=“jpaTransactionManager”)