Skip to content

Commit

Permalink
🐛 Native Support HashMap/TreeSet/HashSet
Browse files Browse the repository at this point in the history
  • Loading branch information
xuxiaowei-com-cn committed Oct 17, 2024
1 parent 337e6b0 commit c98aa36
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
{ "name": "org.apache.ibatis.scripting.xmltags.XMLLanguageDriver", "methods": [{ "name": "<init>", "parameterTypes": [] }] },
{ "name": "org.apache.ibatis.scripting.defaults.RawLanguageDriver", "methods": [{ "name": "<init>", "parameterTypes": [] }] },
{ "name": "org.mybatis.spring.SqlSessionFactoryBean", "allDeclaredConstructors": true, "allDeclaredMethods": true },
{ "name": "java.util.ArrayList", "methods": [{ "name": "<init>", "parameterTypes": [] }] }
{ "name": "java.util.ArrayList", "methods": [{ "name": "<init>", "parameterTypes": [] }] },
{ "name": "java.util.HashMap", "methods": [{ "name": "<init>", "parameterTypes": [] }] },
{ "name": "java.util.TreeSet", "methods": [{ "name": "<init>", "parameterTypes": [] }] },
{ "name": "java.util.HashSet", "methods": [{ "name": "<init>", "parameterTypes": [] }] }
]
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public SampleAnnotationApplication(CityMapper cityMapper) {
@SuppressWarnings("squid:S106")
public void run(String... args) {
System.out.println(this.cityMapper.findByState("CA"));
System.out.println(this.cityMapper.listByState("ShanDong"));
System.out.println(this.cityMapper.mapByState("CA"));
System.out.println(this.cityMapper.listMapByState("ShanDong"));
System.out.println(this.cityMapper.treeSetStateByState("CN"));
System.out.println(this.cityMapper.hashSetStateByState("CN"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
*/
package sample.mybatis.graalvm.annotation.mapper;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
Expand All @@ -30,4 +35,19 @@ public interface CityMapper {
@Select("select id, name, state, country from city where state = #{state}")
City findByState(@Param("state") String state);

@Select("select id, name, state, country from city where state = #{state}")
List<City> listByState(@Param("state") String state);

@Select("select id, name, state, country from city where state = #{state}")
Map<String, Object> mapByState(@Param("state") String state);

@Select("select id, name, state, country from city where state = #{state}")
List<Map<String, Object>> listMapByState(@Param("state") String state);

@Select("select state from city where country = #{country}")
TreeSet<String> treeSetStateByState(@Param("country") String country);

@Select("select state from city where country = #{country}")
HashSet<String> hashSetStateByState(@Param("country") String country);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
--

insert into city (name, state, country) values ('San Francisco', 'CA', 'US');
insert into city (name, state, country) values ('Jinan', 'ShanDong', 'CN');
insert into city (name, state, country) values ('Tsingtao', 'ShanDong', 'CN');
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ void test(OutputCapture outputCapture) {
SampleAnnotationApplication.main(new String[] {});
String output = outputCapture.toString();
assertThat(output).contains("1,San Francisco,CA,US");
assertThat(output).contains("2,Jinan,ShanDong,CN");
assertThat(output).contains("3,Tsingtao,ShanDong,CN");
assertThat(output).contains("{COUNTRY=CN, STATE=ShanDong, ID=2, NAME=Jinan}");
assertThat(output).contains("{COUNTRY=CN, STATE=ShanDong, ID=3, NAME=Tsingtao}");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class SampleMybatisApplicationTest {
void test(OutputCapture outputCapture) {
String output = outputCapture.toString();
assertThat(output).contains("1,San Francisco,CA,US");
assertThat(output).contains("2,Jinan,ShanDong,CN");
assertThat(output).contains("3,Tsingtao,ShanDong,CN");
assertThat(output).contains("{COUNTRY=CN, STATE=ShanDong, ID=2, NAME=Jinan}");
assertThat(output).contains("{COUNTRY=CN, STATE=ShanDong, ID=3, NAME=Tsingtao}");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
package sample.mybatis.graalvm.annotation.mapper;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import org.junit.jupiter.api.Test;
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
Expand Down Expand Up @@ -45,4 +51,64 @@ void findByStateTest() {
assertThat(city.getCountry()).isEqualTo("US");
}

@Test
void listByStateTest() {
List<City> cityList = cityMapper.listByState("ShanDong");
assertThat(cityList).isNotNull();
assertThat(cityList.size()).isEqualTo(2);

assertThat(cityList).satisfies(cities -> {
assertThat(cities).filteredOn(city -> city.getId() == 2 && "Jinan".equals(city.getName())
&& "ShanDong".equals(city.getState()) && "CN".equals(city.getCountry())).hasSize(1);
assertThat(cities).filteredOn(city -> city.getId() == 3 && "Tsingtao".equals(city.getName())
&& "ShanDong".equals(city.getState()) && "CN".equals(city.getCountry())).hasSize(1);
});
}

@Test
void mapByStateTest() {
Map<String, Object> map = cityMapper.mapByState("CA");
assertThat(map).isNotNull();
assertThat(map.size()).isEqualTo(4);
assertThat(map).satisfies(cities -> {
assertThat(cities)
.contains(entry("ID", 1), entry("NAME", "San Francisco"), entry("STATE", "CA"), entry("COUNTRY", "US"))
.isNotNull();
});
}

@Test
void listMapByStateTest() {
List<Map<String, Object>> cityList = cityMapper.listMapByState("ShanDong");
assertThat(cityList).isNotNull();
assertThat(cityList).satisfies(map -> {
assertThat(map).isNotNull();
assertThat(map.size()).isEqualTo(2);
assertThat(map).satisfies(cities -> {
assertThat(cities).filteredOn(city -> city.size() == 4 && city.get("ID") instanceof Integer
&& (Integer) city.get("ID") == 2 && "Jinan".equals(city.get("NAME")) && "ShanDong".equals(city.get("STATE"))
&& "CN".equals(city.get("COUNTRY"))).hasSize(1);
assertThat(cities).filteredOn(city -> city.size() == 4 && city.get("ID") instanceof Integer
&& (Integer) city.get("ID") == 3 && "Tsingtao".equals(city.get("NAME"))
&& "ShanDong".equals(city.get("STATE")) && "CN".equals(city.get("COUNTRY"))).hasSize(1);
});
});
}

@Test
void treeSetStateByStateTest() {
TreeSet<String> stateSet = cityMapper.treeSetStateByState("CN");
assertThat(stateSet).isNotNull();
assertThat(stateSet.size()).isEqualTo(1);
assertThat(stateSet.first()).isEqualTo("ShanDong");
}

@Test
void hashSetStateByStateTest() {
HashSet<String> stateSet = cityMapper.hashSetStateByState("CN");
assertThat(stateSet).isNotNull();
assertThat(stateSet.size()).isEqualTo(1);
assertThat(stateSet).contains("ShanDong");
}

}

0 comments on commit c98aa36

Please sign in to comment.