Mybatis的xml文件继承写法

由于xml文件不是java代码,不能用关键字extend直接继承,那想继承父类的xml应该怎么操作呢

原始的mapper一些内容

下面的一些代码不是很全面,但主要部分都在,仅供参考

*mapper.java代码部分


public interface AutomationSqlConfigureMapper {

    List<AutomationSqlConfigure> findAutomationSqlConfigureList(AutomationSqlConfigureVo automationSqlConfigureVo);

    Integer addAutomationSqlConfigure(AutomationSqlConfigure automationSqlConfigure);

    Integer updateSqlConfigure(AutomationSqlConfigure automationSqlConfigure);
}

实体类Bean文件

@Data
public class AutomationSqlConfigure extends BaseSqlBean {

    private String tableName;
    private String tableNameEnglish;
    private String columnName;
    private String columnNameEnglish;
    private Integer type;

*mapper.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="com.xw.comm.mapper.AutomationSqlConfigureMapper">

    <resultMap id="BaseResultMap" type="com.xw.comm.entity.automation.sql.AutomationSqlConfigure">
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <result column="table_name" jdbcType="VARCHAR" property="tableName"/>
        <result column="table_name_english" jdbcType="VARCHAR" property="tableNameEnglish"/>
        <result column="column_name" jdbcType="VARCHAR" property="columnName"/>
        <result column="column_name_english" jdbcType="VARCHAR" property="columnNameEnglish"/>
        <result column="type" jdbcType="INTEGER" property="type"/>
        <result column="length" jdbcType="INTEGER" property="length"/>
        <result column="indexes_type" jdbcType="INTEGER" property="indexesType"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
    </resultMap>


    <insert id="addAutomationSqlConfigure" parameterType="com.xw.comm.entity.automation.sql.AutomationSqlConfigure">
        insert into automation_sql_configure (id,table_name,table_name_english,column_name,column_name_english,
        type,length,indexes_type,update_time,create_time) values (#{id},#{tableName},#{tableNameEnglish},
        #{columnName},#{columnNameEnglish},#{type},#{length},#{indexesType},#{updateTime},#{createTime})
    </insert>

    <update  id="updateSqlConfigure" >
        update automation_sql_configure         <set>
            <if test="tableName != null and tableName != ''">table_name = #{tableName},</if>
            <if test="tableNameEnglish != null and tableNameEnglish != ''">table_name_english = #{tableNameEnglish},</if>
            <if test="columnName != null and columnName != ''">column_name = #{columnName},</if>
            <if test="columnNameEnglish != null and columnNameEnglish != ''">column_name_english = #{columnNameEnglish},</if>
            <if test="type != null">type = #{type}, </if>
            <if test="length != null">length = #{length},</if>
            <if test="indexesType != null ">indexes_type = #{indexesType},</if>
        </set>
        where id = #{id};
    </update>

    <select id="findAutomationSqlConfigureList"
            parameterType="com.xw.comm.entity.automation.sql.AutomationSqlConfigureVo"
            resultMap="BaseResultMap">
        SELECT id,table_name,table_name_english,column_name,column_name_english,
        type,length,indexes_type,update_time,create_time FROM automation_sql_configure
        <where>
            1=1
            <if test="id != null and id != ''">
                and id=#{id}
            </if>
            <if test="tableName != null and tableName != ''">
                and table_name  like concat('%', #{tableName},'%')
            </if>
            <if test="tableNameEnglish != null and tableNameEnglish != ''">
                and table_name_english=#{tableNameEnglish}
            </if>
            <if test="columnNameEnglish != null and columnNameEnglish != ''">
                and column_name_english=#{columnNameEnglish}
            </if>
        </where>

        order by create_time desc
    </select>

    <delete  id="deleteAutomationSqlConfigure">
        delete FROM automation_sql_configure where id = #{id}
    </delete>

    <select id="getAutomationSqlConfigure" resultMap="BaseResultMap">
        SELECT id,table_name,table_name_english,column_name,column_name_english,
        type,length,indexes_type,update_time,create_time FROM automation_sql_configure where id=#{id}
    </select>

</mapper>

“继承”的扩展写法

继承的mapper.java

public interface AutomationSqlConfigureExtendMapper extends AutomationSqlConfigureMapper{

    List<AutomationSqlConfigure> getAutomationSqlConfigureByName(String tableName);

}

继承的mapper.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="com.xw.comm.mapper.AutomationSqlConfigureExtendMapper">

    <select id="getAutomationSqlConfigureByName" resultMap="com.xw.comm.mapper.AutomationSqlConfigureMapper.BaseResultMap">
        SELECT id,table_name,table_name_english,column_name,column_name_english,
        type,length,indexes_type,update_time,create_time FROM automation_sql_configure where table_name=#{tableName}
    </select>

</mapper>

其中要点:
1、java文件只需要继承即可
2、xml文件namespace对应的mapper.java一定是继承的文件,即此处的AutomationSqlConfigureExtendMapper类
3、select语句中resultMap一定要写com.xw.comm.mapper.AutomationSqlConfigureMapper.BaseResultMap
注意最后的.BaseResultMap。前面部分是父类的Mapper

使用

@Autowired
private AutomationSqlConfigureMapper automationSqlConfigureMapper;

改为

    @Autowired
    private AutomationSqlConfigureExtendMapper automationSqlConfigureMapper;

即可