RoleMapper.xml 6.99 KB
<?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.diligrp.manage.dao.impl.RoleDaoImpl">
    <resultMap id="RoleRM" type="com.diligrp.manage.domain.Role">
            <id property="id" column="id"/>
            <result property="roleName" column="role_name"/>
            <result property="description" column="description"/>
            <result property="created" column="created"/>
            <result property="modified" column="modified"/>
    </resultMap>

   <resultMap id="UserRoleRM" type="com.diligrp.manage.domain.UserRole">
            <id property="id" column="id"/>
            <result property="roleId" column="role_id"/>
    </resultMap>

    <resultMap id="RoleUserRM" type="com.diligrp.manage.domain.RoleUser">
        <id property="id" column="id"/>
        <result property="userId" column="user_id"/>
    </resultMap>

	<select id="findUserByRoleId" parameterType="Long" resultMap="UserRoleRM">
		<![CDATA[
		SELECT
			u.id,
		    u.role_id
		FROM security_user_role u
		WHERE u.role_id=#{id}
		]]>
	</select>

	<select id="getById" parameterType="Long" resultMap="RoleRM">
		<![CDATA[
		SELECT
		    id
		    ,role_name
		    ,description
		    ,created
		    ,modified
		FROM security_role

		WHERE id=#{pk}
		and (yn is null or yn = 1)
		]]>
	</select>
	<select id="findByUserId" parameterType="Long" resultMap="RoleRM">
		<![CDATA[
		SELECT
		    r.id
		    ,r.role_name
		    ,r.description
		    ,r.created
		    ,r.modified
		FROM security_role r
		left join  security_user_role ru on ru.role_id = r.id
		WHERE
		 1 = 1
		and ru.user_id=#{pk}
		and (yn is null or yn = 1)
		]]>
	</select>

	<select id="findByResource" parameterType="Long" resultMap="RoleRM">
		<![CDATA[
		SELECT
		    r.id
		    ,r.role_name
		    ,r.description
		    ,r.created
		    ,r.modified
		FROM security_role r
		left join security_role_permission rp on rp.role_id = r.id
		WHERE rp.resource_id=#{pk}
		and (yn is null or yn = 1)
		]]>
	</select>

    <select id="getByMenu" parameterType="Long" resultMap="RoleRM">
        <![CDATA[
		SELECT
		    sr.id
		    ,sr.role_name
		    ,sr.description
		    ,sr.created
		    ,sr.modified
		FROM security_role sr
		LEFT JOIN security_role_menu srm on sr.id = srm.role_id
		WHERE srm.menu_id=#{pk}
		and (yn is null or yn = 1)
		]]>
    </select>
	
	<select id="getAllRoles" resultMap="RoleRM">
		<![CDATA[
		SELECT
		    id
		    ,role_name
		    ,description
		    ,created
		    ,modified
		FROM security_role where yn = 1
		]]>
	</select>

	<insert id="save" parameterType="com.diligrp.manage.domain.Role">
        <selectKey keyProperty="id" resultType="long" order="AFTER">
            SELECT LAST_INSERT_ID();
        </selectKey>
		<![CDATA[
		INSERT INTO security_role(
                    role_name
                    ,description
		) VALUES(
                    #{roleName}
                    ,#{description}
		)
		]]>
	</insert>

    <insert id="saveRoleMenu" parameterType="map">
        <selectKey keyProperty="id" resultType="long" order="AFTER">
            SELECT LAST_INSERT_ID();
        </selectKey>
        INSERT INTO security_role_menu(
            role_id
            , menu_id
        ) VALUES (
            #{roleId}
            , #{menuId}
        )
    </insert>

    <delete id="delByRole" parameterType="long">
        DELETE FROM security_role_menu WHERE role_id = #{id}
    </delete>

    <update id="update" parameterType="com.diligrp.manage.domain.Role">
    <![CDATA[
            UPDATE security_role set
                        role_name=#{roleName}
                        ,description=#{description}
                        ,modified=#{modified}

            where 1=1
            and id = #{id}
            ]]>
    </update>

	<select id="countByCondition" parameterType="Query" resultType="Integer">
		<![CDATA[
		SELECT count(1) FROM security_role where 1=1
		]]>
        <if test="param != null">
            <if test="param.id != null and param.id != ''">
            and id = #{param.id}
            </if>
            <if test="param.roleName != null and param.roleName != ''">
            and role_name = #{param.roleName}
            </if>
            <if test="param.description != null and param.description != ''">
            and description = #{param.description}
            </if>
            <if test="param.created != null and param.created != ''">
            and DATE_FORMAT(created, '%Y-%m-%d') = #{param.created}
            </if>
            <if test="param.modified != null and param.modified != ''">
            and DATE_FORMAT(modified, '%Y-%m-%d') = #{param.modified}
            </if>
            and yn = 1
        </if>
	</select>

	<select id="listByCondition" parameterType="Query" resultMap="RoleRM">
		<![CDATA[
		SELECT
		    id
		    ,role_name
		    ,description
		    ,created
		    ,modified
		FROM security_role where 1=1
		]]>
        <if test="param != null">
                <if test="param.id != null and param.id != ''">
                    and id = #{param.id}
                </if>
                <if test="param.roleName != null and param.roleName != ''">
                    and role_name = #{param.roleName}
                </if>
                <if test="param.description != null and param.description != ''">
                    and description = #{param.description}
                </if>
                <if test="param.created != null and param.created != ''">
                    and DATE_FORMAT(created, '%Y-%m-%d') = #{param.created}
                </if>
                <if test="param.modified != null and param.modified != ''">
                    and DATE_FORMAT(modified, '%Y-%m-%d') = #{param.modified}
                </if>
                	and yn = 1
        </if>
        order by id desc
		<![CDATA[
		limit #{startRow},#{pageSize}
		]]>
	</select>


    <update id="deleteById" parameterType="Long">
    <![CDATA[
        update security_role set
        yn = 2
        where 1 = 1
        and id = #{id}
    ]]>
    </update>
    
    <insert id="roleResourceBatchInsert" parameterType="map">
    	<![CDATA[
        insert into security_role_permission(role_id, resource_id) values
        ]]>
        	<foreach collection="resourceIds" item="resourceId" separator=",">
        		(#{roleId},#{resourceId})
        	</foreach>
    </insert>
    
    <delete id="roleResourceBatchDelete" parameterType="map">
    	<![CDATA[
        delete from 
        	security_role_permission
        where 
        	role_id = #{roleId} and
        ]]>
        	resource_id in (
        	<foreach collection="resourceIds" item="resourceId" separator="," >
        		#{resourceId}
        	</foreach>
        	)
    </delete>

    <select id="findUsersByRoleId" parameterType="Long" resultMap="RoleUserRM">
        <![CDATA[
		SELECT
			u.id,
		    u.user_id
		FROM security_user_role u
		WHERE u.role_id=#{roleId}
		]]>
    </select>

    <delete id="delUser" parameterType="map">
        DELETE FROM security_user_role where user_id = #{userId} and role_id = #{roleId}
    </delete>

</mapper>