1 注释启动 实体 dao,mapper扫描注解
//@EnableJpaRepositories(basePackages = {"xxx"})
//@EntityScan(basePackages = {"xxx"})
@SpringBootApplication(scanBasePackages = {"xxx"})
//@MapperScan(basePackages = "xxx)
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
配置数据源(可以根据实际情况配置jpa或mybatis 本例两者都配置了)
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.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
@Bean("mysqlDataSource")
@Primary
@ConfigurationProperties("spring.datasource.msql")
DataSource mysqlDataSource() {
return DataSourceBuilder.create().build();
}
@Bean("pgDataSource")
@ConfigurationProperties("spring.datasource.pgsql")
DataSource pgDataSource() {
return DataSourceBuilder.create().build();
}
@Bean("mysqlJdbcTemplate")
public JdbcTemplate mysqlJdbcTemplate(@Qualifier("mysqlDataSource") DataSource mysqlDataSource){
return new JdbcTemplate(mysqlDataSource);
}
@Bean("pgJdbcTemplate")
public JdbcTemplate pgJdbcTemplate(@Qualifier("pgDataSource") DataSource pgDataSource){
return new JdbcTemplate(pgDataSource);
}
}
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.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
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.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.HashMap;
/**
* mysql数据源 jpa ,mybatis
*/
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = "mysql数据源mapper文件包路径",sqlSessionFactoryRef = "mySqlSessionFactory")
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactoryMysql",
transactionManagerRef = "transactionManagerMysql",
basePackages = {"XXX"}) //设置Repository所在位置,两个数据库对应的repository和实体类需要不同的路径
public class MysqlConfig {
@Autowired
@Qualifier("mysqlDataSource")
private DataSource mysqlDataSource;
//方言
private String mysqlDialect="org.hibernate.dialect.MySQL57Dialect";
//表操作
private String mysq_ddl_auto="update";
private String mysql_show_sql="true";
@Bean(name = "entityManagerMysql")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryMysql(builder).getObject().createEntityManager();
}
@Bean(name = "entityManagerFactoryMysql")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryMysql(EntityManagerFactoryBuilder builder) {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(mysqlDataSource);
em.setPackagesToScan(new String[] {"mysql数据源实体包路径" });
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto",mysq_ddl_auto);
properties.put("hibernate.dialect",mysqlDialect);
properties.put("hibernate.show_sql",mysql_show_sql);
properties.put("hibernate.database","mysql");
em.setJpaPropertyMap(properties);
return em;
}
@Bean("mySqlSessionFactory")
public SqlSessionFactory db1SqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/msql/*.xml"));
return bean.getObject();
}
@Bean("mySqlSessionTemplate")
public SqlSessionTemplate db2SqlSessionTemplate(@Qualifier("mySqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
//事务管理器
@Bean(name = "transactionManagerMysql")
PlatformTransactionManager transactionManagerMysql(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryMysql(builder).getObject());
}
}
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.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.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.HashMap;
/**
* postgresql数据源
*/
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = "postgresql数据源mapper文件包路径",sqlSessionFactoryRef = "pgSqlSessionFactory")
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactoryPg",
transactionManagerRef = "transactionManagerPg",
basePackages = {"XXX"}) //postgresql数据源设置Repository所在位置
public class PgConfig {
@Autowired
@Qualifier("pgDataSource")
private DataSource pgDataSource;
//方言
private String pgDialect="org.hibernate.dialect.PostgreSQLDialect";
//表操作
private String pg_ddl_auto="update";
private String pg_show_sql="true";
@Primary
@Bean(name = "entityManagerPg")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryPg(builder).getObject().createEntityManager();
}
@Primary
@Bean(name = "entityManagerFactoryPg")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPg(EntityManagerFactoryBuilder builder) {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(pgDataSource);
em.setPackagesToScan(new String[] { "postgresql数据源实体包路径" });
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto",pg_ddl_auto);
properties.put("hibernate.dialect",pgDialect);
properties.put("hibernate.show_sql",pg_show_sql);
properties.put("hibernate.database","postgresql");
em.setJpaPropertyMap(properties);
return em;
}
@Bean("pgSqlSessionFactory")
public SqlSessionFactory db1SqlSessionFactory(@Qualifier("pgDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/pgsql/*.xml"));
return bean.getObject();
}
@Bean("pgSqlSessionTemplate")
public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("pgSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
@Primary
@Bean(name = "transactionManagerPg")
public PlatformTransactionManager transactionManagerPg(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryPg(builder).getObject());
}
}