目录
部分目录结构
pom依赖
数据源配置applicaction
配置文件
测试类
部分目录结构 pom依赖
2.1.3
3.4.5
3.4.1
......
com.zaxxer
HikariCP
${hikaricp.version}
mysql
mysql-connector-java
8.0.20
com.oracle
ojdbc8
12.2.0.1
org.mybatis.spring.boot
mybatis-spring-boot-starter
${mybatis-spring-boot.version}
com.baomidou
mybatis-plus-boot-starter
${mybatis-plus.version}
数据源配置applicaction
spring.datasource.msp.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.msp.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.msp.jdbc-url=jdbc:mysql://xxxxxx:3311/mymsphrms?useSSL=false&useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai
spring.datasource.msp.username=xxx
spring.datasource.msp.password=xxx
spring.datasource.hrps.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hrps.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.hrps.jdbc-url=jdbc:oracle:thin:@xxxxxx:1521:HCM91TST
spring.datasource.hrps.username=xxxx
spring.datasource.hrps.password=xxxx
配置文件
mysql配置 primary配置主数据源
package com.your.hm.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
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.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* description mysql
*
* @author zhuangxr002
* @date 2022/4/25 16:48
*/
@Configuration
@MapperScan(basePackages = "com.your.hm.mapper.msp", sqlSessionTemplateRef = "mspSqlSessionTemplate")
public class MSPDataSouceConfig {
@Primary
@Bean(name = "mspDataSource")
@ConfigurationProperties(prefix = "spring.datasource.msp")
public DataSource getMSPDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean("mspSqlSessionFactory")
public MybatisSqlSessionFactoryBean sqlSessionFactory(@Qualifier("mspDataSource") DataSource dataSource) throws Exception{
// 解决多数据源baseMapper 用问题 sqlSessionMapper==>MybatisSqlSessionMapper
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
//分页插件配置
bean.setPlugins(mybatisPlusInterceptor());
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setMapperLocations(resolver.getResources("classpath:mapper/msp/**/*.xml"));
return bean;
}
@Primary
@Bean("mspSqlSessionTemplate")
public SqlSessionTemplate mspSqlSessionTemplate(@Qualifier("mspSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean(name = "mspTransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("mspDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题
*/
@Bean(name = "mspMybatisPlusInterceptor")
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
oracle配置
package com.your.hm.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.incrementer.OracleKeyGenerator;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* description oracle数据源配置
*
* @author zhuangxr002
* @date 2022/4/25 16:48
*/
@Configuration
@MapperScan(basePackages = "com.your.hm.mapper.hrps", sqlSessionTemplateRef = "hrpsSqlSessionTemplate")
public class HRPSDataSouceConfig {
@Bean(name = "hrpsDataSource")
@ConfigurationProperties(prefix = "spring.datasource.hrps")
public DataSource getHRPSDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public GlobalConfig globalConfig() {
GlobalConfig conf = new GlobalConfig();
//主键序列
conf.setDbConfig(new GlobalConfig.DbConfig().setKeyGenerator(new OracleKeyGenerator()));
// globalConfig.setMetaObjectHandler(myMetaObjectHandler);
return conf;
}
@Bean("hrpsSqlSessionFactory")
public MybatisSqlSessionFactoryBean sqlSessionFactory(@Qualifier("hrpsDataSource") DataSource dataSource) throws Exception{
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
//分页插件配置
bean.setPlugins(mybatisPlusInterceptor());
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setMapperLocations(resolver.getResources("classpath:mapper/hrps/**/*.xml"));
bean.setGlobalConfig(globalConfig());
return bean;
}
@Bean(name = "hrpsTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("hrpsDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean("hrpsSqlSessionTemplate")
public SqlSessionTemplate hrpsSqlSessionTemplate(@Qualifier("hrpsSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题
*/
@Bean(name = "hrpsMybatisPlusInterceptor")
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.ORACLE));
return interceptor;
}
}
测试类
@Test
public void testt(){
Log log = logDao.selectById(1);
IPage page = new Page<>(1, 10);//参数一是当前页,参数二是每页个数
page = logDao.selectPage(page, new QueryWrapper<>());
System.out.println(page.getRecords().toString());
}
@Test
public void testt1(){
com.your.hm.entity.hrps.Log log = loggDao.selectById(51);
IPage page = new Page<>(1, 10);//参数一是当前页,参数二是每页个数
page = loggDao.selectPage(page, new QueryWrapper<>());
System.out.println(page.getRecords().toString());
System.out.println(log.toString());
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)