背景:多个xml需要套起来复用,写在一个xml文件或者单独写个base文件,用于复用。
步骤:
- 新建base文件,注意命名空间
- 引用到需要的xml中
实现:
- base文件,注意命名空间,要被扫描到
<?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.XXX.infra.mapper.ExceptionInfoBaseSqlMapper">
<sql id="smartMeterCol">
lsm.process_status processStatus,
lsm.device_number deviceNumber
</sql>
<sql id="smartMeterWhere">
<where>
<if test=" area!=null ">
and lsm.area = #{area}
</if>
</where>
</sql>
</mapper>
- 引用,include中的引用必须是全命名空间
<?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.lzsh.infra.mapper.SummaryWarnMapper">
<select id="list">
select * from (
select
<include refid="com.XXX.infra.mapper.ExceptionInfoBaseSqlMapper.smartMeterCol"/>
from table
<include refid="com.XXX.infra.mapper.ExceptionInfoBaseSqlMapper.smartMeterWhere"/>
) a order by a.alertTime desc
</select>
</mapper>