mapper.xml sql动态表查询配置

代码示例:

java 复制代码
List<Map<String, Object>> photovoltaicQueryBySectionId(@Param("tableNameByProductId") String tableNameByProductId,@Param("sectionId") String sectionId);
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.example.mapper.IntelligentAnalysisMapper">

    <!-- 动态表名 + 动态参数查询,根据productId区分查询条件 -->
    <select id="startIntelligentAnalysis" resultType="java.util.Map">
        SELECT * FROM ${tableNameByProductName}
        <where>
            <!-- 通用条件:如果有公共参数可以在这里添加 -->
            <if test="queryParamMap.commonParam != null and queryParamMap.commonParam != ''">
                AND common_column = #{queryParamMap.commonParam}
            </if>
            
            <!-- 根据productId不同,处理不同的查询参数 -->
            <choose>
                <!-- productId为32的情况 -->
                <when test="productId == '32'">
                    <if test="queryParamMap.projectName != null and queryParamMap.projectName != ''">
                        AND project_name = #{queryParamMap.projectName}
                    </if>
                    <if test="queryParamMap.inverterType != null and queryParamMap.inverterType != ''">
                        AND inverter_type = #{queryParamMap.inverterType}
                    </if>
                    <if test="queryParamMap.capacity != null">
                        AND capacity = #{queryParamMap.capacity}
                    </if>
                </when>
                
                <!-- productId为33的情况 -->
                <when test="productId == '33'">
                    <if test="queryParamMap.equipmentNo != null and queryParamMap.equipmentNo != ''">
                        AND equipment_no = #{queryParamMap.equipmentNo}
                    </if>
                    <if test="queryParamMap.productionDate != null">
                        AND production_date = #{queryParamMap.productionDate}
                    </if>
                </when>
                
                <!-- 其他productId的默认情况 -->
                <otherwise>
                    <if test="queryParamMap.deviceId != null and queryParamMap.deviceId != ''">
                        AND device_id = #{queryParamMap.deviceId}
                    </if>
                    <if test="queryParamMap.status != null">
                        AND status = #{queryParamMap.status}
                    </if>
                </otherwise>
            </choose>
            
            <!-- 时间范围条件(通用) -->
            <if test="queryParamMap.startTime != null">
                AND create_time &gt;= #{queryParamMap.startTime}
            </if>
            <if test="queryParamMap.endTime != null">
                AND create_time &lt;= #{queryParamMap.endTime}
            </if>
        </where>
        <!-- 可以根据需要添加排序 -->
        <if test="queryParamMap.orderBy != null and queryParamMap.orderBy != ''">
            ORDER BY ${queryParamMap.orderBy}
        </if>
    </select>

</mapper>
相关推荐
FQNmxDG4S4 小时前
Java多线程编程:Thread与Runnable的并发控制
java·开发语言
虹科网络安全4 小时前
艾体宝干货|数据复制详解:类型、原理与适用场景
java·开发语言·数据库
axng pmje5 小时前
Java语法进阶
java·开发语言·jvm
rKWP8gKv75 小时前
Java微服务性能监控:Prometheus与Grafana集成方案
java·微服务·prometheus
老前端的功夫5 小时前
【Java从入门到入土】28:Stream API:告别for循环的新时代
java·开发语言·python
qq_435287925 小时前
第9章 夸父逐日与后羿射日:死循环与进程终止?十个太阳同时值班的并行冲突
java·开发语言·git·死循环·进程终止·并行冲突·夸父逐日
小江的记录本5 小时前
【Kafka核心】架构模型:Producer、Broker、Consumer、Consumer Group、Topic、Partition、Replica
java·数据库·分布式·后端·搜索引擎·架构·kafka
yaoxin5211236 小时前
397. Java 文件操作基础 - 创建常规文件与临时文件
java·开发语言·python
极客先躯8 小时前
高级java每日一道面试题-2025年11月24日-容器与虚拟化题[Dockerj]-runc 的作用是什么?
java·oci 的命令行工具·最小可用·无守护进程·完全标准·创建容器的核心流程·runc 核心职责思维导图
用户60648767188968 小时前
AI 抢不走的技能:用 Claude API 构建自动化工作流实战
java