Skip to content

Commit d624c02

Browse files
✨ Support Mapper XML based on MyBatis for Spring Boot 3 Native
mybatis/spring-boot-starter#994
1 parent c00ce4f commit d624c02

File tree

29 files changed

+1338
-4
lines changed

29 files changed

+1338
-4
lines changed

.github/workflows/native.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ jobs:
6767
# run: xcode-select --install
6868

6969
- name: Test with Spring Boot Native Latest
70-
run: ./mvnw -V compile -Pnative native:compile -am -pl mybatis-spring-boot-samples/mybatis-spring-boot-sample-graalvm-annotation
70+
run: ./mvnw -V compile -Pnative native:compile -am -pl mybatis-spring-boot-samples/mybatis-spring-boot-sample-graalvm-annotation,mybatis-spring-boot-samples/mybatis-spring-boot-sample-graalvm-xml
7171

7272
- name: Run Native Latest
73-
run: mybatis-spring-boot-samples/mybatis-spring-boot-sample-graalvm-annotation/target/mybatis-spring-boot-sample-graalvm-annotation
73+
run: |
74+
mybatis-spring-boot-samples/mybatis-spring-boot-sample-graalvm-annotation/target/mybatis-spring-boot-sample-graalvm-annotation
75+
mybatis-spring-boot-samples/mybatis-spring-boot-sample-graalvm-xml/target/mybatis-spring-boot-sample-graalvm-xml

mybatis-spring-boot-autoconfigure/src/main/resources/META-INF/native-image/org.mybatis.spring.boot/mybatis-spring-boot-autoconfigure/mybatis-reflection.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@
1313
{ "name": "java.util.ArrayList", "methods": [{ "name": "<init>", "parameterTypes": [] }] },
1414
{ "name": "java.util.HashMap", "methods": [{ "name": "<init>", "parameterTypes": [] }] },
1515
{ "name": "java.util.TreeSet", "methods": [{ "name": "<init>", "parameterTypes": [] }] },
16-
{ "name": "java.util.HashSet", "methods": [{ "name": "<init>", "parameterTypes": [] }] }
16+
{ "name": "java.util.HashSet", "methods": [{ "name": "<init>", "parameterTypes": [] }] },
17+
{ "name": "org.apache.ibatis.session.Configuration", "methods": [{ "name": "<init>", "parameterTypes": [] }] },
18+
{ "name": "org.mybatis.spring.boot.autoconfigure.SpringBootVFS", "methods": [{ "name": "<init>", "parameterTypes": [] }] }
1719
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"resources": {
3+
"includes": [
4+
{ "pattern": "org/apache/ibatis/builder/xml/mybatis-3-config.dtd" },
5+
{ "pattern": "org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd" },
6+
{ "pattern": "org/apache/ibatis/builder/xml/mybatis-config.xsd" },
7+
{ "pattern": "org/apache/ibatis/builder/xml/mybatis-mapper.xsd" }
8+
]
9+
}
10+
}

mybatis-spring-boot-autoconfigure/src/main/resources/META-INF/native-image/org.mybatis.spring.boot/mybatis-spring-boot-autoconfigure/native-image.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
# limitations under the License.
1515
#
1616

17-
Args = -H:ReflectionConfigurationResources=${.}/mybatis-reflection.json
17+
Args = -H:ReflectionConfigurationResources=${.}/mybatis-reflection.json -H:ResourceConfigurationResources=${.}/mybatis-resource.json
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright ${license.git.copyrightYears} the original author or authors.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
https://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2015-2024 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
https://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<!DOCTYPE Format>
20+
<Format>
21+
<!-- Dummy format file -->
22+
</Format>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2015-2024 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
https://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
<parent>
22+
<groupId>org.mybatis.spring.boot</groupId>
23+
<artifactId>mybatis-spring-boot-samples</artifactId>
24+
<version>3.0.4-SNAPSHOT</version>
25+
</parent>
26+
<artifactId>mybatis-spring-boot-sample-graalvm-xml</artifactId>
27+
<packaging>jar</packaging>
28+
<name>mybatis-spring-boot-sample-graalvm-xml</name>
29+
<properties>
30+
<module.name>org.mybatis.spring.boot.sample.graalvmxml</module.name>
31+
</properties>
32+
<dependencies>
33+
<dependency>
34+
<groupId>org.mybatis.spring.boot</groupId>
35+
<artifactId>mybatis-spring-boot-starter</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>com.h2database</groupId>
39+
<artifactId>h2</artifactId>
40+
<scope>runtime</scope>
41+
</dependency>
42+
<!-- test dependencies -->
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-test</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.mybatis.spring.boot</groupId>
50+
<artifactId>mybatis-spring-boot-starter-test</artifactId>
51+
<scope>test</scope>
52+
</dependency>
53+
</dependencies>
54+
<build>
55+
<plugins>
56+
<plugin>
57+
<groupId>org.graalvm.buildtools</groupId>
58+
<artifactId>native-maven-plugin</artifactId>
59+
</plugin>
60+
<plugin>
61+
<groupId>org.springframework.boot</groupId>
62+
<artifactId>spring-boot-maven-plugin</artifactId>
63+
<version>${spring-boot.version}</version>
64+
<executions>
65+
<execution>
66+
<goals>
67+
<goal>repackage</goal>
68+
</goals>
69+
</execution>
70+
</executions>
71+
</plugin>
72+
</plugins>
73+
</build>
74+
<profiles>
75+
<profile>
76+
<id>native</id>
77+
<build>
78+
<pluginManagement>
79+
<plugins>
80+
<plugin>
81+
<groupId>org.apache.maven.plugins</groupId>
82+
<artifactId>maven-jar-plugin</artifactId>
83+
<configuration>
84+
<archive>
85+
<manifestEntries>
86+
<Spring-Boot-Native-Processed>true</Spring-Boot-Native-Processed>
87+
</manifestEntries>
88+
</archive>
89+
</configuration>
90+
</plugin>
91+
<plugin>
92+
<groupId>org.springframework.boot</groupId>
93+
<artifactId>spring-boot-maven-plugin</artifactId>
94+
<configuration>
95+
<image>
96+
<builder>paketobuildpacks/builder-jammy-tiny:latest</builder>
97+
<env>
98+
<BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
99+
</env>
100+
</image>
101+
</configuration>
102+
<executions>
103+
<execution>
104+
<id>process-aot</id>
105+
<goals>
106+
<goal>process-aot</goal>
107+
</goals>
108+
</execution>
109+
</executions>
110+
</plugin>
111+
<plugin>
112+
<groupId>org.graalvm.buildtools</groupId>
113+
<artifactId>native-maven-plugin</artifactId>
114+
<version>${native-build-tools-plugin.version}</version>
115+
<configuration>
116+
<classesDirectory>${project.build.outputDirectory}</classesDirectory>
117+
<requiredVersion>22.3</requiredVersion>
118+
</configuration>
119+
<executions>
120+
<execution>
121+
<id>add-reachability-metadata</id>
122+
<goals>
123+
<goal>add-reachability-metadata</goal>
124+
</goals>
125+
</execution>
126+
</executions>
127+
</plugin>
128+
</plugins>
129+
</pluginManagement>
130+
</build>
131+
</profile>
132+
<profile>
133+
<id>nativeTest</id>
134+
<dependencies>
135+
<dependency>
136+
<groupId>org.junit.platform</groupId>
137+
<artifactId>junit-platform-launcher</artifactId>
138+
<scope>test</scope>
139+
</dependency>
140+
</dependencies>
141+
<build>
142+
<plugins>
143+
<plugin>
144+
<groupId>org.springframework.boot</groupId>
145+
<artifactId>spring-boot-maven-plugin</artifactId>
146+
<executions>
147+
<execution>
148+
<id>process-test-aot</id>
149+
<goals>
150+
<goal>process-test-aot</goal>
151+
</goals>
152+
</execution>
153+
</executions>
154+
</plugin>
155+
<plugin>
156+
<groupId>org.graalvm.buildtools</groupId>
157+
<artifactId>native-maven-plugin</artifactId>
158+
<version>${native-build-tools-plugin.version}</version>
159+
<configuration>
160+
<classesDirectory>${project.build.outputDirectory}</classesDirectory>
161+
<requiredVersion>22.3</requiredVersion>
162+
</configuration>
163+
<executions>
164+
<execution>
165+
<id>native-test</id>
166+
<goals>
167+
<goal>test</goal>
168+
</goals>
169+
</execution>
170+
</executions>
171+
</plugin>
172+
</plugins>
173+
</build>
174+
</profile>
175+
</profiles>
176+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2015-2024 the original author or authors.
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+
* https://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+
package sample.mybatis.graalvm.xml;
17+
18+
import org.springframework.boot.CommandLineRunner;
19+
import org.springframework.boot.SpringApplication;
20+
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
22+
import sample.mybatis.graalvm.xml.dao.CityDao;
23+
import sample.mybatis.graalvm.xml.mapper.CityMapper;
24+
import sample.mybatis.graalvm.xml.mapper.HotelMapper;
25+
26+
@SpringBootApplication
27+
public class SampleXmlApplication implements CommandLineRunner {
28+
29+
public static void main(String[] args) {
30+
SpringApplication.run(SampleXmlApplication.class, args);
31+
}
32+
33+
private final CityDao cityDao;
34+
35+
private final CityMapper cityMapper;
36+
37+
private final HotelMapper hotelMapper;
38+
39+
public SampleXmlApplication(CityDao cityDao, CityMapper cityMapper, HotelMapper hotelMapper) {
40+
this.cityDao = cityDao;
41+
this.cityMapper = cityMapper;
42+
this.hotelMapper = hotelMapper;
43+
}
44+
45+
@Override
46+
@SuppressWarnings("squid:S106")
47+
public void run(String... args) {
48+
System.out.println(this.cityDao.selectCityById(1));
49+
System.out.println(this.cityMapper.findByState("CA"));
50+
System.out.println(this.cityMapper.listByState("ShanDong"));
51+
System.out.println(this.cityMapper.mapByState("CA"));
52+
System.out.println(this.cityMapper.listMapByState("ShanDong"));
53+
System.out.println(this.cityMapper.treeSetStateByState("CN"));
54+
System.out.println(this.cityMapper.hashSetStateByState("CN"));
55+
System.out.println(this.hotelMapper.selectByCityId(1));
56+
}
57+
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2015-2024 the original author or authors.
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+
* https://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+
package sample.mybatis.graalvm.xml.dao;
17+
18+
import org.apache.ibatis.session.SqlSession;
19+
import org.springframework.stereotype.Component;
20+
21+
import sample.mybatis.graalvm.xml.domain.City;
22+
23+
/**
24+
* @author Eddú Meléndez
25+
*/
26+
@Component
27+
public class CityDao {
28+
29+
private final SqlSession sqlSession;
30+
31+
public CityDao(SqlSession sqlSession) {
32+
this.sqlSession = sqlSession;
33+
}
34+
35+
public City selectCityById(long id) {
36+
return this.sqlSession.selectOne("selectCityById", id);
37+
}
38+
39+
}

0 commit comments

Comments
 (0)