Skip to content

Commit 5f588b2

Browse files
committed
增加dao测试
1 parent 495b04e commit 5f588b2

File tree

10 files changed

+448
-26
lines changed

10 files changed

+448
-26
lines changed

hsweb-web-dao/hsweb-web-dao-mybatis/pom.xml

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@
1717
</properties>
1818

1919
<dependencies>
20+
21+
<!--test-->
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-test</artifactId>
25+
<scope>test</scope>
26+
</dependency>
27+
<dependency>
28+
<groupId>com.alibaba</groupId>
29+
<artifactId>druid</artifactId>
30+
<version>1.0.26</version>
31+
<scope>test</scope>
32+
</dependency>
33+
2034
<dependency>
2135
<groupId>org.hsweb</groupId>
2236
<artifactId>hsweb-web-dao-api</artifactId>
@@ -32,11 +46,11 @@
3246
<artifactId>spring-boot-starter-jdbc</artifactId>
3347
</dependency>
3448

35-
<dependency>
36-
<groupId>org.hsweb</groupId>
37-
<artifactId>hsweb-web-datasource</artifactId>
38-
<optional>true</optional>
39-
</dependency>
49+
<!--<dependency>-->
50+
<!--<groupId>org.hsweb</groupId>-->
51+
<!--<artifactId>hsweb-web-datasource</artifactId>-->
52+
<!--<optional>true</optional>-->
53+
<!--</dependency>-->
4054

4155
<!-- mybatis start-->
4256
<dependency>

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

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,36 @@
2323
import java.util.*;
2424
import java.util.stream.Collectors;
2525

26+
/**
27+
* mybatis配置,继承官方配置类,增加一些属性以拓展更多功能
28+
* <ul>
29+
* <li>是否启用动态数据源{@link this#dynamicDatasource}</li>
30+
* <li>可设置不加载的配置{@link this#mapperLocationExcludes}</li>
31+
* </ul>
32+
*
33+
* @author zhouhao
34+
* @see org.mybatis.spring.boot.autoconfigure.MybatisProperties
35+
* @since 2.1
36+
*/
2637
public class MybatisProperties extends org.mybatis.spring.boot.autoconfigure.MybatisProperties {
38+
/**
39+
* 默认支持的hsweb mapper
40+
*/
2741
private static final String defaultMapperLocation = "classpath*:org/hsweb/web/dao/impl/mybatis/mapper/**/*.xml";
28-
private String type = "oracle";
42+
/**
43+
* 是否启用动态数据源
44+
* 启用后调用{@link org.hsweb.web.core.datasource.DynamicDataSource#use(String)},mybatis也会进行数据源切换
45+
*
46+
* @see org.hsweb.web.core.datasource.DynamicDataSource
47+
*/
2948
private boolean dynamicDatasource = false;
49+
/**
50+
* 排除加载的mapper.xml
51+
* 想自定义mapper并覆盖原始mapper的场景下,通过设置此属性来排除配置文件。
52+
* 排除使用{@link Resource#getURL()#toString()}进行对比
53+
*/
3054
private String[] mapperLocationExcludes = null;
3155

32-
public String getType() {
33-
return type;
34-
}
35-
36-
public void setType(String type) {
37-
this.type = type;
38-
}
39-
4056
public String[] getMapperLocationExcludes() {
4157
return mapperLocationExcludes;
4258
}
@@ -53,11 +69,6 @@ public void setDynamicDatasource(boolean dynamicDatasource) {
5369
this.dynamicDatasource = dynamicDatasource;
5470
}
5571

56-
@Override
57-
public String[] getMapperLocations() {
58-
return super.getMapperLocations();
59-
}
60-
6172
public Resource[] resolveMapperLocations() {
6273
Map<String, Resource> resources = new HashMap<>();
6374
Set<String> locations;
@@ -77,9 +88,8 @@ public Resource[] resolveMapperLocations() {
7788
} catch (IOException e) {
7889
}
7990
}
80-
if (mapperLocationExcludes != null && mapperLocationExcludes.length > 0)
81-
82-
{
91+
//排除不需要的配置
92+
if (mapperLocationExcludes != null && mapperLocationExcludes.length > 0) {
8393
for (String mapperLocationExclude : mapperLocationExcludes) {
8494
try {
8595
Resource[] excludesMappers = new PathMatchingResourcePatternResolver().getResources(mapperLocationExclude);
@@ -90,11 +100,8 @@ public Resource[] resolveMapperLocations() {
90100
}
91101
}
92102
}
93-
94103
Resource[] mapperLocations = new Resource[resources.size()];
95-
mapperLocations = resources.values().
96-
97-
toArray(mapperLocations);
104+
mapperLocations = resources.values().toArray(mapperLocations);
98105
return mapperLocations;
99106
}
100107

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
{
2+
"groups": [
3+
{
4+
"name": "mybatis",
5+
"type": "org.hsweb.web.mybatis.MybatisProperties",
6+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
7+
}
8+
],
9+
"properties": [
10+
{
11+
"name": "mybatis.dynamic-datasource",
12+
"type": "java.lang.Boolean",
13+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties",
14+
"description": "enable dynamicDatasource."
15+
},
16+
{
17+
"name": "mybatis.mapper-location-excludes",
18+
"type": "java.lang.String[]",
19+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties",
20+
"description": "exclude mapperLocations."
21+
},
22+
{
23+
"name": "mybatis.check-config-location",
24+
"type": "java.lang.Boolean",
25+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
26+
},
27+
{
28+
"name": "mybatis.check-config-location",
29+
"type": "java.lang.Boolean",
30+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
31+
},
32+
{
33+
"name": "mybatis.check-config-location",
34+
"type": "java.lang.Boolean",
35+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
36+
},
37+
{
38+
"name": "mybatis.check-config-location",
39+
"type": "java.lang.Boolean",
40+
"description": "Check the config file exists.",
41+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties",
42+
"defaultValue": false
43+
},
44+
{
45+
"name": "mybatis.config",
46+
"type": "java.lang.String",
47+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
48+
},
49+
{
50+
"name": "mybatis.config",
51+
"type": "java.lang.String",
52+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
53+
},
54+
{
55+
"name": "mybatis.config",
56+
"type": "java.lang.String",
57+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
58+
},
59+
{
60+
"name": "mybatis.config",
61+
"type": "java.lang.String",
62+
"description": "Config file path.",
63+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
64+
},
65+
{
66+
"name": "mybatis.executor-type",
67+
"type": "org.apache.ibatis.session.ExecutorType",
68+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
69+
},
70+
{
71+
"name": "mybatis.executor-type",
72+
"type": "org.apache.ibatis.session.ExecutorType",
73+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
74+
},
75+
{
76+
"name": "mybatis.executor-type",
77+
"type": "org.apache.ibatis.session.ExecutorType",
78+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
79+
},
80+
{
81+
"name": "mybatis.executor-type",
82+
"type": "org.apache.ibatis.session.ExecutorType",
83+
"description": "Execution mode.",
84+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
85+
},
86+
{
87+
"name": "mybatis.mapper-locations",
88+
"type": "java.lang.String[]",
89+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
90+
},
91+
{
92+
"name": "mybatis.mapper-locations",
93+
"type": "java.lang.String[]",
94+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
95+
},
96+
{
97+
"name": "mybatis.mapper-locations",
98+
"type": "java.lang.String[]",
99+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
100+
},
101+
{
102+
"name": "mybatis.mapper-locations",
103+
"type": "java.lang.String[]",
104+
"description": "Location of mybatis mapper files.",
105+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
106+
},
107+
{
108+
"name": "mybatis.type-aliases-package",
109+
"type": "java.lang.String",
110+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
111+
},
112+
{
113+
"name": "mybatis.type-aliases-package",
114+
"type": "java.lang.String",
115+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
116+
},
117+
{
118+
"name": "mybatis.type-aliases-package",
119+
"type": "java.lang.String",
120+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
121+
},
122+
{
123+
"name": "mybatis.type-aliases-package",
124+
"type": "java.lang.String",
125+
"description": "Package to scan domain objects.",
126+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
127+
},
128+
{
129+
"name": "mybatis.type-handlers-package",
130+
"type": "java.lang.String",
131+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
132+
},
133+
{
134+
"name": "mybatis.type-handlers-package",
135+
"type": "java.lang.String",
136+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
137+
},
138+
{
139+
"name": "mybatis.type-handlers-package",
140+
"type": "java.lang.String",
141+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
142+
},
143+
{
144+
"name": "mybatis.type-handlers-package",
145+
"type": "java.lang.String",
146+
"description": "Package to scan handlers.",
147+
"sourceType": "org.hsweb.web.mybatis.MybatisProperties"
148+
}
149+
],
150+
"hints": []
151+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2015-2016 http://hsweb.me
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.hsweb.web.mybatis;
18+
19+
import org.junit.runner.RunWith;
20+
import org.springframework.boot.test.SpringApplicationConfiguration;
21+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
22+
23+
/**
24+
* Created by zhouhao on 16-4-20.
25+
*/
26+
@RunWith(SpringJUnit4ClassRunner.class)
27+
@SpringApplicationConfiguration(classes = SpringApplication.class)
28+
public abstract class AbstractTestCase {
29+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2015-2016 http://hsweb.me
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.hsweb.web.mybatis;
18+
19+
import org.hsweb.ezorm.executor.AbstractJdbcSqlExecutor;
20+
import org.hsweb.ezorm.executor.SqlExecutor;
21+
import org.hsweb.web.core.datasource.DataSourceHolder;
22+
import org.hsweb.web.core.datasource.DatabaseType;
23+
import org.mybatis.spring.annotation.MapperScan;
24+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.ComponentScan;
27+
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.jdbc.datasource.DataSourceUtils;
29+
import org.springframework.transaction.annotation.EnableTransactionManagement;
30+
31+
import javax.sql.DataSource;
32+
import java.sql.Connection;
33+
import java.sql.SQLException;
34+
35+
/**
36+
* Created by zhouhao on 16-4-20.
37+
*/
38+
@Configuration
39+
@EnableAutoConfiguration
40+
@ComponentScan("org.hsweb.web")
41+
@MapperScan("org.hsweb.web.dao")
42+
public class SpringApplication {
43+
44+
@Bean
45+
public SqlExecutor sqlExecutor(DataSource dataSource) throws SQLException {
46+
Connection connection = dataSource.getConnection();
47+
try {
48+
DataSourceHolder.install(dataSource, DatabaseType.fromJdbcUrl(connection.getMetaData().getURL()));
49+
} finally {
50+
connection.close();
51+
}
52+
return new AbstractJdbcSqlExecutor() {
53+
@Override
54+
public Connection getConnection() {
55+
return DataSourceUtils.getConnection(dataSource);
56+
}
57+
58+
@Override
59+
public void releaseConnection(Connection connection) throws SQLException {
60+
DataSourceUtils.releaseConnection(connection, dataSource);
61+
}
62+
};
63+
}
64+
}

0 commit comments

Comments
 (0)