@SelectProvider中mybatis参数替换是如何工作的

发布于 2024-12-27 13:50:51 字数 1091 浏览 1 评论 0原文

我继承了一些我试图理解的代码,并且在 @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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔一刀 2025-01-03 13:50:51

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.

才能让你更想念 2025-01-03 13:50:51

您可以将这段代码重写如下,这也许更清晰一些? parentId 的实际值确实不是必需的,但需要该参数是否存在的信息。

 // ...
 boolean hasIdParam = params.containsKey("parentId");

 StringBuffer buffer = new StringBuffer();
 buffer.append("SELECT COUNT(id) FROM Category ");

 if (!hasIdParam) {
    buffer.append("WHERE parentId IS NULL ");
 } else {
    buffer.append("WHERE parentId = #{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.

 // ...
 boolean hasIdParam = params.containsKey("parentId");

 StringBuffer buffer = new StringBuffer();
 buffer.append("SELECT COUNT(id) FROM Category ");

 if (!hasIdParam) {
    buffer.append("WHERE parentId IS NULL ");
 } else {
    buffer.append("WHERE parentId = #{parentId} ");
 }
 // ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文