mybatis中单独引用mapper文件中的sq

mybatis中单独引用mapper文件中的sql

问题描述

在分配数据范围的时候,需要将用户的数据范围sql单独引进

解决方案

在mybatis中加载xml的路径,如resources/mappings/新建任意名称的xml,这里为GlobalUserScope.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="globalUserScope">

    <sql id="scopeForDepart">
        SELECT
        DISTINCT dpt1.id
        FROM
        t_depart dpt1
        INNER JOIN t_depart dpt2 ON dpt2.`level` like concat(dpt1.`level`,'%') AND dpt2.type=3
        WHERE
        dpt1.user_id = #{userId,jdbcType=BIGINT}
        AND dpt2.is_deleted = 0
        AND dpt2.type=3
    </sql>
</mapper>

那么在引入这个sql的时候为namespace.sqlid,这里为globalUserScope.scopeForDepart

select
    *
from
    t_user tu
where
    tu.removed=0
and tu.scope_id in (<include refid="globalUserScope.scopeForDepart">)
发布了76 篇原创文章 · 获赞 66 · 访问量 51万+

猜你喜欢

转载自blog.csdn.net/u013887008/article/details/103130460