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>
相关推荐
吴声子夜歌3 分钟前
SQL进阶——HAVING子句
数据库·sql
CodeStats10 分钟前
《源纹天书》第121-125章:源匠归来——全栈重构与归元圣域的2.0时代
java·开发语言·源纹天书
AI人工智能+电脑小能手11 分钟前
【大白话说Java面试题 第154题】【06_Spring篇】第14题:Spring 支持的 Bean 作用域
java·开发语言·spring·面试
wuminyu20 分钟前
markword在高并发场景下变化剖析
java·linux·c语言·jvm·c++
组合缺一34 分钟前
用 ChatModel 构建 LLM 驱动的 Java 应用
java·开发语言·ai·llm·solon·rag
zzz_236842 分钟前
【Java实习面试算法冲刺】哈希!
java·算法·面试
带刺的坐椅1 小时前
ReActAgent 使用指南:构建会思考、能行动的 AI Agent
java·ai·llm·solon·loop·react-agent
漂亮的摩托1 小时前
如何编写一个SpringBoot项目告警推送的Starter
java·spring boot·后端
盖伦暴打诺手1 小时前
类比推理知识点
java
ch.ju1 小时前
Java程序设计(第3版)第四章——类加载
java·开发语言