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.

109 lines
4.0 KiB

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.example.demo.mapper.RoleMapper">
  4. <update id="updateRole">
  5. UPDATE role
  6. <set>
  7. <if test="roleName != null">
  8. role_name = #{roleName},
  9. </if>
  10. <if test="priority != null">
  11. priority = #{priority},
  12. </if>
  13. <if test="fatherId != null">
  14. father_id = #{fatherId},
  15. </if>
  16. </set>
  17. WHERE id = #{id}
  18. </update>
  19. <select id="selectByRoleName" resultType="com.example.demo.domain.entity.Role">
  20. SELECT id FROM role
  21. <where>
  22. role_name = #{roleName}
  23. </where>
  24. </select>
  25. <select id="selectByRoleId" resultType="com.example.demo.domain.entity.Role">
  26. SELECT role_name FROM role
  27. <where>
  28. id = #{id}
  29. </where>
  30. </select>
  31. <select id="selectAllRole" resultType="com.example.demo.domain.vo.RoleVo">
  32. SELECT id,role_name FROM role
  33. <where>
  34. <!-- 判断 market 是否不为总部且 markets 不为空 -->
  35. <if test="markets != null and markets.size() > 0 and '总部' not in markets">
  36. AND market IN
  37. <foreach collection="markets" item="market" open="(" close=")" separator=",">
  38. #{market}
  39. </foreach>
  40. </if>
  41. </where>
  42. </select>
  43. <select id="selectByFatherId" resultType="com.example.demo.domain.vo.RoleVo">
  44. SELECT id,role_name,priority FROM role
  45. <where>
  46. father_id = #{id}
  47. </where>
  48. ORDER BY priority desc
  49. </select>
  50. <!-- 插入角色信息 -->
  51. <insert id="addRole" parameterType="com.example.demo.domain.vo.RoleVo" useGeneratedKeys="true" keyProperty="id">
  52. INSERT INTO role
  53. (role_name, priority, father_id, market)
  54. VALUES (#{roleName}, #{priority}, #{fatherId}, #{market})
  55. </insert>
  56. <!-- 删除 role_menu 表中对应角色的数据 -->
  57. <delete id="deleteRoleMenu" parameterType="Integer">
  58. DELETE FROM role_menu WHERE role_id = #{id};
  59. </delete>
  60. <!-- 删除 role 表中对应的数据 -->
  61. <delete id="deleteRole" parameterType="Integer">
  62. DELETE FROM role WHERE id = #{id};
  63. </delete>
  64. <!-- 查询筛选后角色记录 -->
  65. <select id="selectBy" resultType="com.example.demo.domain.vo.RoleVo">
  66. SELECT r.id AS id,
  67. r.role_name AS roleName,
  68. r.priority AS priority,
  69. r.father_id AS fatherId,
  70. -- 通过自连接查询父角色名称
  71. father.role_name AS fatherName
  72. FROM role r
  73. -- 自连接,通过 father_id 关联父角色
  74. LEFT JOIN role father ON r.father_id = father.id
  75. <where>
  76. <!-- 判断 market 是否不为总部且 markets 不为空 -->
  77. <if test="markets != null and markets.size() > 0 and '总部' not in markets">
  78. AND r.market IN
  79. <foreach collection="markets" item="market" open="(" close=")" separator=",">
  80. #{market}
  81. </foreach>
  82. </if>
  83. <if test="roleName != null and roleName != ''">
  84. r.role_name LIKE CONCAT('%', #{roleName}, '%')
  85. </if>
  86. </where>
  87. ORDER BY r.priority DESC
  88. </select>
  89. <!--获取当前角色的上级角色-->
  90. <select id="selectFather" resultType="com.example.demo.domain.vo.RoleVo">
  91. select r.id as id,
  92. r.role_name as roleName,
  93. r.market as market,
  94. r.father_id as fatherId,
  95. father.market as fatherMarket,
  96. father.role_name as fatherName
  97. from role as r
  98. -- 自连接,通过 father_id 关联父角色
  99. LEFT JOIN role as father ON r.father_id = father.id
  100. where r.id = #{id}
  101. </select>
  102. </mapper>