`
BryanMelody
  • 浏览: 60667 次
  • 性别: Icon_minigender_1
  • 来自: 长春
社区版块
存档分类
最新评论

SpringMVC中的SQL动态查询

阅读更多

        在现在写的项目中用到了动态查询,就是给出数个查询条件,在其中选择自己需要的查询条件,如果不需要则不填,根据选择的条件实现查询语句

        前台页面不再赘述,无非是表单,不过顺便一提,表单为空时不代表该值为null,详情可见之前博文

 JSP页面传值为空格而不为null以及List为空不为null 

        之后配置相应Mapper,只要对照相应参数便可,以下是对应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>

         以上

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics