Suppose you have the following table "shops"
shop_cd | prefecture |
---|---|
0001 | Kanagawa Prefecture |
0002 | Kanagawa Prefecture |
0002 | Kanagawa Prefecture |
0003 | Kanagawa Prefecture |
0003 | Kanagawa Prefecture |
0004 | Tokyo |
I want a unique list of store codes that specify "Kanagawa Prefecture".
public interface ShopMapper {
/**
*Get a list of store codes by specifying a prefecture.
*
* @return List<Map<String, String>>
*Map per result
* - prefecture:Specified prefecture name (Japanese)
* - shopCd:site code
*/
@SelectProvider(type = SqlProvider.class, method = "getShopCodesByPrefecture")
@Results(id = "ShopCodesByPrefecture", value = {
@Result(column = "shop_cd", property = "shopCd"),
@Result(column = "prefecture", property = "prefecture"),
})
List<Map<String, String>> getShopCodesByPrefecture(String brandCd, String prefecture);
class SqlProvider {
public String getShopCodesByPrefecture(String prefecture) {
SQL sql = new SQL() {
{
SELECT("shop_cd", "prefecture");
FROM("shops");
WHERE("shops.prefecture = #{prefecture}");
GROUP_BY("shop_cd");
ORDER_BY("shop_cd ASC");
}
};
return sql.toString();
}
}
}
I specified @Results and @Result so that the search results can be obtained on the Map.
Recommended Posts