@SelectProvider中mybatis参数替换是如何工作的
我继承了一些我试图理解的代码,并且在 @SelectProvider
上进行的任何搜索都一无所获。
Java DAO
@SelectProvider(type = CategoryDaoSelectProvider.class, method = "findByParentIdAndName")
Category findByParentIdAndName(@Param("parentId") Long parentId, @Param("name") String name);
选择提供程序
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();
}
}
这段代码中的参数parentId 有何用途?据我所知,它实际上从未执行任何操作,除非以某种方式神奇地将 #{parentId} 替换为该值。在这种情况下是否不使用此参数? mybatis
实际上在哪里进行查询注入?
I've in inherited some code that I'm trying to understand and any searching I do to find something on @SelectProvider
turns up a whole lot of nothing.
Java DAO
@SelectProvider(type = CategoryDaoSelectProvider.class, method = "findByParentIdAndName")
Category findByParentIdAndName(@Param("parentId") Long parentId, @Param("name") String name);
Select Provider
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();
}
}
What purpose does the param parentId serve in this code? As far as I can tell it never actually does anything unless somehow magically the #{parentId} is replaced with the value. Is this param just not used in this situation? Where does mybatis
actually do the injections into the query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SelectProviders 通过两种方式接收参数:作为 params Map 参数中的项目和作为 #{parentId} (在您的示例中)。您的代码显示在 select 语句中使用parentId 之前先对其进行检查。需要parentId变量,因为您无法查询#{parentId}。
顺便说一句,这不是 SelectProviders 的最佳实现,您应该在最后使用 SELECT()、WHERE() 和 SQL() 来返回编译后的语句。我想你的例子也有效。
SelectProviders receive parameters in 2 ways: as items in the params Map argument and as #{parentId} (in your example). You code shows parentId being checked before it is used in the select statement. The parentId variable is needed because you can't query #{parentId}.
Btw, this isn't the best implementation of a SelectProviders, you're supposed to use SELECT(), WHERE() and SQL() at the end to return the compiled statement. I guess your example works too.
您可以将这段代码重写如下,这也许更清晰一些?
parentId
的实际值确实不是必需的,但需要该参数是否存在的信息。You could rewrite that piece of code as follows, which is perhaps a bit clearer? The actual value of
parentId
is indeed not necessary, but the info on whether or not the parameter is present is required.