mybatis动态sql片段与分页,排序,传参的使用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis动态sql片段与分页,排序,传参的使用相关的知识,希望对你有一定的参考价值。

<?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">

<!-- namespace命名空间,作用就是对sql进行分类化管理,理解sql隔离 注意:使用mapper代理方法开发,namespace有特殊重要的作用 -->
<mapper namespace="danger.mapper.queryView.QueryViewCustomMapper">


    <select id="getDangerSpotByDangerId" resultType="danger.bean.queryView.DangerSpot"
        parameterType="int">
        SELECT * FROM danger_spot where dangerId=#{value}
    </select>


    <!-- 查询隐患四定视图开始 -->
    <select id="getDangerSidingCountByCondition" resultType="int"
        parameterType="hashmap">
        SELECT count(sidingid) FROM danger_siding
        <where>
            <include refid="query_where"></include>
        </where>
    </select>
    <select id="findDangerSidingByCondition" resultType="danger.bean.queryView.DangerSiding"
        parameterType="hashmap">
        SELECT * FROM danger_siding
        <where>
            <include refid="query_where"></include>
        </where>
        ORDER BY rectificationTime desc
        <include refid="query_page_limit"></include>
    </select>

    <select id="getDangerSidingBySidingId" resultType="danger.bean.queryView.DangerSiding"
        parameterType="int">
        select * from danger_siding where sidingid=#{value}
    </select>
    <!-- 查询隐患四定视图结束 -->


    <!-- 查询隐患四定跟踪视图开始 -->
    <select id="getDangerSidingFollowCountByCondition" resultType="int"
        parameterType="hashmap">
        SELECT count(sidingid) FROM danger_siding_follow
        <where>
            <include refid="query_where"></include>
        </where>
    </select>
    <select id="findDangerSidingFollowByCondition" resultType="danger.bean.queryView.DangerSidingFollow"
        parameterType="hashmap">
        SELECT * FROM `danger`.`danger_siding_follow`
        <where>
            <include refid="query_where"></include>
        </where>
        ORDER BY recheckTime desc
        <include refid="query_page_limit"></include>
    </select>

    <select id="getDangerSidingFollowBySidingId" resultType="danger.bean.queryView.DangerSidingFollow"
        parameterType="int">
        select * from danger_siding where sidingid=#{value}
    </select>
    <!-- 查询隐患四定跟踪视图结束 -->


    <!-- 查询隐患四定跟踪复查视图开始 -->
    <select id="getDangerSidingFollowRecheckCountByCondition"
        resultType="int" parameterType="hashmap">
        SELECT count(sidingid) FROM danger_siding_follow_recheck
        <where>
            <include refid="query_where"></include>
        </where>
    </select>
    <select id="findDangerSidingFollowRecheckByCondition"
        resultType="danger.bean.queryView.DangerSidingFollowRecheck"
        parameterType="hashmap">
        SELECT * FROM `danger`.`danger_siding_follow_recheck`
        <where>
            <include refid="query_where"></include>
        </where>
        ORDER BY unit    
        <include refid="query_page_limit"></include>
    </select>

    <select id="getDangerSidingFollowRecheckBySidingId" resultType="danger.bean.queryView.DangerSidingFollowRecheck"
        parameterType="int">
        select * from danger_siding where sidingid=#{value}
    </select>
    <!-- 查询隐患四定跟踪复查视图结束 -->






    <!-- 查询分页 -->
    <sql id="query_page_limit">
        <if test="index!=null">
            LIMIT #{index},#{currentCount}
        </if>
    </sql>
    <!-- 查询隐患的条件 -->
    <sql id="query_where">
        <if test="checkunit!=null">
            and checkunit=#{checkunit}
        </if>
        <if test="manager!=null">
            and manager=#{manager}
        </if>
        <if test="startTime!=null">
            and findTime > #{startTime}
        </if>
        <if test="endTime!=null">
            and findTime&lt;#{endTime}
        </if>
        <if test="dangergrade!=null">
            and dangergrade=#{dangergrade}
        </if>
        <if test="address!=null">
            and address like ‘%${address}%‘
        </if>
        <if test="content!=null">
            and content like ‘%${content}%‘
        </if>
        <if test="type!=null">
            and type=#{type}
        </if>
        <if test="dangerstatus">
            and dangerstatus=#{dangerstatus}
        </if>
        <if test="unit!=null">
            and unit=#{unit}
        </if>
    </sql>



</mapper>

 

<?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">

<!-- namespace命名空间,作用就是对sql进行分类化管理,理解sql隔离 注意:使用mapper代理方法开发,namespace有特殊重要的作用 -->
<mapper namespace="danger.mapper.queryView.QueryViewCustomMapper">


    <select id="getDangerSpotByDangerId" resultType="danger.bean.queryView.DangerSpot"
        parameterType="int">
        SELECT * FROM danger_spot where dangerId=#{value}
    </select>


    <!-- 查询隐患四定视图开始 -->
    <select id="getDangerSidingCountByCondition" resultType="int"
        parameterType="hashmap">
        SELECT count(sidingid) FROM danger_siding
        <where>
            <include refid="query_where"></include>
        </where>
    </select>
    <select id="findDangerSidingByCondition" resultType="danger.bean.queryView.DangerSiding"
        parameterType="hashmap">
        SELECT * FROM danger_siding
        <where>
            <include refid="query_where"></include>
        </where>
        ORDER BY rectificationTime desc
        <include refid="query_page_limit"></include>
    </select>

    <select id="getDangerSidingBySidingId" resultType="danger.bean.queryView.DangerSiding"
        parameterType="int">
        select * from danger_siding where sidingid=#{value}
    </select>
    <!-- 查询隐患四定视图结束 -->


    <!-- 查询隐患四定跟踪视图开始 -->
    <select id="getDangerSidingFollowCountByCondition" resultType="int"
        parameterType="hashmap">
        SELECT count(sidingid) FROM danger_siding_follow
        <where>
            <include refid="query_where"></include>
        </where>
    </select>
    <select id="findDangerSidingFollowByCondition" resultType="danger.bean.queryView.DangerSidingFollow"
        parameterType="hashmap">
        SELECT * FROM `danger`.`danger_siding_follow`
        <where>
            <include refid="query_where"></include>
        </where>
        ORDER BY recheckTime desc
        <include refid="query_page_limit"></include>
    </select>

    <select id="getDangerSidingFollowBySidingId" resultType="danger.bean.queryView.DangerSidingFollow"
        parameterType="int">
        select * from danger_siding where sidingid=#{value}
    </select>
    <!-- 查询隐患四定跟踪视图结束 -->


    <!-- 查询隐患四定跟踪复查视图开始 -->
    <select id="getDangerSidingFollowRecheckCountByCondition"
        resultType="int" parameterType="hashmap">
        SELECT count(sidingid) FROM danger_siding_follow_recheck
        <where>
            <include refid="query_where"></include>
        </where>
    </select>
    <select id="findDangerSidingFollowRecheckByCondition"
        resultType="danger.bean.queryView.DangerSidingFollowRecheck"
        parameterType="hashmap">
        SELECT * FROM `danger`.`danger_siding_follow_recheck`
        <where>
            <include refid="query_where"></include>
        </where>
        ORDER BY unit    
        <include refid="query_page_limit"></include>
    </select>

    <select id="getDangerSidingFollowRecheckBySidingId" resultType="danger.bean.queryView.DangerSidingFollowRecheck"
        parameterType="int">
        select * from danger_siding where sidingid=#{value}
    </select>
    <!-- 查询隐患四定跟踪复查视图结束 -->






    <!-- 查询分页 -->
    <sql id="query_page_limit">
        <if test="index!=null">
            LIMIT #{index},#{currentCount}
        </if>
    </sql>
    <!-- 查询隐患的条件 -->
    <sql id="query_where">
        <if test="checkunit!=null">
            and checkunit=#{checkunit}
        </if>
        <if test="manager!=null">
            and manager=#{manager}
        </if>
        <if test="startTime!=null">
            and findTime > #{startTime}
        </if>
        <if test="endTime!=null">
            and findTime&lt;#{endTime}
        </if>
        <if test="dangergrade!=null">
            and dangergrade=#{dangergrade}
        </if>
        <if test="address!=null">
            and address like ‘%${address}%‘
        </if>
        <if test="content!=null">
            and content like ‘%${content}%‘
        </if>
        <if test="type!=null">
            and type=#{type}
        </if>
        <if test="dangerstatus">
            and dangerstatus=#{dangerstatus}
        </if>
        <if test="unit!=null">
            and unit=#{unit}
        </if>
    </sql>



</mapper>

以上是关于mybatis动态sql片段与分页,排序,传参的使用的主要内容,如果未能解决你的问题,请参考以下文章

mybatis中#{}和${}传参的区别

MyBatis使用MyBatis的分页组件PageHelper时,多表关联下使用别名查询时,前台传参过来,根据参数排序的解决方案

Mysql第五期 排序与分页

Mysql 排序Order by与分页limit结合的数据异常问题

Oracle分页排序的实现与分页数据重复问题

Laravel orderBy 与分页和关系