476 lines
22 KiB
XML
476 lines
22 KiB
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="com.example.mapper.UserMapper">
|
|
|
|
<!-- 结果映射 -->
|
|
<resultMap id="userResultMap" type="com.example.model.User">
|
|
<id property="id" column="id"/>
|
|
<result property="username" column="username"/>
|
|
<result property="password" column="password"/>
|
|
<result property="email" column="email"/>
|
|
<result property="phone" column="phone"/>
|
|
<result property="fullName" column="full_name"/>
|
|
<result property="createdAt" column="created_at"/>
|
|
<result property="updatedAt" column="updated_at"/>
|
|
<result property="active" column="active"/>
|
|
</resultMap>
|
|
|
|
<!-- 基础列 -->
|
|
<sql id="baseColumns">
|
|
id, username, password, email, phone, full_name, created_at, updated_at, active
|
|
</sql>
|
|
|
|
<!-- 高级搜索条件 -->
|
|
<sql id="advancedSearchCondition">
|
|
<where>
|
|
<if test="criteria != null and criteria.size() > 0">
|
|
<foreach collection="criteria" item="criterion" index="index">
|
|
<choose>
|
|
<when test="index == 0">
|
|
<include refid="criterionFragment"/>
|
|
</when>
|
|
<otherwise>
|
|
<choose>
|
|
<when test="criterion.logicalOperator == 'OR'">
|
|
OR <include refid="criterionFragment"/>
|
|
</when>
|
|
<otherwise>
|
|
AND <include refid="criterionFragment"/>
|
|
</otherwise>
|
|
</choose>
|
|
</otherwise>
|
|
</choose>
|
|
</foreach>
|
|
</if>
|
|
</where>
|
|
</sql>
|
|
|
|
<!-- 单个条件片段 -->
|
|
<sql id="criterionFragment">
|
|
<choose>
|
|
<!-- 嵌套条件 -->
|
|
<when test="criterion.subCriteria != null and criterion.subCriteria.size() > 0">
|
|
(
|
|
<foreach collection="criterion.subCriteria" item="subCriterion" index="subIndex">
|
|
<choose>
|
|
<when test="subIndex == 0">
|
|
<include refid="subCriterionFragment"/>
|
|
</when>
|
|
<otherwise>
|
|
<choose>
|
|
<when test="criterion.logicalOperator == 'OR'">
|
|
OR <include refid="subCriterionFragment"/>
|
|
</when>
|
|
<otherwise>
|
|
AND <include refid="subCriterionFragment"/>
|
|
</otherwise>
|
|
</choose>
|
|
</otherwise>
|
|
</choose>
|
|
</foreach>
|
|
)
|
|
</when>
|
|
<!-- 单个条件 -->
|
|
<otherwise>
|
|
<choose>
|
|
<!-- 等于 -->
|
|
<when test="criterion.operator == 'EQ' and criterion.field == 'username'">
|
|
username = #{criterion.value}
|
|
</when>
|
|
<when test="criterion.operator == 'EQ' and criterion.field == 'email'">
|
|
email = #{criterion.value}
|
|
</when>
|
|
<when test="criterion.operator == 'EQ' and criterion.field == 'phone'">
|
|
phone = #{criterion.value}
|
|
</when>
|
|
<when test="criterion.operator == 'EQ' and criterion.field == 'fullName'">
|
|
full_name = #{criterion.value}
|
|
</when>
|
|
<when test="criterion.operator == 'EQ' and criterion.field == 'active'">
|
|
active = #{criterion.value}
|
|
</when>
|
|
|
|
<!-- 不等于 -->
|
|
<when test="criterion.operator == 'NEQ' and criterion.field == 'username'">
|
|
username != #{criterion.value}
|
|
</when>
|
|
<when test="criterion.operator == 'NEQ' and criterion.field == 'email'">
|
|
email != #{criterion.value}
|
|
</when>
|
|
<when test="criterion.operator == 'NEQ' and criterion.field == 'phone'">
|
|
phone != #{criterion.value}
|
|
</when>
|
|
<when test="criterion.operator == 'NEQ' and criterion.field == 'fullName'">
|
|
full_name != #{criterion.value}
|
|
</when>
|
|
<when test="criterion.operator == 'NEQ' and criterion.field == 'active'">
|
|
active != #{criterion.value}
|
|
</when>
|
|
|
|
<!-- 包含 -->
|
|
<when test="criterion.operator == 'LIKE' and criterion.field == 'username'">
|
|
username LIKE '%' || #{criterion.value} || '%'
|
|
</when>
|
|
<when test="criterion.operator == 'LIKE' and criterion.field == 'email'">
|
|
email LIKE '%' || #{criterion.value} || '%'
|
|
</when>
|
|
<when test="criterion.operator == 'LIKE' and criterion.field == 'phone'">
|
|
phone LIKE '%' || #{criterion.value} || '%'
|
|
</when>
|
|
<when test="criterion.operator == 'LIKE' and criterion.field == 'fullName'">
|
|
full_name LIKE '%' || #{criterion.value} || '%'
|
|
</when>
|
|
|
|
<!-- 开始于 -->
|
|
<when test="criterion.operator == 'START' and criterion.field == 'username'">
|
|
username LIKE #{criterion.value} || '%'
|
|
</when>
|
|
<when test="criterion.operator == 'START' and criterion.field == 'email'">
|
|
email LIKE #{criterion.value} || '%'
|
|
</when>
|
|
<when test="criterion.operator == 'START' and criterion.field == 'phone'">
|
|
phone LIKE #{criterion.value} || '%'
|
|
</when>
|
|
<when test="criterion.operator == 'START' and criterion.field == 'fullName'">
|
|
full_name LIKE #{criterion.value} || '%'
|
|
</when>
|
|
|
|
<!-- 结束于 -->
|
|
<when test="criterion.operator == 'END' and criterion.field == 'username'">
|
|
username LIKE '%' || #{criterion.value}
|
|
</when>
|
|
<when test="criterion.operator == 'END' and criterion.field == 'email'">
|
|
email LIKE '%' || #{criterion.value}
|
|
</when>
|
|
<when test="criterion.operator == 'END' and criterion.field == 'phone'">
|
|
phone LIKE '%' || #{criterion.value}
|
|
</when>
|
|
<when test="criterion.operator == 'END' and criterion.field == 'fullName'">
|
|
full_name LIKE '%' || #{criterion.value}
|
|
</when>
|
|
|
|
<!-- 大于 -->
|
|
<when test="criterion.operator == 'GT' and criterion.field == 'createdAt'">
|
|
created_at > #{criterion.value}
|
|
</when>
|
|
<when test="criterion.operator == 'GT' and criterion.field == 'updatedAt'">
|
|
updated_at > #{criterion.value}
|
|
</when>
|
|
|
|
<!-- 大于等于 -->
|
|
<when test="criterion.operator == 'GTE' and criterion.field == 'createdAt'">
|
|
created_at >= #{criterion.value}
|
|
</when>
|
|
<when test="criterion.operator == 'GTE' and criterion.field == 'updatedAt'">
|
|
updated_at >= #{criterion.value}
|
|
</when>
|
|
|
|
<!-- 小于 -->
|
|
<when test="criterion.operator == 'LT' and criterion.field == 'createdAt'">
|
|
created_at **<** #{criterion.value}
|
|
</when>
|
|
<when test="criterion.operator == 'LT' and criterion.field == 'updatedAt'">
|
|
updated_at **<** #{criterion.value}
|
|
</when>
|
|
|
|
<!-- 小于等于 -->
|
|
<when test="criterion.operator == 'LTE' and criterion.field == 'createdAt'">
|
|
created_at **<**= #{criterion.value}
|
|
</when>
|
|
<when test="criterion.operator == 'LTE' and criterion.field == 'updatedAt'">
|
|
updated_at **<**= #{criterion.value}
|
|
</when>
|
|
<!-- 在...之间 -->
|
|
<when test="criterion.operator == 'BETWEEN' and criterion.field == 'createdAt'">
|
|
<!-- DEBUG: 根据ID查询用户 #{criterion.value[0]} -->
|
|
<!-- created_at BETWEEN #{criterion.value[0],jdbcType=TIMESTAMP} AND #{criterion.value[1],jdbcType=TIMESTAMP} -->
|
|
<!-- created_at BETWEEN '${criterion.value[0]}'::timestamp AND '${criterion.value[1]}'::timestamp -->
|
|
<!-- (created_at >= to_timestamp(#{criterion.value[0]}, 'YYYY-MM-DD"T"HH24:MI:SS')
|
|
AND created_at **<**= to_timestamp(#{criterion.value[1]}, 'YYYY-MM-DD"T"HH24:MI:SS')) -->
|
|
<!-- created_at BETWEEN to_timestamp(#{criterion.value[0]}, 'YYYY-MM-DD"T"HH24:MI:SS') AND
|
|
to_timestamp(#{criterion.value[1]}, 'YYYY-MM-DD"T"HH24:MI:SS') -->
|
|
<!-- created_at BETWEEN
|
|
to_timestamp(#{criterion.value[0]}::text, 'YYYY-MM-DD"T"HH24:MI:SS') AND
|
|
to_timestamp(#{criterion.value[1]}::text, 'YYYY-MM-DD"T"HH24:MI:SS') -->
|
|
<!-- 'created_at' BETWEEN
|
|
(SELECT to_timestamp(#{criterion.value[0]}, 'YYYY-MM-DD"T"HH24:MI:SS')) AND
|
|
(SELECT to_timestamp(#{criterion.value[1]}, 'YYYY-MM-DD"T"HH24:MI:SS')) -->
|
|
<!-- created_at BETWEEN
|
|
(#{criterion.value[0]}::timestamp with time zone) AND
|
|
(#{criterion.value[1]}::timestamp with time zone) -->
|
|
created_at BETWEEN #{criterion.value[0]} AND #{criterion.value[1]}
|
|
</when>
|
|
<when test="criterion.operator == 'BETWEEN' and criterion.field == 'updatedAt'">
|
|
updated_at BETWEEN #{criterion.value[0]} AND #{criterion.value[1]}
|
|
</when>
|
|
|
|
<!-- 为空 -->
|
|
<when test="criterion.operator == 'NULL' and criterion.field == 'email'">
|
|
email IS NULL
|
|
</when>
|
|
<when test="criterion.operator == 'NULL' and criterion.field == 'phone'">
|
|
phone IS NULL
|
|
</when>
|
|
<when test="criterion.operator == 'NULL' and criterion.field == 'fullName'">
|
|
full_name IS NULL
|
|
</when>
|
|
|
|
<!-- 不为空 -->
|
|
<when test="criterion.operator == 'NOT_NULL' and criterion.field == 'email'">
|
|
email IS NOT NULL
|
|
</when>
|
|
<when test="criterion.operator == 'NOT_NULL' and criterion.field == 'phone'">
|
|
phone IS NOT NULL
|
|
</when>
|
|
<when test="criterion.operator == 'NOT_NULL' and criterion.field == 'fullName'">
|
|
full_name IS NOT NULL
|
|
</when>
|
|
|
|
<!-- 在列表中 -->
|
|
<when test="criterion.operator == 'IN' and criterion.field == 'username'">
|
|
username IN
|
|
<foreach collection="criterion.value" item="item" open="(" separator="," close=")">
|
|
#{item}
|
|
</foreach>
|
|
</when>
|
|
<when test="criterion.operator == 'IN' and criterion.field == 'email'">
|
|
email IN
|
|
<foreach collection="criterion.value" item="item" open="(" separator="," close=")">
|
|
#{item}
|
|
</foreach>
|
|
</when>
|
|
</choose>
|
|
</otherwise>
|
|
</choose>
|
|
</sql>
|
|
|
|
<!-- 子条件片段 -->
|
|
<sql id="subCriterionFragment">
|
|
<choose>
|
|
<!-- 等于 -->
|
|
<when test="subCriterion.operator == 'EQ' and subCriterion.field == 'username'">
|
|
username = #{subCriterion.value}
|
|
</when>
|
|
<when test="subCriterion.operator == 'EQ' and subCriterion.field == 'email'">
|
|
email = #{subCriterion.value}
|
|
</when>
|
|
<when test="subCriterion.operator == 'EQ' and subCriterion.field == 'phone'">
|
|
phone = #{subCriterion.value}
|
|
</when>
|
|
<when test="subCriterion.operator == 'EQ' and subCriterion.field == 'fullName'">
|
|
full_name = #{subCriterion.value}
|
|
</when>
|
|
<when test="subCriterion.operator == 'EQ' and subCriterion.field == 'active'">
|
|
active = #{subCriterion.value}
|
|
</when>
|
|
|
|
<!-- 不等于 -->
|
|
<when test="subCriterion.operator == 'NEQ' and subCriterion.field == 'username'">
|
|
username != #{subCriterion.value}
|
|
</when>
|
|
<when test="subCriterion.operator == 'NEQ' and subCriterion.field == 'email'">
|
|
email != #{subCriterion.value}
|
|
</when>
|
|
<when test="subCriterion.operator == 'NEQ' and subCriterion.field == 'phone'">
|
|
phone != #{subCriterion.value}
|
|
</when>
|
|
<when test="subCriterion.operator == 'NEQ' and subCriterion.field == 'fullName'">
|
|
full_name != #{subCriterion.value}
|
|
</when>
|
|
<when test="subCriterion.operator == 'NEQ' and subCriterion.field == 'active'">
|
|
active != #{subCriterion.value}
|
|
</when>
|
|
|
|
<!-- 包含 -->
|
|
<when test="subCriterion.operator == 'LIKE' and subCriterion.field == 'username'">
|
|
username LIKE '%' || #{subCriterion.value} || '%'
|
|
</when>
|
|
<when test="subCriterion.operator == 'LIKE' and subCriterion.field == 'email'">
|
|
email LIKE '%' || #{subCriterion.value} || '%'
|
|
</when>
|
|
<when test="subCriterion.operator == 'LIKE' and subCriterion.field == 'phone'">
|
|
phone LIKE '%' || #{subCriterion.value} || '%'
|
|
</when>
|
|
<when test="subCriterion.operator == 'LIKE' and subCriterion.field == 'fullName'">
|
|
full_name LIKE '%' || #{subCriterion.value} || '%'
|
|
</when>
|
|
|
|
<!-- 其他操作符... -->
|
|
</choose>
|
|
</sql>
|
|
|
|
<!-- 根据ID查询用户 -->
|
|
<select id="findById" resultMap="userResultMap">
|
|
SELECT <include refid="baseColumns"/>
|
|
FROM users
|
|
WHERE id = #{id}
|
|
</select>
|
|
|
|
<!-- 查询所有用户 -->
|
|
<select id="findAll" resultMap="userResultMap">
|
|
SELECT <include refid="baseColumns"/>
|
|
FROM users
|
|
</select>
|
|
|
|
<!-- 分页查询用户 -->
|
|
<select id="findByPage" resultMap="userResultMap">
|
|
SELECT <include refid="baseColumns"/>
|
|
FROM users
|
|
<where>
|
|
<if test="filters != null">
|
|
<if test="filters.username != null">
|
|
AND username LIKE '%' || #{filters.username} || '%'
|
|
</if>
|
|
<if test="filters.email != null">
|
|
AND email LIKE '%' || #{filters.email} || '%'
|
|
</if>
|
|
<if test="filters.phone != null">
|
|
AND phone LIKE '%' || #{filters.phone} || '%'
|
|
</if>
|
|
<if test="filters.fullName != null">
|
|
AND full_name LIKE '%' || #{filters.fullName} || '%'
|
|
</if>
|
|
<if test="filters.active != null">
|
|
AND active = #{filters.active}
|
|
</if>
|
|
<if test="filters.createdAtStart != null">
|
|
AND created_at >= #{filters.createdAtStart}
|
|
</if>
|
|
<if test="filters.createdAtEnd != null">
|
|
AND created_at <= #{filters.createdAtEnd}
|
|
</if>
|
|
</if>
|
|
</where>
|
|
<if test="sortField != null and sortOrder != null">
|
|
ORDER BY
|
|
<choose>
|
|
<when test="sortField == 'username'">username</when>
|
|
<when test="sortField == 'email'">email</when>
|
|
<when test="sortField == 'phone'">phone</when>
|
|
<when test="sortField == 'fullName'">full_name</when>
|
|
<when test="sortField == 'createdAt'">created_at</when>
|
|
<when test="sortField == 'updatedAt'">updated_at</when>
|
|
<otherwise>id</otherwise>
|
|
</choose>
|
|
<choose>
|
|
<when test="sortOrder == 'asc'">ASC</when>
|
|
<otherwise>DESC</otherwise>
|
|
</choose>
|
|
</if>
|
|
<if test="sortField == null or sortOrder == null">
|
|
ORDER BY id DESC
|
|
</if>
|
|
LIMIT #{limit} OFFSET #{offset}
|
|
</select>
|
|
|
|
<!-- 高级搜索用户 -->
|
|
<select id="advancedSearch" resultMap="userResultMap">
|
|
SELECT <include refid="baseColumns"/>
|
|
FROM users
|
|
<include refid="advancedSearchCondition"/>
|
|
<if test="sortField != null and sortOrder != null">
|
|
ORDER BY
|
|
<choose>
|
|
<when test="sortField == 'username'">username</when>
|
|
<when test="sortField == 'email'">email</when>
|
|
<when test="sortField == 'phone'">phone</when>
|
|
<when test="sortField == 'fullName'">full_name</when>
|
|
<when test="sortField == 'createdAt'">created_at</when>
|
|
<when test="sortField == 'updatedAt'">updated_at</when>
|
|
<otherwise>id</otherwise>
|
|
</choose>
|
|
<choose>
|
|
<when test="sortOrder == 'asc'">ASC</when>
|
|
<otherwise>DESC</otherwise>
|
|
</choose>
|
|
</if>
|
|
<if test="sortField == null or sortOrder == null">
|
|
ORDER BY id DESC
|
|
</if>
|
|
LIMIT #{limit} OFFSET #{offset}
|
|
</select>
|
|
|
|
<!-- 统计符合高级搜索条件的用户总数 -->
|
|
<select id="countByAdvancedCriteria" resultType="long">
|
|
SELECT COUNT(*)
|
|
FROM users
|
|
<include refid="advancedSearchCondition"/>
|
|
</select>
|
|
|
|
<!-- 统计符合条件的用户总数 -->
|
|
<select id="countByFilters" resultType="long">
|
|
SELECT COUNT(*)
|
|
FROM users
|
|
<where>
|
|
<if test="filters != null">
|
|
<if test="filters.username != null">
|
|
AND username LIKE '%' || #{filters.username} || '%'
|
|
</if>
|
|
<if test="filters.email != null">
|
|
AND email LIKE '%' || #{filters.email} || '%'
|
|
</if>
|
|
<if test="filters.phone != null">
|
|
AND phone LIKE '%' || #{filters.phone} || '%'
|
|
</if>
|
|
<if test="filters.fullName != null">
|
|
AND full_name LIKE '%' || #{filters.fullName} || '%'
|
|
</if>
|
|
<if test="filters.active != null">
|
|
AND active = #{filters.active}
|
|
</if>
|
|
<if test="filters.createdAtStart != null">
|
|
AND created_at >= #{filters.createdAtStart}
|
|
</if>
|
|
<if test="filters.createdAtEnd != null">
|
|
AND created_at <= #{filters.createdAtEnd}
|
|
</if>
|
|
</if>
|
|
</where>
|
|
</select>
|
|
|
|
<!-- 插入用户 -->
|
|
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
|
|
INSERT INTO users (
|
|
username, password, email, phone, full_name, created_at, updated_at, active
|
|
) VALUES (
|
|
#{username}, #{password}, #{email}, #{phone}, #{fullName},
|
|
COALESCE(#{createdAt}, CURRENT_TIMESTAMP),
|
|
COALESCE(#{updatedAt}, CURRENT_TIMESTAMP),
|
|
COALESCE(#{active}, true)
|
|
)
|
|
</insert>
|
|
|
|
<!-- 更新用户 -->
|
|
<update id="update">
|
|
UPDATE users
|
|
<set>
|
|
<if test="username != null">username = #{username},</if>
|
|
<if test="password != null">password = #{password},</if>
|
|
<if test="email != null">email = #{email},</if>
|
|
<if test="phone != null">phone = #{phone},</if>
|
|
<if test="fullName != null">full_name = #{fullName},</if>
|
|
updated_at = CURRENT_TIMESTAMP,
|
|
<if test="active != null">active = #{active},</if>
|
|
</set>
|
|
WHERE id = #{id}
|
|
</update>
|
|
|
|
<!-- 删除用户 -->
|
|
<delete id="deleteById">
|
|
DELETE FROM users WHERE id = #{id}
|
|
</delete>
|
|
|
|
<!-- 根据用户名查询用户 -->
|
|
<select id="findByUsername" resultMap="userResultMap">
|
|
SELECT <include refid="baseColumns"/>
|
|
FROM users
|
|
WHERE username = #{username}
|
|
</select>
|
|
|
|
<!-- 根据邮箱查询用户 -->
|
|
<select id="findByEmail" resultMap="userResultMap">
|
|
SELECT <include refid="baseColumns"/>
|
|
FROM users
|
|
WHERE email = #{email}
|
|
</select>
|
|
</mapper> |