Skip to content

Commit eb07284

Browse files
committed
使用DataSourceHolder 获取当前数据源
1 parent 5c197a6 commit eb07284

File tree

15 files changed

+44
-461
lines changed

15 files changed

+44
-461
lines changed

hsweb-web-core/src/main/java/org/hsweb/web/core/datasource/DataSourceHolder.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,10 @@
1717
package org.hsweb.web.core.datasource;
1818

1919

20-
import org.springframework.beans.factory.annotation.Autowired;
21-
import org.springframework.stereotype.Component;
22-
23-
import javax.annotation.PostConstruct;
2420
import javax.sql.DataSource;
2521
import java.sql.Connection;
2622
import java.sql.SQLException;
2723

28-
@Component
2924
public class DataSourceHolder {
3025

3126
private static DynamicDataSource dynamicDataSource;
@@ -34,17 +29,12 @@ public class DataSourceHolder {
3429

3530
private static DatabaseType defaultDatabaseType;
3631

37-
@Autowired(required = false)
38-
private DataSource dataSource;
39-
40-
@PostConstruct
41-
public void init() throws SQLException {
32+
public void init(DataSource dataSource) throws SQLException {
4233
if (null != dataSource) {
4334
Connection connection = null;
4435
try {
4536
connection = dataSource.getConnection();
4637
install(dataSource, DatabaseType.fromJdbcUrl(connection.getMetaData().getURL()));
47-
dataSource = null;
4838
} finally {
4939
if (null != connection)
5040
connection.close();

hsweb-web-dao/hsweb-web-dao-mybatis/src/main/java/org/hsweb/web/mybatis/MyBatisAutoConfiguration.java

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,23 @@
1919
import org.apache.ibatis.mapping.DatabaseIdProvider;
2020
import org.apache.ibatis.plugin.Interceptor;
2121
import org.apache.ibatis.session.SqlSessionFactory;
22-
import org.hsweb.web.core.datasource.DataSourceHolder;
23-
import org.hsweb.web.datasource.dynamic.DynamicDataSourceAutoConfiguration;
24-
import org.hsweb.web.mybatis.MybatisProperties;
2522
import org.hsweb.web.mybatis.dynamic.DynamicDataSourceSqlSessionFactoryBuilder;
2623
import org.mybatis.spring.SqlSessionFactoryBean;
2724
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
2825
import org.springframework.beans.factory.annotation.Autowired;
29-
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
30-
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3127
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3228
import org.springframework.context.annotation.Bean;
3329
import org.springframework.context.annotation.Configuration;
3430
import org.springframework.core.io.DefaultResourceLoader;
35-
import org.springframework.core.io.Resource;
3631
import org.springframework.core.io.ResourceLoader;
37-
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
3832
import org.springframework.util.StringUtils;
3933

4034
import javax.sql.DataSource;
41-
import java.io.IOException;
42-
import java.sql.SQLException;
43-
import java.util.ArrayList;
44-
import java.util.Arrays;
45-
import java.util.List;
46-
import java.util.Properties;
4735

4836
@Configuration
49-
@AutoConfigureAfter(DynamicDataSourceAutoConfiguration.class)
5037
@EnableConfigurationProperties(MybatisProperties.class)
38+
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class})
5139
public class MyBatisAutoConfiguration {
5240

5341
@Autowired
@@ -60,17 +48,7 @@ public class MyBatisAutoConfiguration {
6048
private ResourceLoader resourceLoader = new DefaultResourceLoader();
6149

6250
@Autowired(required = false)
63-
private DatabaseIdProvider databaseIdProvider = new DatabaseIdProvider() {
64-
@Override
65-
public void setProperties(Properties p) {
66-
67-
}
68-
69-
@Override
70-
public String getDatabaseId(DataSource dataSource) throws SQLException {
71-
return DataSourceHolder.getActiveDatabaseType().name();
72-
}
73-
};
51+
private DatabaseIdProvider databaseIdProvider;
7452

7553
@Bean(name = "sqlSessionFactory")
7654
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {

hsweb-web-dao/hsweb-web-dao-mybatis/src/main/java/org/hsweb/web/mybatis/MybatisDaoAutoConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.hsweb.web.mybatis;
22

3-
import org.hsweb.web.datasource.dynamic.DynamicDataSourceAutoConfiguration;
43
import org.hsweb.web.mybatis.utils.ResultMapsUtils;
54
import org.mybatis.spring.SqlSessionTemplate;
65
import org.mybatis.spring.annotation.MapperScan;
@@ -15,7 +14,7 @@
1514
@Configuration
1615
@ComponentScan(basePackages = {"org.hsweb.web.mybatis"})
1716
@MapperScan(basePackages = {"org.hsweb.web.dao"})
18-
@AutoConfigureAfter(DynamicDataSourceAutoConfiguration.class)
17+
@AutoConfigureAfter(MyBatisAutoConfiguration.class)
1918
@EnableConfigurationProperties(MybatisProperties.class)
2019
public class MybatisDaoAutoConfiguration {
2120

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package org.hsweb.web.mybatis.plgins.pager;
22

33
import org.apache.ibatis.executor.Executor;
4-
import org.apache.ibatis.executor.resultset.ResultSetHandler;
5-
import org.apache.ibatis.executor.statement.RoutingStatementHandler;
64
import org.apache.ibatis.executor.statement.StatementHandler;
75
import org.apache.ibatis.mapping.MappedStatement;
86
import org.apache.ibatis.plugin.*;
@@ -11,11 +9,8 @@
119
import org.apache.ibatis.session.ResultHandler;
1210
import org.apache.ibatis.session.RowBounds;
1311
import org.hsweb.web.bean.common.QueryParam;
14-
import org.slf4j.Logger;
15-
import org.slf4j.LoggerFactory;
12+
import org.hsweb.web.core.datasource.DataSourceHolder;
1613
import org.springframework.beans.factory.annotation.Autowired;
17-
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
18-
import org.springframework.boot.context.properties.ConfigurationProperties;
1914
import org.springframework.context.ApplicationContext;
2015
import org.springframework.stereotype.Component;
2116

@@ -28,14 +23,10 @@
2823
RowBounds.class, ResultHandler.class})})
2924
@Component
3025
public class PagerInterceptor implements Interceptor {
31-
protected Logger logger = LoggerFactory.getLogger(this.getClass());
3226
protected Map<String, PagerHelper> pagerHelperBase = new HashMap<>();
3327

34-
protected String dialect = null;
3528
@Autowired
3629
private ApplicationContext context;
37-
@Autowired
38-
private DataSourceProperties properties;
3930

4031
@Override
4132
public Object intercept(Invocation target) throws Throwable {
@@ -51,7 +42,7 @@ public Object plugin(Object target) {
5142
Object obj = statementHandler.getParameterHandler().getParameterObject();
5243
if (obj instanceof QueryParam) {
5344
QueryParam param = (QueryParam) obj;
54-
PagerHelper helper = pagerHelperBase.get(dialect);
45+
PagerHelper helper = pagerHelperBase.get(getDialect());
5546
if (helper != null && param.isPaging() && !sql.contains("count(0)")) {
5647
String newSql = helper.doPaging(param, sql);
5748
metaStatementHandler.setValue("delegate.boundSql.sql", newSql);
@@ -63,26 +54,15 @@ public Object plugin(Object target) {
6354

6455
@Override
6556
public void setProperties(Properties properties) {
66-
System.out.println(properties);
6757
}
6858

6959
@PostConstruct
7060
public void init() {
7161
Map<String, PagerHelper> helperMap = context.getBeansOfType(PagerHelper.class);
7262
helperMap.forEach((name, helper) -> pagerHelperBase.put(helper.getDialect(), helper));
73-
dialect = getDialect();
7463
}
7564

7665
public String getDialect() {
77-
String url = properties.getDriverClassName();
78-
if (url.contains("mysql")) {
79-
return "mysql";
80-
} else if (url.contains("oracle")) {
81-
return "oracle";
82-
} else if (url.contains("h2")) {
83-
return "h2";
84-
} else
85-
logger.error("mybatis pager dialect not found!");
86-
return "undefine";
66+
return DataSourceHolder.getDefaultDatabaseType().name();
8767
}
8868
}

hsweb-web-oauth2/hsweb-web-oauth2-mybatis/src/main/resources/org/hsweb/web/dao/impl/mybatis/mapper/mysql/OAuth2AccessMapper.xml

Lines changed: 0 additions & 59 deletions
This file was deleted.

hsweb-web-oauth2/hsweb-web-oauth2-mybatis/src/main/resources/org/hsweb/web/dao/impl/mybatis/mapper/oracle/OAuth2ClientMapper.xml

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)