浏览代码

优化mybatis

zhou-hao 7 年之前
父节点
当前提交
65fe048176

+ 2 - 2
hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/src/main/java/org/hswebframework/web/dao/mybatis/MyBatisAutoConfiguration.java

@@ -27,7 +27,6 @@ import org.hswebframework.web.commons.entity.factory.EntityFactory;
 import org.hswebframework.web.dao.mybatis.builder.EasyOrmSqlBuilder;
 import org.hswebframework.web.dao.mybatis.dynamic.DynamicDataSourceSqlSessionFactoryBuilder;
 import org.hswebframework.web.dao.mybatis.dynamic.DynamicSpringManagedTransaction;
-import org.hswebframework.web.dao.mybatis.utils.ResultMapsUtils;
 import org.mybatis.spring.SqlSessionFactoryBean;
 import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
 import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
@@ -110,10 +109,11 @@ public class MyBatisAutoConfiguration {
         factory.setMapperLocations(mybatisProperties.resolveMapperLocations());
 
         SqlSessionFactory sqlSessionFactory = factory.getObject();
+        MybatisUtils.sqlSession=sqlSessionFactory;
+
         EnumDictHandlerRegister.typeHandlerRegistry = sqlSessionFactory.getConfiguration().getTypeHandlerRegistry();
         EnumDictHandlerRegister.register("org.hswebframework.web;" + mybatisProperties.getTypeHandlersPackage());
 
-        ResultMapsUtils.setSqlSession(sqlSessionFactory);
         try {
             Class.forName("javax.persistence.Table");
             EasyOrmSqlBuilder.getInstance().useJpa = mybatisProperties.isUseJpa();

+ 0 - 5
hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/src/main/java/org/hswebframework/web/dao/mybatis/MybatisDaoAutoConfiguration.java

@@ -19,17 +19,12 @@
 package org.hswebframework.web.dao.mybatis;
 
 import org.hswebframework.web.dao.Dao;
-import org.hswebframework.web.dao.mybatis.utils.ResultMapsUtils;
-import org.mybatis.spring.SqlSessionTemplate;
 import org.mybatis.spring.annotation.MapperScan;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.AutoConfigureAfter;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
 
-import javax.annotation.PostConstruct;
-
 @Configuration
 @ComponentScan("org.hswebframework.web.dao.mybatis")
 @MapperScan(value = "org.hswebframework.web.dao", markerInterface = Dao.class)

+ 42 - 0
hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/src/main/java/org/hswebframework/web/dao/mybatis/MybatisUtils.java

@@ -0,0 +1,42 @@
+/*
+ *
+ *  * Copyright 2016 http://www.hswebframework.org
+ *  *
+ *  * Licensed under the Apache License, Version 2.0 (the "License");
+ *  * you may not use this file except in compliance with the License.
+ *  * You may obtain a copy of the License at
+ *  *
+ *  *     http://www.apache.org/licenses/LICENSE-2.0
+ *  *
+ *  * Unless required by applicable law or agreed to in writing, software
+ *  * distributed under the License is distributed on an "AS IS" BASIS,
+ *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  * See the License for the specific language governing permissions and
+ *  * limitations under the License.
+ *
+ */
+
+package org.hswebframework.web.dao.mybatis;
+
+import org.apache.ibatis.mapping.ResultMap;
+import org.apache.ibatis.session.SqlSessionFactory;
+
+import java.util.concurrent.CountDownLatch;
+
+/**
+ * @since 2.0
+ */
+public class MybatisUtils {
+    volatile static SqlSessionFactory sqlSession;
+
+    public static ResultMap getResultMap(String id) {
+        return getSqlSession().getConfiguration().getResultMap(id);
+    }
+
+    public static SqlSessionFactory getSqlSession() {
+        if (sqlSession == null) {
+            throw new UnsupportedOperationException("sqlSession is null");
+        }
+        return sqlSession;
+    }
+}

+ 2 - 2
hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/src/main/java/org/hswebframework/web/dao/mybatis/builder/EasyOrmSqlBuilder.java

@@ -45,7 +45,7 @@ import org.hswebframework.web.BusinessException;
 import org.hswebframework.web.commons.entity.factory.EntityFactory;
 import org.hswebframework.web.dao.mybatis.builder.jpa.JpaAnnotationParser;
 import org.hswebframework.web.dao.mybatis.plgins.pager.Pager;
-import org.hswebframework.web.dao.mybatis.utils.ResultMapsUtils;
+import org.hswebframework.web.dao.mybatis.MybatisUtils;
 import org.hswebframework.utils.StringUtils;
 import org.hswebframework.web.datasource.DataSourceHolder;
 import org.hswebframework.web.datasource.DatabaseType;
@@ -146,7 +146,7 @@ public class EasyOrmSqlBuilder {
             return cached;
         }
         RDBTableMetaData rdbTableMetaData = new RDBTableMetaData();
-        ResultMap resultMaps = ResultMapsUtils.getResultMap(resultMapId);
+        ResultMap resultMaps = MybatisUtils.getResultMap(resultMapId);
         rdbTableMetaData.setName(tableName);
         rdbTableMetaData.setDatabaseMetaData(active);
 

+ 0 - 58
hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/src/main/java/org/hswebframework/web/dao/mybatis/utils/ResultMapsUtils.java

@@ -1,58 +0,0 @@
-/*
- *
- *  * Copyright 2016 http://www.hswebframework.org
- *  *
- *  * Licensed under the Apache License, Version 2.0 (the "License");
- *  * you may not use this file except in compliance with the License.
- *  * You may obtain a copy of the License at
- *  *
- *  *     http://www.apache.org/licenses/LICENSE-2.0
- *  *
- *  * Unless required by applicable law or agreed to in writing, software
- *  * distributed under the License is distributed on an "AS IS" BASIS,
- *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  * See the License for the specific language governing permissions and
- *  * limitations under the License.
- *
- */
-
-package org.hswebframework.web.dao.mybatis.utils;
-
-import org.apache.ibatis.mapping.ResultMap;
-import org.apache.ibatis.session.SqlSession;
-import org.apache.ibatis.session.SqlSessionFactory;
-import org.mybatis.spring.SqlSessionTemplate;
-
-import java.util.concurrent.CountDownLatch;
-
-/**
- * @since 2.0
- */
-public class ResultMapsUtils {
-    private volatile static SqlSessionFactory sqlSession;
-
-    private static CountDownLatch countDownLatch = new CountDownLatch(1);
-
-    public static ResultMap getResultMap(String id) {
-        if (sqlSession == null) {
-            try {
-                countDownLatch.await();
-            } catch (InterruptedException e) {
-                Thread.currentThread().interrupt();
-                throw new UnsupportedOperationException(e);
-            }
-            if (sqlSession == null) {
-                throw new UnsupportedOperationException("sqlSession is null");
-            }
-        }
-
-        return sqlSession.getConfiguration().getResultMap(id);
-    }
-
-    public static void setSqlSession(SqlSessionFactory sqlSession) {
-        ResultMapsUtils.sqlSession = sqlSession;
-        if (countDownLatch.getCount() != 0) {
-            countDownLatch.countDown();
-        }
-    }
-}