myBatis xml 映射器文件管理器与 Java 接口一起使用 - 动态 SQL 查询

myBatis xml mapper filer in use with Java interface - dynamic SQL query

我有问题。我没有创建 MyBatis 配置。我听说没有它可能会起作用。例如,我有 Java 接口调用:InterfaceDAO.java 和 myBatis 映射器 InterfaceDAO.xml

InterfaceDAO.java :

@Mapper
public interface InterfaceDAO extends ApiConsumerDAO, ServiceDAO {

@Select("SELECT * FROM interface WHERE id = #{interfaceId}")
@Results({
        @Result(property = "id", column = "id"),
        @Result(property = "date", column = "date"),
        @Result(property = "apiConsumer", column = "api_consumer", one = @One(select = "getApiConsumer")),
        @Result(property = "service", column = "service", one = @One(select = "getService")),
        @Result(property = "counterStatus", column = "counter_status"),
        @Result(property = "ratingProcessId", column = "rating_process_id"),
        @Result(property = "value", column = "value"),
        @Result(property = "createdDate", column = "created_date"),
        @Result(property = "modifiedDate", column = "modified_date")
})
InterfaceObject getInterfaceDAO(@Param("interfaceId") Integer interfaceId);


List<InterfaceObject > getInterfaceDAOList(@Param("apiConsumerIdsList") List<Integer> apiConsumerIdsList,
                                       @Param("serviceIdsList") List<Integer> serviceIdsList,
                                       @Param("dateFrom") Date dateFrom,
                                       @Param("dateTo") Date dateTo,
                                       @Param("status") InterfaceDAO.Status status);
}

InterfaceDAO.xml :

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="pl.net.manager.dao.InterfaceDAO">
    <select id="getInterfaceDAOList" parameterType="map" resultType="List">
        SELECT * FROM interface WHERE
        <if test="apiConsumerIdsList != null">
            #{apiConsumerIdsList} IS NULL OR api_consumer IN (#{apiConsumerIdsList,jdbcType=ARRAY})
        </if>
        <if test="serviceIdsList != null">
            #{serviceIdsList} IS NULL OR service IN (#{serviceIdsList,jdbcType=ARRAY})
        </if>
        <if test="status != null">
            #{status} IS NULL OR status IN (#{status,jdbcType=ARRAY})
        </if>
        <if test="dateFrom != null">
            #{dateFrom} IS NULL OR date &gt;= #{dateFrom,jdbcType=DATE}
        </if>
        <if test="dateTo != null">
            #{dateTo} IS NULL OR date &lt;= (#{dateTo,jdbcType=DATE})
        </if>
    </select>
</mapper>

所以第一个方法示例称为:getInterfaceDAO 正在正常工作。但是第二个调用:getInterfaceDAOList 更复杂,并且与第一个的工作方式不同。这就是为什么我希望这个特定的方法使用 xml 映射器来获取我想要的数据。 income 参数可以为空,或者列表中可以有多个值。

你遇到过这样的问题吗?你知道解决这个问题的最佳方法吗?这是我第一次接触这个。

我正在使用 MyBatis 和 Postgres 数据库。

首先,您可以删除 #{apiConsumerIdsList} IS NULL 和类似事件,因为您已经在 if 条件中检查了 != null,这就足够了。

其次,对于 IN 子句,您必须使用 foreach 结构,如下所示:

<foreach item="item" index="index" collection="apiConsumerIdsList" 
    open="(" separator="," close=")">
  #{item}
</foreach>

以上适用于所有IN条款。

而表达式 #{dateFrom,jdbcType=DATE} 可以简单地写成 #{dateFrom:DATE}

然后来到WHERE子句,如果多个if条件满足,那么你的WHERE子句就会因为没有AND而失效,因为你可以使用 <where> 标签,比如

<where>
    <if test="apiConsumerIdsList != null">
    api_consumer IN <foreach item="item" collection="apiConsumerIdsList" 
            open="(" separator="," close=")">
          #{item}
        </foreach>
    </if>
    <if test="serviceIdsList != null">
        AND service IN <foreach item="item" collection="serviceIdsList" 
                open="(" separator="," close=")">
              #{item}
            </foreach>(#{serviceIdsList,jdbcType=ARRAY})
    </if>
    <if test="status != null">
        AND status IN <foreach item="item" collection="status" 
                open="(" separator="," close=")">
              #{item}
            </foreach>
    </if>
    <if test="dateFrom != null">
        AND date &gt;= #{dateFrom:DATE}
    </if>
    <if test="dateTo != null">
        AND date &lt;= (#{dateTo:DATE})
    </if>
</where>