SQL dynamic query in SpringMVC

        The dynamic query is used in the project I am writing now, that is, to give several query conditions, select the query conditions you need, and leave it blank if you don’t need it, and implement the query statement according to the selected conditions.

        The front page will not be repeated, it is nothing more than a form, but by the way, when the form is empty, it does not mean that the value is null. For details, see the previous blog post

 JSP page pass value is space but not null and List is empty and not null 

        Then configure the corresponding Mapper, as long as you compare the corresponding parameters, the following is the corresponding Mapper.xml

<select id="queryByConditions" resultMap="pw.News">
        SELECT `id`,`title`,`author`,`type`,`time`
        FROM `t_news`
        <where>
            id != 0
            <if test="title != null">
                AND `title`
                LIKE concat('%',#{title},'%')
            </if>

            <if test="content != null">
                AND `content`
                LIKE concat('%',#{content},'%')
            </if>

            <if test="author != null">
                AND `author`
                LIKE concat('%',#{author},'%')
            </if>

            <if test="type != null">
                AND `type` = #{type}
            </if>
        </where>
        ORDER BY `time` DESC
    </select>

         above

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326604085&siteId=291194637