Mybatis 一个dao 对应多个Mapper.xml

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/yy756127197/article/details/80136717

Mybatis 一个dao 对应多个Mapper.xml

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

    1.添加接口

在原dao中加个接口

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

2. 创建mapper.xml

PcacheClusterMapperExtend.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.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"/>
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" >
            <array>
                <value>classpath:mybatis/mappers/*Mapper.xml</value>
                <!-- 扩展mapper.xml -->
                <value>classpath:mybatis/mappers/extend/*MapperExtend.xml</value>
            </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

猜你喜欢

转载自blog.csdn.net/yy756127197/article/details/80136717