application.yml
spring:
data:
mongodb:
uri: mongodb://localhost:27017/dbname
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/dbname?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8
username: root
password: 111
initial-size: 10
max-active: 100
min-idle: 10
max-wait: 60000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
test-while-idle: true
test-on-borrow: false
test-on-return: false
stat-view-servlet:
enabled: true
url-pattern: /druid/*
#login-username: admin
#login-password: admin
filter:
stat:
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: false
wall:
config:
multi-statement-allow: true
clickhouse:
driverClassName: ru.yandex.clickhouse.ClickHouseDriver
url: jdbc:clickhouse://localhost:8123/dbname?serverTimezone=UTC&max_query_size=100000000
username: default
password: 1111
testWhileIdle: true
validationQuery: SELECT 1
initialSize: 10
maxActive: 100
minIdle: 10
maxWait: 60000
jar:
<!--mongodb-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>ru.yandex.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.2.4</version>
</dependency>
MyBatisConfig
package xxx.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
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.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* 连接 smart_device 数据库
*/
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = "{com.xxx.mapper}", sqlSessionTemplateRef = "sqlSessionTemplate1")
public class MyBatisConfig {
private static final Logger logger = LoggerFactory.getLogger(MyBatisConfig.class);
@Autowired
@Qualifier("druidDataSource1")
private DruidDataSource dataSource;
@Bean(name = "druidDataSource1",destroyMethod = "close", initMethod = "init")
@ConfigurationProperties(prefix = "spring.datasource.druid")
@Primary
public DruidDataSource druidDataSource() {
return DruidDataSourceBuilder.create().build();
}
/**因为用的是mybatis-pus所以需要返回MybatisSqlSessionFactoryBean ,如果只是mybatis返回SqlSessionFactory类型的Bean**/
@Bean(name = "sqlSessionFactory1")
@Primary
public MybatisSqlSessionFactoryBean sqlSessionFactoryBean() {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setTypeAliasesPackage("youbang.sub.b2b.domain");
//添加XML目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
bean.setMapperLocations(resolver.getResources("classpath:mapper/*Mapper.xml"));
return bean;
} catch (Exception e) {
logger.error(e.getMessage());
throw new RuntimeException(e);
}
}
@Bean(name = "sqlSessionTemplate1")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("sqlSessionFactory1") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean(name = "transactionManager1")
@Primary
public DataSourceTransactionManager transactionManager1() {
return new DataSourceTransactionManager(dataSource);
}
}
clickhouse:
ClickHouseJdbcParamConfig:
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Data
@Configuration
@ConfigurationProperties(prefix = "spring.datasource.clickhouse")
public class ClickHouseJdbcParamConfig {
private String driverClassName;
private String url;
private String username;
private String password;
private Integer initialSize;
private Integer maxActive;
private Integer minIdle;
private Integer maxWait;
private Boolean testWhileIdle;
private String validationQuery;
}
ClickHouseConfig:
package xxx.config.clickhouse;
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.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = "com.xxx.clickhouseMapper", sqlSessionTemplateRef = "sqlSessionTemplate2")
public class ClickHouseConfig {
@Autowired
private ClickHouseJdbcParamConfig jdbcParamConfig;
@Bean("clickDataSource")
public DruidDataSource dataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(jdbcParamConfig.getUrl());
datasource.setDriverClassName(jdbcParamConfig.getDriverClassName());
datasource.setUsername(jdbcParamConfig.getUsername());
datasource.setPassword(jdbcParamConfig.getPassword());
datasource.setTestWhileIdle(jdbcParamConfig.getTestWhileIdle());
datasource.setValidationQuery(jdbcParamConfig.getValidationQuery());
datasource.setInitialSize(jdbcParamConfig.getInitialSize());
datasource.setMinIdle(jdbcParamConfig.getMinIdle());
datasource.setMaxActive(jdbcParamConfig.getMaxActive());
datasource.setMaxWait(jdbcParamConfig.getMaxWait());
return datasource;
}
@Bean("clickTransactionManager")
public DataSourceTransactionManager getDataSourceTransactionManager(
@Qualifier("clickDataSource") DruidDataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean("clickhouseSqlSessionFactory")
public SqlSessionFactory getSqlSessionManager(@Qualifier("clickDataSource") DruidDataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
Resource[] resource = new PathMatchingResourcePatternResolver()
.getResources("classpath:clickhouse/*Mapper.xml");
bean.setMapperLocations(resource);
return bean.getObject();
}
@Bean(name = "sqlSessionTemplate2")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("clickhouseSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}