org.iartisan.runtime.jdbc.DataSourceConfig Maven / Gradle / Ivy
package org.iartisan.runtime.jdbc;
import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.iartisan.runtime.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
/**
*
* mybatis durid 配置
*
* @author King
* @since 2017/4/21
*/
public class DataSourceConfig {
@Autowired
private Environment env;
private final static String mybatis_mapper_locations_key = "mybatis.mapper-locations";
/**
* 创建数据源
*/
@Bean
@Primary
public DataSource getDataSource() throws Exception {
DruidDataSource datasource = new DruidDataSource();
datasource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
datasource.setUrl(env.getProperty("jdbc.url"));
datasource.setUsername(env.getProperty("jdbc.username"));
datasource.setPassword(env.getProperty("jdbc.password"));
List filters = new ArrayList<>();
filters.add(setWallFilter());
filters.add(setStatFilter());
datasource.setProxyFilters(filters);
return datasource;
}
/**
* 根据数据源创建SqlSessionFactory
*/
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource ds) throws Exception {
MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();
//指定数据源(这个必须有,否则报错)
sessionFactoryBean.setDataSource(ds);
//下边两句仅仅用于*.xml文件,如果整个持久层操作不需要使用到xml文件的话(只用注解就可以搞定),则不加
//指定基包
sessionFactoryBean.setTypeAliasesPackage(env.getProperty("mybatis.type-aliases-package"));
//配置文件位置
PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
sessionFactoryBean.setConfigLocation(resourcePatternResolver.getResource(env.getProperty("mybatis.config-location")));
//指定xml文件位置
if (StringUtils.isNotEmpty(env.getProperty(mybatis_mapper_locations_key))) {
String[] locals = env.getProperty(mybatis_mapper_locations_key).split(",");
List resourceList = new ArrayList<>();
for (String local : locals) {
Resource[] resources = resourcePatternResolver.getResources(local);
resourceList.addAll(Arrays.asList(resources));
}
sessionFactoryBean.setMapperLocations(resourceList.toArray(new Resource[]{}));
}
Properties configProperties = new Properties();
configProperties.put("prefix", "");
configProperties.put("blobType", env.getProperty("flowable.mybatis.JdbcType.blobType"));
configProperties.put("boolValue", env.getProperty("flowable.mybatis.JdbcType.boolValue"));
sessionFactoryBean.setConfigurationProperties(configProperties);
return sessionFactoryBean.getObject();
}
@Bean
public ServletRegistrationBean druidServlet() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.setServlet(new StatViewServlet());
servletRegistrationBean.addUrlMappings("/druid/*");
return servletRegistrationBean;
}
public WallFilter setWallFilter() {
WallFilter wallFilter = new WallFilter();
WallConfig config = new WallConfig();
config.setMultiStatementAllow(true);
wallFilter.setConfig(config);
return wallFilter;
}
public StatFilter setStatFilter() {
StatFilter statFilter = new StatFilter();
statFilter.setLogSlowSql(true);
statFilter.setMergeSql(true);
statFilter.setSlowSqlMillis(1000);
return statFilter;
}
}