Mybatis 一个dao 对应多个Mapper.xml

Mybatis 一个dao 对应多个Mapper.xml

由于项目中的mybatis的mapper是用mybatis generator自动生成的,但是生成的mapper满足不了我的业务,需要自己扩展,所以就研究了下、
  1. 添加接口
  2. 创建mapper.xml
  3. 修改配置

    1.添加接口

在原dao中加个接口

/** ---------------自定义Mapper--------------- **/  
List select(ClusterInstanceBO clusterInstanceBO);

2. 创建mapper.xml

PcacheClusterMapperExtend.xml



<mapper namespace="com.oppo.paas.pcache.manager.mapper.PcacheTemplateMapper">

  <select id="select" parameterType="com.oppo.paas.pcache.manager.entity.PcacheTemplate" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from t_pcache_template 

    <where>
        <if test="templateId != null and templateId != ''">
            and template_id = #{templateId}
        if>
        <if test="templateName != null and templateName != ''">
            and template_name = #{templateName}
        if>
        <if test="templateType != null and templateType != ''">
            and template_type = #{templateType}
        if>
        <if test="createUser != null and createUser != ''">
            and create_user = #{createUser}
        if>
        <if test="createTime != null ">
            and create_time = #{createTime,jdbcType=TIMESTAMP}
        if>
    where>
    order by create_time desc
  select>



mapper>

3. 修改配置

项目目录:
这里写图片描述

添加mapper扫描路径

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        
        <property name="mapperLocations" >
            <array>
                <value>classpath:mybatis/mappers/*Mapper.xmlvalue>
                
                <value>classpath:mybatis/mappers/extend/*MapperExtend.xmlvalue>
            array>

        property>
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml">property>
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    
                    <property name="properties">
                        <value>
                            helperDialect=mysql
                            reasonable=true
                            supportMethodsArguments=true
                            params=count=countSql
                            autoRuntimeDialect=true
                        value>
                    property>
                bean>
            array>
        property>
    bean>

mybatis generator 已经过时了哦,太麻烦,耦合性高,建议使用通用Mapper,完美继承spring,springboot
学习地址:https://gitee.com/free/Mapper/wikis/Home

你可能感兴趣的:(mybatis)