代码之家  ›  专栏  ›  技术社区  ›  xross

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

  •  2
  • xross  · 技术社区  · 7 年前

    我有问题。我没有创建MyBatis配置。我听说没有它也行。我有Java接口,例如: InterfaceDAO.java 和myBatis mapper InterfaceDAO.xml

    接口DAO。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);
    }
    

    接口DAO。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>
    

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

    我正在使用MyBatis和Postgres DB。

    1 回复  |  直到 7 年前
        1
  •  3
  •   MohamedSanaulla    7 年前

    首先,您可以删除 #{apiConsumerIdsList} IS NULL 因为你已经检查过了 != null 在你的 if 条件,这应该足够了。

    其次,对于 IN 条款您必须使用 foreach

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

    以上适用于所有 条款。

    和表达式 #{dateFrom,jdbcType=DATE} #{dateFrom:DATE}

    然后来到 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>