zhouhao 8 rokov pred
rodič
commit
5f588b2c0b

+ 19 - 5
hsweb-web-dao/hsweb-web-dao-mybatis/pom.xml

@@ -17,6 +17,20 @@
     </properties>
 
     <dependencies>
+
+        <!--test-->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>druid</artifactId>
+            <version>1.0.26</version>
+            <scope>test</scope>
+        </dependency>
+
         <dependency>
             <groupId>org.hsweb</groupId>
             <artifactId>hsweb-web-dao-api</artifactId>
@@ -32,11 +46,11 @@
             <artifactId>spring-boot-starter-jdbc</artifactId>
         </dependency>
 
-        <dependency>
-            <groupId>org.hsweb</groupId>
-            <artifactId>hsweb-web-datasource</artifactId>
-            <optional>true</optional>
-        </dependency>
+        <!--<dependency>-->
+            <!--<groupId>org.hsweb</groupId>-->
+            <!--<artifactId>hsweb-web-datasource</artifactId>-->
+            <!--<optional>true</optional>-->
+        <!--</dependency>-->
 
         <!-- mybatis start-->
         <dependency>

+ 28 - 21
hsweb-web-dao/hsweb-web-dao-mybatis/src/main/java/org/hsweb/web/mybatis/MybatisProperties.java

@@ -23,20 +23,36 @@ import java.io.IOException;
 import java.util.*;
 import java.util.stream.Collectors;
 
+/**
+ * mybatis配置,继承官方配置类,增加一些属性以拓展更多功能
+ * <ul>
+ * <li>是否启用动态数据源{@link this#dynamicDatasource}</li>
+ * <li>可设置不加载的配置{@link this#mapperLocationExcludes}</li>
+ * </ul>
+ *
+ * @author zhouhao
+ * @see org.mybatis.spring.boot.autoconfigure.MybatisProperties
+ * @since 2.1
+ */
 public class MybatisProperties extends org.mybatis.spring.boot.autoconfigure.MybatisProperties {
+    /**
+     * 默认支持的hsweb mapper
+     */
     private static final String   defaultMapperLocation  = "classpath*:org/hsweb/web/dao/impl/mybatis/mapper/**/*.xml";
-    private              String   type                   = "oracle";
+    /**
+     * 是否启用动态数据源
+     * 启用后调用{@link org.hsweb.web.core.datasource.DynamicDataSource#use(String)},mybatis也会进行数据源切换
+     *
+     * @see org.hsweb.web.core.datasource.DynamicDataSource
+     */
     private              boolean  dynamicDatasource      = false;
+    /**
+     * 排除加载的mapper.xml
+     * 想自定义mapper并覆盖原始mapper的场景下,通过设置此属性来排除配置文件。
+     * 排除使用{@link Resource#getURL()#toString()}进行对比
+     */
     private              String[] mapperLocationExcludes = null;
 
-    public String getType() {
-        return type;
-    }
-
-    public void setType(String type) {
-        this.type = type;
-    }
-
     public String[] getMapperLocationExcludes() {
         return mapperLocationExcludes;
     }
@@ -53,11 +69,6 @@ public class MybatisProperties extends org.mybatis.spring.boot.autoconfigure.Myb
         this.dynamicDatasource = dynamicDatasource;
     }
 
-    @Override
-    public String[] getMapperLocations() {
-        return super.getMapperLocations();
-    }
-
     public Resource[] resolveMapperLocations() {
         Map<String, Resource> resources = new HashMap<>();
         Set<String> locations;
@@ -77,9 +88,8 @@ public class MybatisProperties extends org.mybatis.spring.boot.autoconfigure.Myb
             } catch (IOException e) {
             }
         }
-        if (mapperLocationExcludes != null && mapperLocationExcludes.length > 0)
-
-        {
+        //排除不需要的配置
+        if (mapperLocationExcludes != null && mapperLocationExcludes.length > 0) {
             for (String mapperLocationExclude : mapperLocationExcludes) {
                 try {
                     Resource[] excludesMappers = new PathMatchingResourcePatternResolver().getResources(mapperLocationExclude);
@@ -90,11 +100,8 @@ public class MybatisProperties extends org.mybatis.spring.boot.autoconfigure.Myb
                 }
             }
         }
-
         Resource[] mapperLocations = new Resource[resources.size()];
-        mapperLocations = resources.values().
-
-                toArray(mapperLocations);
+        mapperLocations = resources.values().toArray(mapperLocations);
         return mapperLocations;
     }
 

+ 151 - 0
hsweb-web-dao/hsweb-web-dao-mybatis/src/main/resources/META-INF/spring-configuration-metadata.json

@@ -0,0 +1,151 @@
+{
+  "groups": [
+    {
+      "name": "mybatis",
+      "type": "org.hsweb.web.mybatis.MybatisProperties",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    }
+  ],
+  "properties": [
+    {
+      "name": "mybatis.dynamic-datasource",
+      "type": "java.lang.Boolean",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties",
+      "description": "enable dynamicDatasource."
+    },
+    {
+      "name": "mybatis.mapper-location-excludes",
+      "type": "java.lang.String[]",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties",
+      "description": "exclude mapperLocations."
+    },
+    {
+      "name": "mybatis.check-config-location",
+      "type": "java.lang.Boolean",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    },
+    {
+      "name": "mybatis.check-config-location",
+      "type": "java.lang.Boolean",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    },
+    {
+      "name": "mybatis.check-config-location",
+      "type": "java.lang.Boolean",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    },
+    {
+      "name": "mybatis.check-config-location",
+      "type": "java.lang.Boolean",
+      "description": "Check the config file exists.",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties",
+      "defaultValue": false
+    },
+    {
+      "name": "mybatis.config",
+      "type": "java.lang.String",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    },
+    {
+      "name": "mybatis.config",
+      "type": "java.lang.String",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    },
+    {
+      "name": "mybatis.config",
+      "type": "java.lang.String",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    },
+    {
+      "name": "mybatis.config",
+      "type": "java.lang.String",
+      "description": "Config file path.",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    },
+    {
+      "name": "mybatis.executor-type",
+      "type": "org.apache.ibatis.session.ExecutorType",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    },
+    {
+      "name": "mybatis.executor-type",
+      "type": "org.apache.ibatis.session.ExecutorType",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    },
+    {
+      "name": "mybatis.executor-type",
+      "type": "org.apache.ibatis.session.ExecutorType",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    },
+    {
+      "name": "mybatis.executor-type",
+      "type": "org.apache.ibatis.session.ExecutorType",
+      "description": "Execution mode.",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    },
+    {
+      "name": "mybatis.mapper-locations",
+      "type": "java.lang.String[]",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    },
+    {
+      "name": "mybatis.mapper-locations",
+      "type": "java.lang.String[]",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    },
+    {
+      "name": "mybatis.mapper-locations",
+      "type": "java.lang.String[]",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    },
+    {
+      "name": "mybatis.mapper-locations",
+      "type": "java.lang.String[]",
+      "description": "Location of mybatis mapper files.",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    },
+    {
+      "name": "mybatis.type-aliases-package",
+      "type": "java.lang.String",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    },
+    {
+      "name": "mybatis.type-aliases-package",
+      "type": "java.lang.String",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    },
+    {
+      "name": "mybatis.type-aliases-package",
+      "type": "java.lang.String",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    },
+    {
+      "name": "mybatis.type-aliases-package",
+      "type": "java.lang.String",
+      "description": "Package to scan domain objects.",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    },
+    {
+      "name": "mybatis.type-handlers-package",
+      "type": "java.lang.String",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    },
+    {
+      "name": "mybatis.type-handlers-package",
+      "type": "java.lang.String",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    },
+    {
+      "name": "mybatis.type-handlers-package",
+      "type": "java.lang.String",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    },
+    {
+      "name": "mybatis.type-handlers-package",
+      "type": "java.lang.String",
+      "description": "Package to scan handlers.",
+      "sourceType": "org.hsweb.web.mybatis.MybatisProperties"
+    }
+  ],
+  "hints": []
+}

+ 29 - 0
hsweb-web-dao/hsweb-web-dao-mybatis/src/test/java/org/hsweb/web/mybatis/AbstractTestCase.java

@@ -0,0 +1,29 @@
+/*
+ * Copyright 2015-2016 http://hsweb.me
+ *
+ * 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.hsweb.web.mybatis;
+
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.SpringApplicationConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+/**
+ * Created by zhouhao on 16-4-20.
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@SpringApplicationConfiguration(classes = SpringApplication.class)
+public abstract class AbstractTestCase {
+}

+ 64 - 0
hsweb-web-dao/hsweb-web-dao-mybatis/src/test/java/org/hsweb/web/mybatis/SpringApplication.java

@@ -0,0 +1,64 @@
+/*
+ * Copyright 2015-2016 http://hsweb.me
+ *
+ * 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.hsweb.web.mybatis;
+
+import org.hsweb.ezorm.executor.AbstractJdbcSqlExecutor;
+import org.hsweb.ezorm.executor.SqlExecutor;
+import org.hsweb.web.core.datasource.DataSourceHolder;
+import org.hsweb.web.core.datasource.DatabaseType;
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.jdbc.datasource.DataSourceUtils;
+import org.springframework.transaction.annotation.EnableTransactionManagement;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.SQLException;
+
+/**
+ * Created by zhouhao on 16-4-20.
+ */
+@Configuration
+@EnableAutoConfiguration
+@ComponentScan("org.hsweb.web")
+@MapperScan("org.hsweb.web.dao")
+public class SpringApplication {
+
+    @Bean
+    public SqlExecutor sqlExecutor(DataSource dataSource) throws SQLException {
+        Connection connection = dataSource.getConnection();
+        try {
+            DataSourceHolder.install(dataSource, DatabaseType.fromJdbcUrl(connection.getMetaData().getURL()));
+        } finally {
+            connection.close();
+        }
+        return new AbstractJdbcSqlExecutor() {
+            @Override
+            public Connection getConnection() {
+                return DataSourceUtils.getConnection(dataSource);
+            }
+
+            @Override
+            public void releaseConnection(Connection connection) throws SQLException {
+                DataSourceUtils.releaseConnection(connection, dataSource);
+            }
+        };
+    }
+}

+ 53 - 0
hsweb-web-dao/hsweb-web-dao-mybatis/src/test/java/org/hsweb/web/mybatis/user/UserMapperTest.java

@@ -0,0 +1,53 @@
+/*
+ * Copyright 2015-2016 http://hsweb.me
+ *
+ * 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.hsweb.web.mybatis.user;
+
+import org.hsweb.web.bean.common.InsertParam;
+import org.hsweb.web.bean.common.QueryParam;
+import org.hsweb.web.bean.po.user.User;
+import org.hsweb.web.dao.user.UserMapper;
+import org.hsweb.web.mybatis.AbstractTestCase;
+import org.junit.Assert;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import java.util.Date;
+
+/**
+ * @author zhouhao
+ */
+public class UserMapperTest extends AbstractTestCase {
+
+    @Autowired
+    private UserMapper userMapper;
+
+    @Test
+    public void testInsert() {
+        User user = new User();
+        user.setId("test");
+        user.setUsername("admin");
+        user.setName("test");
+        user.setCreateDate(new Date());
+        int i = userMapper.insert(InsertParam.build(user));
+        Assert.assertEquals(i, 1);
+    }
+
+    @Test
+    public void testQuery() {
+        userMapper.select(QueryParam.build());
+    }
+}

+ 10 - 0
hsweb-web-dao/hsweb-web-dao-mybatis/src/test/resources/application.yml

@@ -0,0 +1,10 @@
+logging:
+    config: classpath:logback.xml
+spring:
+    aop:
+        auto: true
+    redis:
+        host: 127.0.0.1
+        port: 6379
+mybatis:
+    config: classpath:mybatis-config.xml

+ 19 - 0
hsweb-web-dao/hsweb-web-dao-mybatis/src/test/resources/logback.xml

@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<configuration>
+    <!-- 控制台输出日志 -->
+    <appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
+        <encoder>
+            <pattern>%d{HH:mm:ss.SSS} > %-5level %logger{35} - %m%n</pattern>
+        </encoder>
+    </appender>
+    <logger name="org.springframework" level="ERROR" />
+    <logger name="org.apache.tomcat" level="ERROR" />
+    <logger name="org.hsweb" level="DEBUG" />
+    <logger name="com.atomikos" level="ERROR" />
+    <logger name="org.h2" level="ON" />
+    <logger name="org.mybatis.spring.SqlSessionUtils" level="DEBUG" />
+    <logger name="org.hsweb.web.datasource" level="INFO" />
+    <root level="ERROR">
+        <appender-ref ref="Console"/>
+    </root>
+</configuration>

+ 56 - 0
hsweb-web-dao/hsweb-web-dao-mybatis/src/test/resources/mybatis-config.xml

@@ -0,0 +1,56 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE configuration
+        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-config.dtd">
+<configuration>
+    <!-- 配置mybatis的缓存,延迟加载等等一系列属性 -->
+    <settings>
+        <!-- 全局映射器启用缓存 -->
+        <setting name="cacheEnabled" value="true"/>
+
+        <!-- 查询时,关闭关联对象即时加载以提高性能 -->
+        <setting name="lazyLoadingEnabled" value="true"/>
+
+        <!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 -->
+        <setting name="multipleResultSetsEnabled" value="true"/>
+
+        <!-- 允许使用列标签代替列名 -->
+        <setting name="useColumnLabel" value="true"/>
+
+        <!-- 不允许使用自定义的主键值(比如由程序生成的UUID 32位编码作为键值),数据表的PK生成策略将被覆盖 -->
+        <setting name="useGeneratedKeys" value="false"/>
+
+        <!-- 给予被嵌套的resultMap以字段-属性的映射支持 FULL,PARTIAL -->
+        <setting name="autoMappingBehavior" value="PARTIAL"/>
+
+        <!-- 对于批量更新操作缓存SQL以提高性能 BATCH,SIMPLE -->
+        <!-- <setting name="defaultExecutorType" value="BATCH" /> -->
+
+        <!-- 数据库超过25000秒仍未响应则超时 -->
+        <!-- <setting name="defaultStatementTimeout" value="25000" /> -->
+
+        <!-- Allows using RowBounds on nested statements -->
+        <setting name="safeRowBoundsEnabled" value="false"/>
+
+        <!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn. -->
+        <setting name="mapUnderscoreToCamelCase" value="true"/>
+
+        <!-- MyBatis uses local cache to prevent circular references and speed up repeated nested queries. By default (SESSION) all queries executed during a session are cached. If localCacheScope=STATEMENT
+            local session will be used just for statement execution, no data will be shared between two different calls to the same SqlSession. -->
+        <setting name="localCacheScope" value="SESSION"/>
+
+        <!-- Specifies the JDBC type for null values when no specific JDBC type was provided for the parameter. Some drivers require specifying the column JDBC type but others work with generic values
+            like NULL, VARCHAR or OTHER. -->
+        <setting name="jdbcTypeForNull" value="OTHER"/>
+
+        <!-- Specifies which Object's methods trigger a lazy load -->
+        <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
+
+        <!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的所有字段,以提高性能 -->
+        <setting name="aggressiveLazyLoading" value="false"/>
+        <setting name="proxyFactory" value="CGLIB"/>
+    </settings>
+    <typeHandlers>
+        <package name="org.hsweb.web.mybatis.handler"></package>
+    </typeHandlers>
+</configuration>

+ 19 - 0
hsweb-web-dao/hsweb-web-dao-mybatis/src/test/resources/transactions.properties

@@ -0,0 +1,19 @@
+#
+# Copyright 2015-2016 http://hsweb.me
+#
+# 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.
+#
+com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory
+com.atomikos.icatch.serial_jta_transactions=false
+com.atomikos.icatch.output_dir=./data/atomikos
+com.atomikos.icatch.log_base_dir=./data/atomikos