公共片段
<!-- CommonMapper.xml -->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="common.commonSqlFragments">
<sql id="getUserCustomerNo">
select distinct sd.customer_no from sys_dept sd
left join enterprise_detail ed on ed.customer_no = sd.customer_no
left join sys_role_dept srd on srd.dept_id = sd.dept_id
left join sys_role sr on sr.role_id = srd.role_id
left join sys_user_role sur on sur.role_id = sr.role_id
where sd.customer_no is not null and sd.customer_no != ''
and ed.customer is not null and ed.customer != ''
and sur.user_id
</sql>
</mapper>
其他mapper.xml引用CommonMapper.xml片段
使用namespace+id
<if test="userId != null and userId != ''">
and a.Customer_No in (
<include refid="common.commonSqlFragments.getUserCustomerNo"/> = #{userId}
)
</if>
application.yml中部分配置,确保能加载CommonMapper.xml
mybatis:
# 配置mapper的扫描,找到所有的mapper.xml映射文件
mapperLocations: classpath*:mybatis/**/*Mapper.xml
# 加载全局的配置文件
configLocation: classpath:mybatis/mybatis-config.xml