MyBatis Select 生成器
MyBatis Select Builder
我正在尝试提出一个动态查询,并且根据我所做的研究,看起来我最好的选择是 select 生成器,因为我想使用注释而不是 XML。
我有两个参数;我想传递给此查询生成器方法的列表和对象。我可以毫无问题地使用对象,但无法从方法访问列表。
这是我的映射器界面:
@SelectProvider(type = ProductProvider.class, method = "getProductByBrandByCategory")
List<Product> getProductListByBrandByCategory(@Param("brandList") List<Brand> brandList, @Param("category") Category category);
这是 ProductProvider class 上的 getProductByBrandByCategory 方法:
public String getProductByBrandByCategory(Map params) {
return new SQL() {{
SELECT("*");
FROM("product");
WHERE("category_id = #{category.id}");
}}.toString();
}
基本上,我尝试遍历列表以根据列表中的元素添加 and/or/where 语句。
有什么方法可以访问 brandList 元素吗?我的猜测是它是 Maps 参数的一部分,但不知道如何访问它。
提前致谢。
有关如何获取集合中的参数的示例,请参阅 this question。
引用问题的代码示例
public class CategoryDaoSelectProvider {
public static String findByParentIdAndName(Map<String, Object> params) {
Long parentId = (Long)params.get("parentId"); // WHY IS THIS HERE???
StringBuffer buffer = new StringBuffer();
buffer.append("SELECT COUNT(id) FROM Category ");
if (parentId == null) {
buffer.append("WHERE parentId IS NULL ");
} else {
buffer.append("WHERE parentId = #{parentId} ");
}
buffer.append("AND LOWER(name) = LOWER(#{name}) ");
return buffer.toString();
}
}
其他解决方案是
您可以使用 MyBatis Velocity 来使用 Apache Velocity 脚本
生成动态 SQL 查询的语言。
您也可以使用基于 XML 的映射器定义,它们非常适合您的情况
引用 offical documentation,部分 'Mapper Annotations'
Java Annotations are unfortunately limited in their expressiveness and flexibility. Despite a lot of time spent in investigation, design and trials, the most powerful MyBatis mappings simply cannot be built with Annotations
根据我的商业经验,myBatis XML 配置工作得非常好(我们将 myBatis 与 Spring 一起使用)。数量样板代码是最少的——只有查询和 resultMaps。 XML 语法易于阅读,即使在 XML 标记(if、foreach 等)中有一些逻辑。基于注解的配置需要复杂的嵌套注解,即使是最简单的映射也不适合我。我们仍然有一些映射器的注释,但对于新代码,我更喜欢 XML.
IntelliJ IDEA 将映射器接口方法与 XML SQL 定义链接起来,因此导航很容易。
我正在尝试提出一个动态查询,并且根据我所做的研究,看起来我最好的选择是 select 生成器,因为我想使用注释而不是 XML。
我有两个参数;我想传递给此查询生成器方法的列表和对象。我可以毫无问题地使用对象,但无法从方法访问列表。
这是我的映射器界面:
@SelectProvider(type = ProductProvider.class, method = "getProductByBrandByCategory")
List<Product> getProductListByBrandByCategory(@Param("brandList") List<Brand> brandList, @Param("category") Category category);
这是 ProductProvider class 上的 getProductByBrandByCategory 方法:
public String getProductByBrandByCategory(Map params) {
return new SQL() {{
SELECT("*");
FROM("product");
WHERE("category_id = #{category.id}");
}}.toString();
}
基本上,我尝试遍历列表以根据列表中的元素添加 and/or/where 语句。
有什么方法可以访问 brandList 元素吗?我的猜测是它是 Maps 参数的一部分,但不知道如何访问它。
提前致谢。
有关如何获取集合中的参数的示例,请参阅 this question。 引用问题的代码示例
public class CategoryDaoSelectProvider {
public static String findByParentIdAndName(Map<String, Object> params) {
Long parentId = (Long)params.get("parentId"); // WHY IS THIS HERE???
StringBuffer buffer = new StringBuffer();
buffer.append("SELECT COUNT(id) FROM Category ");
if (parentId == null) {
buffer.append("WHERE parentId IS NULL ");
} else {
buffer.append("WHERE parentId = #{parentId} ");
}
buffer.append("AND LOWER(name) = LOWER(#{name}) ");
return buffer.toString();
}
}
其他解决方案是
您可以使用 MyBatis Velocity 来使用 Apache Velocity 脚本 生成动态 SQL 查询的语言。
您也可以使用基于 XML 的映射器定义,它们非常适合您的情况
引用 offical documentation,部分 'Mapper Annotations'
Java Annotations are unfortunately limited in their expressiveness and flexibility. Despite a lot of time spent in investigation, design and trials, the most powerful MyBatis mappings simply cannot be built with Annotations
根据我的商业经验,myBatis XML 配置工作得非常好(我们将 myBatis 与 Spring 一起使用)。数量样板代码是最少的——只有查询和 resultMaps。 XML 语法易于阅读,即使在 XML 标记(if、foreach 等)中有一些逻辑。基于注解的配置需要复杂的嵌套注解,即使是最简单的映射也不适合我。我们仍然有一些映射器的注释,但对于新代码,我更喜欢 XML.
IntelliJ IDEA 将映射器接口方法与 XML SQL 定义链接起来,因此导航很容易。