MybatisUtil.java 3.37 KB
package com.glsk.util;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

public class MybatisUtil {
    private static Logger logger = Logger.getLogger("MybatisUtil");


    private static AtomicInteger count = new AtomicInteger();

    public static void main(String[] args)throws  Exception{


        //数据源first
//        SqlSession sessionfirst = MybatisUtil.getsqlSession("first");
//        ShopmallGoodsMapper dj_smartcarlife = sessionfirst.getMapper(ShopmallGoodsMapper.class);
//        getsqlSession("gh_scrm_entmicro");


    }

    public static  String username ="";
    public static  String password ="";
    public static  String driverClassName ="";
    public static  String url ="";

    public static SqlSession getsqlSession(String propName) throws Exception {
        //获取相关信息
        username = ConfigurationManager.getProperty("spring.datasource."+propName+".username");
        password = ConfigurationManager.getProperty("spring.datasource."+propName+".password");
        driverClassName = ConfigurationManager.getProperty("spring.datasource."+propName+".driver-class-name");
        url = ConfigurationManager.getProperty("spring.datasource."+propName+".jdbc-url");

        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUsername(username);
        dataSource.setPassword(password);

        // 设置连接池配置以提高稳定性和性能
        dataSource.setInitialSize(3);
        dataSource.setMinIdle(5);
        dataSource.setMaxActive(20);
        
        // 设置连接超时时间
        dataSource.setMaxWait(60000);
        
        // 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
        dataSource.setTimeBetweenEvictionRunsMillis(60000);
        
        // 配置一个连接在池中最小生存的时间,单位是毫秒
        dataSource.setMinEvictableIdleTimeMillis(300000);
        
        // 用来检测连接是否有效的sql
        dataSource.setValidationQuery("SELECT 1");
        dataSource.setTestWhileIdle(true);
        dataSource.setTestOnBorrow(false);
        dataSource.setTestOnReturn(false);
        
        // 是否打开PSCache,并且指定每个连接上PSCache的大小
        dataSource.setPoolPreparedStatements(true);
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(20);
        
        // 配置监控统计拦截的filters
        try {
            dataSource.setFilters("stat");
        } catch (Exception e) {
            logger.warning("Failed to set druid filters: " + e.getMessage());
        }

        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);

//        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*/*"));
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*"));
        SqlSessionFactory sqlSessionFactory = bean.getObject();
        return sqlSessionFactory.openSession();
    }
}