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.

116 lines
4.2 KiB

4 weeks ago
4 weeks ago
  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. limit 1
  25. </select>
  26. <select id="selectByRoleId" resultType="com.example.demo.domain.entity.Role">
  27. SELECT role_name FROM role
  28. <where>
  29. id = #{id}
  30. </where>
  31. </select>
  32. <select id="selectAllRole" resultType="com.example.demo.domain.vo.RoleVo">
  33. SELECT id,role_name FROM role
  34. <where>
  35. <!-- 判断 market 是否不为总部且 markets 不为空 -->
  36. <if test="markets != null and markets.size() > 0 and '总部' not in markets">
  37. AND market IN
  38. <foreach collection="markets" item="market" open="(" close=")" separator=",">
  39. #{market}
  40. </foreach>
  41. </if>
  42. </where>
  43. </select>
  44. <select id="selectByFatherId" resultType="com.example.demo.domain.vo.RoleVo">
  45. SELECT id,role_name,priority FROM role
  46. <where>
  47. father_id = #{id}
  48. </where>
  49. ORDER BY priority desc
  50. </select>
  51. <!-- 插入角色信息 -->
  52. <insert id="addRole" parameterType="com.example.demo.domain.vo.RoleVo" useGeneratedKeys="true" keyProperty="id">
  53. INSERT INTO role
  54. (role_name, priority, father_id, market)
  55. VALUES (#{roleName}, #{priority}, #{fatherId}, #{market})
  56. </insert>
  57. <!-- 删除 role_menu 表中对应角色的数据 -->
  58. <delete id="deleteRoleMenu" parameterType="Integer">
  59. DELETE FROM role_menu WHERE role_id = #{id};
  60. </delete>
  61. <!-- 删除 role 表中对应的数据 -->
  62. <delete id="deleteRole" parameterType="Integer">
  63. DELETE FROM role WHERE id = #{id};
  64. </delete>
  65. <!-- 查询筛选后角色记录 -->
  66. <select id="selectBy" resultType="com.example.demo.domain.vo.RoleVo">
  67. SELECT r.id AS id,
  68. r.role_name AS roleName,
  69. r.priority AS priority,
  70. r.father_id AS fatherId,
  71. r.market AS market,
  72. r.create_time AS createTime,
  73. r.update_time AS updateTime,
  74. -- 通过自连接查询父角色名称
  75. father.role_name AS fatherName
  76. FROM role r
  77. -- 自连接,通过 father_id 关联父角色
  78. LEFT JOIN role father ON r.father_id = father.id
  79. <where>
  80. <if test="roleName != null and roleName != ''">
  81. r.role_name LIKE CONCAT('%', #{roleName}, '%')
  82. </if>
  83. <if test="market != null and market != ''">
  84. r.market LIKE CONCAT('%', #{market}, '%')
  85. </if>
  86. <!-- 判断 market 是否不为总部且 markets 不为空 -->
  87. <if test="markets != null and markets.size() > 0 and '总部' not in markets">
  88. AND r.market IN
  89. <foreach collection="markets" item="market" open="(" close=")" separator=",">
  90. #{market}
  91. </foreach>
  92. </if>
  93. </where>
  94. ORDER BY r.update_time DESC,r.priority DESC
  95. </select>
  96. <!--获取当前角色的上级角色-->
  97. <select id="selectFather" resultType="com.example.demo.domain.vo.RoleVo">
  98. select r.id as id,
  99. r.role_name as roleName,
  100. r.market as market,
  101. r.father_id as fatherId,
  102. father.market as fatherMarket,
  103. father.role_name as fatherName
  104. from role as r
  105. -- 自连接,通过 father_id 关联父角色
  106. LEFT JOIN role as father ON r.father_id = father.id
  107. where r.id = #{id}
  108. </select>
  109. </mapper>