【弄nèng - Mybatis】Mybatis一个dao对应多个Mapper.xml

本文详细介绍了在MyBatis中如何通过自定义Mapper来扩展原有DAO的功能,包括添加接口、创建mapper.xml以及修改配置文件的具体步骤。适用于需要对自动生成的Mapper进行功能增强的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
学习地址:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值