MybatisUtil.java
3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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();
}
}