Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support multiple mapper-locations #318

Merged
merged 5 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ This source code is licensed under Apache 2.0 License.
Route-Tag: abc
```

- feat: @Space annotation supports dynamic configuration.
> @Space 注解的 name 属性值可通过 spring 配置文件自定义配置。
- example:
- feat: @Space annotation and space config in mapper xml supports dynamic configuration.
> @Space 注解的 name 属性值和 xml 文件中 Mapper 标签指定的 Space 可通过 spring 配置文件自定义配置。
- @Space example:

```yaml
app:
person:
space: PERSON_SPACE
```

```java
@Space(name = "${nebula.space}")
@Space(name = "${app.person.space}")
@Table(name = "person")
public class Person {
@Id
Expand All @@ -114,6 +114,14 @@ This source code is licensed under Apache 2.0 License.
}
```

- XML example:

```xml
<mapper namespace="com.xxx.TestSpaceMapper" space="${app.person.space}">

</mapper>
```

# 1.2.2

## Bugfix
Expand Down
25 changes: 7 additions & 18 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<name>ngbatis</name>
<groupId>org.nebula-contrib</groupId>
<artifactId>ngbatis</artifactId>
<version>1.3.0</version>
<version>1.3.0-snapshot</version>

<description>
NgBatis is a database ORM framework base NebulaGraph + spring-boot,
Expand Down Expand Up @@ -157,17 +157,6 @@

<build>
<plugins>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.13</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://s01.oss.sonatype.org</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>

<!-- compiler -->
<plugin>
Expand Down Expand Up @@ -396,14 +385,14 @@

<distributionManagement>
<repository>
<id>ossrh</id>
<name>Nexus Release Repository</name>
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
<id>nexus-releases</id>
<url>http://172.17.10.100:8081/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>ossrh</id>
<name>Nexus Snapshot Repository</name>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
<id>nexus-snapshots</id>
<url>http://172.17.10.100:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>


</project>
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private ParseCfgProps readParseCfgProps(ConfigurableEnvironment environment) {
.setLogShow(environment.getProperty("cql.parser.log-show"))
.setMapper(environment.getProperty("cql.parser.mapper"))
.setNamespace(environment.getProperty("cql.parser.namespace"))
.setMapperLocations(environment.getProperty("cql.parser.mapper-locations"))
.setMapperLocations(environment.getProperty("cql.parser.mapper-locations", String[].class))
.setMapperTplLocation(environment.getProperty("cql.parser.mapper-tpl-location"))
.setResultType(environment.getProperty("cql.parser.result-type"))
.setParameterType(environment.getProperty("cql.parser.parameter-type"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class ParseCfgProps {

private String mapperTplLocation = "NebulaDaoBasic.xml";

private String mapperLocations = "mapper/**/*.xml";
private String[] mapperLocations = {"mapper/**/*.xml"};

private String id = "id";

Expand Down Expand Up @@ -75,7 +75,7 @@ public ParseCfgProps setMapperTplLocation(String mapperTplLocation) {
* <p>获取开发者业务dao对应xml所存放的路径。</p>
* @return 开发者业务dao对应xml所存放的路径
*/
public String getMapperLocations() {
public String[] getMapperLocations() {
return mapperLocations;
}

Expand All @@ -84,8 +84,8 @@ public String getMapperLocations() {
* @param mapperLocations 业务dao对应xml所存放的路径
* @return 解析配置(本应是 void,为支持链式调用而改)
*/
public ParseCfgProps setMapperLocations(String mapperLocations) {
if (isBlank(mapperLocations)) {
public ParseCfgProps setMapperLocations(String[] mapperLocations) {
if (isEmpty(mapperLocations)) {
return this;
}
this.mapperLocations = mapperLocations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,13 @@ public MapperResourceLoader(ParseCfgProps parseConfig,ApplicationContext applica
@TimeLog(name = "xml-load", explain = "mappers xml load completed : {} ms")
public Map<String, ClassModel> load() {
Map<String, ClassModel> resultClassModel = new HashMap<>();
String mapperLocations = parseConfig.getMapperLocations();
String[] mapperLocations = parseConfig.getMapperLocations();
try {
Resource[] resources = getResources(mapperLocations);
for (Resource resource : resources) {
resultClassModel.putAll(parseClassModel(resource));
for (String mapperLocation : mapperLocations) {
Resource[] resources = getResources(mapperLocation);
for (Resource resource : resources) {
resultClassModel.putAll(parseClassModel(resource));
}
}
} catch (FileNotFoundException ffe) {
log.warn("No mapper files were found in path pattern '{}', please add", mapperLocations);
Expand Down Expand Up @@ -125,6 +127,9 @@ public Map<String, ClassModel> parseClassModel(Resource resource)
// 从注解获取 space
if (null == cm.getSpace()) {
setClassModelBySpaceAnnotation(cm);
}else {
//动态解析 XML 中 mapper 标签配置的 Space 属性
setClassModelByXmlConfigSpace(cm);
}
addSpaceToSessionPool(cm.getSpace());

Expand All @@ -138,6 +143,15 @@ public Map<String, ClassModel> parseClassModel(Resource resource)
return result;
}

/**
* 解析自定义的 XML 中的 mapper 标签设置的 space
* @param cm ClassModel
*/
private void setClassModelByXmlConfigSpace(ClassModel cm) {
String space = tryResolvePlaceholder(cm.getSpace());
cm.setSpace(space);
}

/**
* 设置 space
* @param cm ClassModel
Expand Down
Loading