Skip to content

Instantly share code, notes, and snippets.

@haijohn
Created May 13, 2025 15:03
Show Gist options
  • Save haijohn/52ee1e5cb5e1b7c09bdf2b44d30ee9c7 to your computer and use it in GitHub Desktop.
Save haijohn/52ee1e5cb5e1b7c09bdf2b44d30ee9c7 to your computer and use it in GitHub Desktop.
mybatis filter model
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.OlympicWinnerMapper">
<sql id="selectColumns">
*
</sql>
<sql id="fromTable">
FROM sample_data.olympic_winners
</sql>
<sql id="filterConditions">
<if test="filterModel != null">
<if test="filterModel.conditionOperator != null and filterModel.conditions != null and !filterModel.conditions.isEmpty()">
( <foreach collection="filterModel.conditions" item="nestedFilter" separator=" ${filterModel.conditionOperator} ">
<include refid="filterConditions">
<property name="filterModel" value="nestedFilter"/>
</include>
</foreach>
) </if>
<if test="filterModel.conditionOperator == null or filterModel.conditions == null or filterModel.conditions.isEmpty()">
<bind name="columnKey" value="key"/> <choose>
<when test="filterModel.filterType == 'text'">
<include refid="textFilter">
<property name="key" value="${columnKey}"/>
<property name="item" value="filterModel"/>
</include>
</when>
<when test="filterModel.filterType == 'number'">
<include refid="numberFilter">
<property name="key" value="${columnKey}"/>
<property name="item" value="filterModel"/>
</include>
</when>
</choose>
</if>
</if>
</sql>
<sql id="whereClause">
<where>
<if test="request.groupKeys != null and request.groupKeys.size() > 0">
<foreach collection="request.groupKeys" item="groupKey" index="index" separator=" AND ">
${request.rowGroupCols[index].field} = #{groupKey}
</foreach>
</if>
<if test="request.filterModel != null and !request.filterModel.isEmpty()">
<bind name="filterModelMap" value="request.filterModel"/>
<foreach collection="filterModelMap.entrySet()" item="entry">
<bind name="key" value="entry.key"/> <bind name="filterModel" value="entry.value"/> AND
<include refid="filterConditions">
<property name="filterModel" value="filterModel"/>
<property name="key" value="${key}"/> </include>
</foreach>
</if>
</where>
</sql>
<sql id="textFilter">
<choose>
<when test="item.type == 'equals'">
${key} = #{item.filter}
</when>
<when test="item.type == 'notEqual'">
${key} != #{item.filter}
</when>
<when test="item.type == 'contains'">
${key} LIKE CONCAT('%', #{item.filter}, '%')
</when>
<when test="item.type == 'notContains'">
${key} NOT LIKE CONCAT('%', #{item.filter}, '%')
</when>
<when test="item.type == 'startsWith'">
${key} LIKE CONCAT(#{item.filter}, '%')
</when>
<when test="item.type == 'endsWith'">
${key} LIKE CONCAT('%', #{item.filter})
</when>
<otherwise>
1=1 </otherwise>
</choose>
</sql>
<sql id="numberFilter">
<choose>
<when test="item.type == 'equals'">
${key} = #{item.filter}
</when>
<when test="item.type == 'notEqual'">
${key} != #{item.filter}
</when>
<when test="item.type == 'greaterThan'">
${key} > #{item.filter}
</when>
<when test="item.type == 'greaterThanOrEqual'">
${key} >= #{item.filter}
</when>
<when test="item.type == 'lessThan'">
${key} < #{item.filter}
</when>
<when test="item.type == 'lessThanOrEqual'">
${key} <= #{item.filter}
</when>
<when test="item.type == 'inRange'">
(${key} >= #{item.filter} AND #{item.filterTo})
</when>
<otherwise>
1=1 </otherwise>
</choose>
</sql>
<sql id="orderByClause">
<if test="request.sortModel != null and !request.sortModel.isEmpty()">
ORDER BY
<foreach collection="request.sortModel" item="sortItem" separator=", ">
${sortItem.colId} ${sortItem.sort}
</foreach>
</if>
</sql>
<sql id="limitClause">
LIMIT #{request.endRow - request.startRow} OFFSET #{request.startRow}
</sql>
<select id="getData" resultType="com.example.model.OlympicWinner">
SELECT
<include refid="selectColumns"/>
<include refid="fromTable"/>
<include refid="whereClause"/>
<include refid="orderByClause"/>
<include refid="limitClause"/>
</select>
<select id="getTotalRowCount" resultType="long">
SELECT COUNT(*)
<include refid="fromTable"/>
<include refid="whereClause"/>
</select>
</mapper>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment