在mybatis中使用查询if条件

时间:2021-06-25 22:15:07

标签: sql mybatis ibatis mybatis-mapper mybatis-sql

我正在学习mybatis。我可以使用 if 条件如下所示:

    // num is a property of employee
    @Inset({
        "<script>",
            "<if test = 'num == null'>",
                "INSERT into version values('333','REOPEN', 3)",
            "</if>",
            "<if test = 'num != null'>",
                "INSERT into version values(#{num},'REOPEN', 3)",
            "</if>",
        "</script>"
    })
    void save(Employee e);

现在我试图在我的 if 标签中使用一个查询,如下所示:

   @Insert({
        "<script>",
             "WITH (select * from version where id = #{num}) as a",
             "<if test = 'a == null'>",     
                  "insert into employee (num, id, name) values (#{num}, #{key}, #val})", 
             "</if>",
        "</script>"
    })
    void insert(Employee e);

这给出了以下运行时错误

org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'a' in 'class com.theopendle.demo.domain.Employee'
    at org.apache.ibatis.reflection.Reflector.getGetInvoker(Reflector.java:421) ~[mybatis-3.4.4.jar:3.4.4]

我尝试使用 IF NOT EXISTS 子句,但会出现编译时错误。

我正在尝试查找表中的值,如果该值不存在,那么我是否想插入新行。

我正在使用 Oracle 数据库。请帮忙。

0 个答案:

没有答案
相关问题