All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.iartisan.runtime.jdbc.DataSourceConfig Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
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.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
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.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

/**
 * 

* mybatis durid 配置 * * @author King * @since 2017/4/21 */ public class DataSourceConfig { @Autowired private Environment env; /** * 创建数据源 */ @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"))); sessionFactoryBean.setMapperLocations(resourcePatternResolver.getResources(env.getProperty("mybatis.mapper-locations")));//指定xml文件位置 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; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy