Skip to content

Commit

Permalink
优化数据源调用
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Jul 2, 2017
1 parent 5c9785a commit 08265f7
Show file tree
Hide file tree
Showing 17 changed files with 92 additions and 332 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.hswebframework.web</groupId>
<artifactId>hsweb-datasource-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.TransactionIsolationLevel;
import org.apache.ibatis.transaction.Transaction;
import org.hsweb.ezorm.rdb.executor.AbstractJdbcSqlExecutor;
import org.hsweb.ezorm.rdb.executor.SqlExecutor;
import org.hswebframework.web.commons.entity.factory.EntityFactory;
import org.hswebframework.web.dao.datasource.DataSourceHolder;
import org.hswebframework.web.dao.datasource.DatabaseType;
import org.hswebframework.web.dao.mybatis.dynamic.DynamicDataSourceSqlSessionFactoryBuilder;
import org.hswebframework.web.dao.mybatis.dynamic.DynamicSpringManagedTransaction;
import org.mybatis.spring.SqlSessionFactoryBean;
Expand All @@ -36,21 +32,17 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@Configuration
@EnableConfigurationProperties(MybatisProperties.class)
Expand Down Expand Up @@ -116,14 +108,5 @@ public Transaction newTransaction(DataSource dataSource, TransactionIsolationLev
return factory.getObject();
}

@Bean
@ConditionalOnMissingBean(SqlExecutor.class)
public SqlExecutor sqlExecutor(DataSource dataSource) {
try (Connection connection = dataSource.getConnection()) {
DataSourceHolder.install(dataSource, DatabaseType.fromJdbcUrl(connection.getMetaData().getURL()));
} catch (Exception e) {
throw new RuntimeException(e);
}
return new DefaultJdbcExecutor(dataSource);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,21 @@
import org.hsweb.ezorm.rdb.render.support.simple.CommonSqlRender;
import org.hsweb.ezorm.rdb.render.support.simple.SimpleWhereSqlBuilder;
import org.hswebframework.web.BusinessException;
import org.hswebframework.web.dao.datasource.DataSourceHolder;
import org.hswebframework.web.dao.datasource.DatabaseType;
import org.hswebframework.web.dao.mybatis.plgins.pager.Pager;
import org.hswebframework.web.dao.mybatis.utils.ResultMapsUtils;
import org.hswebframework.utils.StringUtils;
import org.hswebframework.web.datasource.DataSourceHolder;
import org.hswebframework.web.datasource.DatabaseType;

import java.sql.JDBCType;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
* 使用easyorm 动态构建 sql
* @author zhouhao
* @TODO
* @since 2.0
*/
public class EasyOrmSqlBuilder {

Expand Down Expand Up @@ -112,7 +113,7 @@ public Map<String, RDBTableMetaData> get(Object key) {
};

public RDBDatabaseMetaData getActiveDatabase() {
DatabaseType type = DataSourceHolder.getActiveDatabaseType();
DatabaseType type = DataSourceHolder.currentDatabaseType();
switch (type) {
case h2:
return h2;
Expand Down Expand Up @@ -172,8 +173,8 @@ public String buildUpdateFields(String resultMapId, String tableName, UpdatePara
SqlAppender appender = new SqlAppender();
columns.forEach(column -> {
RDBColumnMetaData columnMetaData = column.getRDBColumnMetaData();
if (columnMetaData.getName().contains(".")) return;
if (columnMetaData == null) return;
if (columnMetaData.getName().contains(".")) return;
try {
Object tmp = propertyUtils.getProperty(param.getData(), columnMetaData.getAlias());
if (tmp == null) return;
Expand Down Expand Up @@ -305,15 +306,15 @@ class OracleMeta extends OracleRDBDatabaseMetaData {
public OracleMeta() {
super();
renderMap.put(SqlRender.TYPE.INSERT, new InsertSqlBuilder());
renderMap.put(SqlRender.TYPE.UPDATE, new UpdateSqlBuilder(Dialect.MYSQL));
renderMap.put(SqlRender.TYPE.UPDATE, new UpdateSqlBuilder(Dialect.ORACLE));
}
}

class H2Meta extends H2RDBDatabaseMetaData {
public H2Meta() {
super();
renderMap.put(SqlRender.TYPE.INSERT, new InsertSqlBuilder());
renderMap.put(SqlRender.TYPE.UPDATE, new UpdateSqlBuilder(Dialect.MYSQL));
renderMap.put(SqlRender.TYPE.UPDATE, new UpdateSqlBuilder(Dialect.H2));
}
}
}
Loading

0 comments on commit 08265f7

Please sign in to comment.