繁体   English   中英

与Java接口一起使用的myBatis xml映射器文件管理器-动态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参数可以为null,或者列表中可以有多个值。

您是否遇到过这样的问题,并且您知道一些解决此问题的最佳方法吗? 这是我的第一次接触。

我正在使用MyBatis和Postgres DB。

首先,您可以删除#{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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM