You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
4.2 KiB

<?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.demo.mapper.RoleMapper">
<update id="updateRole">
UPDATE role
<set>
<if test="roleName != null">
role_name = #{roleName},
</if>
<if test="priority != null">
priority = #{priority},
</if>
<if test="fatherId != null">
father_id = #{fatherId},
</if>
</set>
WHERE id = #{id}
</update>
<select id="selectByRoleName" resultType="com.example.demo.domain.entity.Role">
SELECT id FROM role
<where>
role_name = #{roleName}
</where>
limit 1
</select>
<select id="selectByRoleId" resultType="com.example.demo.domain.entity.Role">
SELECT role_name FROM role
<where>
id = #{id}
</where>
</select>
<select id="selectAllRole" resultType="com.example.demo.domain.vo.RoleVo">
SELECT id,role_name FROM role
<where>
<!-- 判断 market 是否不为总部且 markets 不为空 -->
<if test="markets != null and markets.size() > 0 and '总部' not in markets">
AND market IN
<foreach collection="markets" item="market" open="(" close=")" separator=",">
#{market}
</foreach>
</if>
</where>
</select>
<select id="selectByFatherId" resultType="com.example.demo.domain.vo.RoleVo">
SELECT id,role_name,priority FROM role
<where>
father_id = #{id}
</where>
ORDER BY priority desc
</select>
<!-- 插入角色信息 -->
<insert id="addRole" parameterType="com.example.demo.domain.vo.RoleVo" useGeneratedKeys="true" keyProperty="id">
INSERT INTO role
(role_name, priority, father_id, market)
VALUES (#{roleName}, #{priority}, #{fatherId}, #{market})
</insert>
<!-- 删除 role_menu 表中对应角色的数据 -->
<delete id="deleteRoleMenu" parameterType="Integer">
DELETE FROM role_menu WHERE role_id = #{id};
</delete>
<!-- 删除 role 表中对应的数据 -->
<delete id="deleteRole" parameterType="Integer">
DELETE FROM role WHERE id = #{id};
</delete>
<!-- 查询筛选后角色记录 -->
<select id="selectBy" resultType="com.example.demo.domain.vo.RoleVo">
SELECT r.id AS id,
r.role_name AS roleName,
r.priority AS priority,
r.father_id AS fatherId,
r.market AS market,
r.create_time AS createTime,
r.update_time AS updateTime,
-- 通过自连接查询父角色名称
father.role_name AS fatherName
FROM role r
-- 自连接,通过 father_id 关联父角色
LEFT JOIN role father ON r.father_id = father.id
<where>
<if test="roleName != null and roleName != ''">
r.role_name LIKE CONCAT('%', #{roleName}, '%')
</if>
<if test="market != null and market != ''">
r.market LIKE CONCAT('%', #{market}, '%')
</if>
<!-- 判断 market 是否不为总部且 markets 不为空 -->
<if test="markets != null and markets.size() > 0 and '总部' not in markets">
AND r.market IN
<foreach collection="markets" item="market" open="(" close=")" separator=",">
#{market}
</foreach>
</if>
</where>
ORDER BY r.update_time DESC,r.priority DESC
</select>
<!--获取当前角色的上级角色-->
<select id="selectFather" resultType="com.example.demo.domain.vo.RoleVo">
select r.id as id,
r.role_name as roleName,
r.market as market,
r.father_id as fatherId,
father.market as fatherMarket,
father.role_name as fatherName
from role as r
-- 自连接,通过 father_id 关联父角色
LEFT JOIN role as father ON r.father_id = father.id
where r.id = #{id}
</select>
</mapper>