Skip to content

Commit 2b8be23

Browse files
authored
Merge pull request #454 from kazuki43zoo/gh-452
Add sample(test) using TemplateFilePathProvider
2 parents ed9f38c + e4003e7 commit 2b8be23

File tree

16 files changed

+105
-16
lines changed

16 files changed

+105
-16
lines changed

mybatis-spring-boot-samples/mybatis-spring-boot-sample-freemarker/src/main/java/sample/mybatis/SampleFreeMarkerApplication.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,6 +41,7 @@ public SampleFreeMarkerApplication(CityMapper cityMapper) {
4141
@SuppressWarnings("squid:S106")
4242
public void run(String... args) {
4343
System.out.println(this.cityMapper.findByState("CA"));
44+
System.out.println(this.cityMapper.findByCountry("JP"));
4445
}
4546

4647
}

mybatis-spring-boot-samples/mybatis-spring-boot-sample-freemarker/src/main/java/sample/mybatis/mapper/CityMapper.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,8 @@
1818
import org.apache.ibatis.annotations.Mapper;
1919
import org.apache.ibatis.annotations.Param;
2020
import org.apache.ibatis.annotations.Select;
21+
import org.apache.ibatis.annotations.SelectProvider;
22+
import org.mybatis.scripting.freemarker.support.TemplateFilePathProvider;
2123
import sample.mybatis.domain.City;
2224

2325
/**
@@ -29,9 +31,12 @@ public interface CityMapper {
2931
@Select("select id, name, state, country from city where id = <@p name='id'/>")
3032
City findById(@Param("id") Long id);
3133

32-
@Select("/mappers/CityMapper-findByState.ftl")
34+
@Select("CityMapper-findByState.ftl")
3335
City findByState(@Param("state") String state);
3436

3537
City findByName(@Param("name") String name);
3638

39+
@SelectProvider(TemplateFilePathProvider.class)
40+
City findByCountry(@Param("country") String country);
41+
3742
}

mybatis-spring-boot-samples/mybatis-spring-boot-sample-freemarker/src/main/resources/application.properties

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright 2015-2019 the original author or authors.
2+
# Copyright 2015-2020 the original author or authors.
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -19,3 +19,7 @@ logging.level.sample.mybatis.mapper=TRACE
1919

2020
mybatis.mapper-locations=classpath*:/mappers/*.xml
2121
mybatis.type-aliases-package=sample.mybatis.domain
22+
23+
mybatis.scripting-language-driver.freemarker.template-file.base-dir=mappers/
24+
mybatis.scripting-language-driver.freemarker.template-file.path-provider.includes-package-path=false
25+
mybatis.scripting-language-driver.freemarker.template-file.path-provider.separate-directory-per-mapper=false

mybatis-spring-boot-samples/mybatis-spring-boot-sample-freemarker/src/main/resources/data.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--
2-
-- Copyright 2015-2019 the original author or authors.
2+
-- Copyright 2015-2020 the original author or authors.
33
--
44
-- Licensed under the Apache License, Version 2.0 (the "License");
55
-- you may not use this file except in compliance with the License.
@@ -15,3 +15,4 @@
1515
--
1616

1717
insert into city (name, state, country) values ('San Francisco', 'CA', 'US');
18+
insert into city (name, state, country) values ('Tokyo', '13', 'JP');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<#--
2+
3+
Copyright 2015-2020 the original author or authors.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
-->
18+
select
19+
id, name, state, country
20+
from
21+
city
22+
where
23+
country = <@p name="country"/>

mybatis-spring-boot-samples/mybatis-spring-boot-sample-freemarker/src/test/java/sample/mybatis/SampleMybatisApplicationMainTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@ void test(OutputCapture outputCapture) {
3232
SampleFreeMarkerApplication.main(new String[] {});
3333
String output = outputCapture.toString();
3434
assertThat(output).contains("1,San Francisco,CA,US");
35+
assertThat(output).contains("2,Tokyo,13,JP");
3536
}
3637

3738
}

mybatis-spring-boot-samples/mybatis-spring-boot-sample-freemarker/src/test/java/sample/mybatis/SampleMybatisApplicationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@ class SampleMybatisApplicationTest {
3434
void test(OutputCapture outputCapture) {
3535
String output = outputCapture.toString();
3636
assertThat(output).contains("1,San Francisco,CA,US");
37+
assertThat(output).contains("2,Tokyo,13,JP");
3738
}
3839

3940
}

mybatis-spring-boot-samples/mybatis-spring-boot-sample-freemarker/src/test/java/sample/mybatis/mapper/CityMapperTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,4 +61,13 @@ void findByName() {
6161
assertThat(city.getCountry()).isEqualTo("US");
6262
}
6363

64+
@Test
65+
void findByCountry() {
66+
City city = cityMapper.findByCountry("JP");
67+
assertThat(city.getId()).isEqualTo(2);
68+
assertThat(city.getName()).isEqualTo("Tokyo");
69+
assertThat(city.getState()).isEqualTo("13");
70+
assertThat(city.getCountry()).isEqualTo("JP");
71+
}
72+
6473
}

mybatis-spring-boot-samples/mybatis-spring-boot-sample-thymeleaf/src/main/java/sample/mybatis/SampleThymeleafApplication.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,6 +41,7 @@ public SampleThymeleafApplication(CityMapper cityMapper) {
4141
@SuppressWarnings("squid:S106")
4242
public void run(String... args) {
4343
System.out.println(this.cityMapper.findByState("CA"));
44+
System.out.println(this.cityMapper.findByCountry("JP"));
4445
}
4546

4647
}

mybatis-spring-boot-samples/mybatis-spring-boot-sample-thymeleaf/src/main/java/sample/mybatis/mapper/CityMapper.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,8 @@
1818
import org.apache.ibatis.annotations.Mapper;
1919
import org.apache.ibatis.annotations.Param;
2020
import org.apache.ibatis.annotations.Select;
21+
import org.apache.ibatis.annotations.SelectProvider;
22+
import org.mybatis.scripting.thymeleaf.support.TemplateFilePathProvider;
2123
import sample.mybatis.domain.City;
2224

2325
/**
@@ -29,9 +31,12 @@ public interface CityMapper {
2931
@Select("select id, name, state, country from city where id = /*[# mb:p='id']*/ 1 /*[/]*/")
3032
City findById(@Param("id") Long id);
3133

32-
@Select("/mappers/CityMapper-findByState.sql")
34+
@Select("CityMapper-findByState.sql")
3335
City findByState(@Param("state") String state);
3436

3537
City findByName(@Param("name") String name);
3638

39+
@SelectProvider(TemplateFilePathProvider.class)
40+
City findByCountry(@Param("country") String country);
41+
3742
}

mybatis-spring-boot-samples/mybatis-spring-boot-sample-thymeleaf/src/main/resources/application.properties

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright 2015-2019 the original author or authors.
2+
# Copyright 2015-2020 the original author or authors.
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -19,3 +19,7 @@ logging.level.sample.mybatis.mapper=TRACE
1919

2020
mybatis.mapper-locations=classpath*:/mappers/*.xml
2121
mybatis.type-aliases-package=sample.mybatis.domain
22+
23+
mybatis.scripting-language-driver.thymeleaf.template-file.base-dir=mappers/
24+
mybatis.scripting-language-driver.thymeleaf.template-file.path-provider.includes-package-path=false
25+
mybatis.scripting-language-driver.thymeleaf.template-file.path-provider.separate-directory-per-mapper=false

mybatis-spring-boot-samples/mybatis-spring-boot-sample-thymeleaf/src/main/resources/data.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--
2-
-- Copyright 2015-2019 the original author or authors.
2+
-- Copyright 2015-2020 the original author or authors.
33
--
44
-- Licensed under the Apache License, Version 2.0 (the "License");
55
-- you may not use this file except in compliance with the License.
@@ -15,3 +15,4 @@
1515
--
1616

1717
insert into city (name, state, country) values ('San Francisco', 'CA', 'US');
18+
insert into city (name, state, country) values ('Tokyo', '13', 'JP');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--
2+
-- Copyright 2015-2020 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+
-- 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+
select
18+
id, name, state, country
19+
from
20+
city
21+
where
22+
country = /*[# mb:p="country"]*/ 'US' /*[/]*/

mybatis-spring-boot-samples/mybatis-spring-boot-sample-thymeleaf/src/test/java/sample/mybatis/SampleMybatisApplicationMainTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@ void test(OutputCapture outputCapture) {
3232
SampleThymeleafApplication.main(new String[] {});
3333
String output = outputCapture.toString();
3434
assertThat(output).contains("1,San Francisco,CA,US");
35+
assertThat(output).contains("2,Tokyo,13,JP");
3536
}
3637

3738
}

mybatis-spring-boot-samples/mybatis-spring-boot-sample-thymeleaf/src/test/java/sample/mybatis/SampleMybatisApplicationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@ class SampleMybatisApplicationTest {
3434
void test(OutputCapture outputCapture) {
3535
String output = outputCapture.toString();
3636
assertThat(output).contains("1,San Francisco,CA,US");
37+
assertThat(output).contains("2,Tokyo,13,JP");
3738
}
3839

3940
}

mybatis-spring-boot-samples/mybatis-spring-boot-sample-thymeleaf/src/test/java/sample/mybatis/mapper/CityMapperTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,4 +61,13 @@ void findByName() {
6161
assertThat(city.getCountry()).isEqualTo("US");
6262
}
6363

64+
@Test
65+
void findByCountry() {
66+
City city = cityMapper.findByCountry("JP");
67+
assertThat(city.getId()).isEqualTo(2);
68+
assertThat(city.getName()).isEqualTo("Tokyo");
69+
assertThat(city.getState()).isEqualTo("13");
70+
assertThat(city.getCountry()).isEqualTo("JP");
71+
}
72+
6473
}

0 commit comments

Comments
 (0)