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>
相关推荐
蛋蛋的就会顺顺的1 小时前
hot100 p0——栈,贪心算法,动态规划
java·前端·javascript·算法·leetcode·力扣
余槐i1 小时前
PostgreSQL 函数索引报错 must be immutable:3种绕法与代价
数据库·后端·sql·postgresql·性能优化
需要8261 小时前
Spring Bean 生命周期:从实例化到销毁的完整钩子
java·后端·spring
小 黄 鸡2 小时前
HashMap 面试必会知识点详解(JDK 8)
java·面试·职场和发展
伯恩bourne4 小时前
Spring Boot Bean 注册的两种方式:@Component 家族 vs @Configuration + @Bean
java·spring boot·后端
silvamux_4 小时前
curl 测试 OpenAI 兼容 API 的完整流程
java·人工智能·缓存·ai·oneapi
省钱兄--zs4 小时前
深圳24小时自助健身房软硬件解决方案实战指南与开发流程详解
java·数据仓库·spring boot·小程序·架构
顽疲5 小时前
Java vue 养老系统源码详解:月账单 care_month_bill 与 Quartz 扣费 Job 实践
java·开发语言·vue.js·vue养老·springboot养老·java养老
Wang's Blog5 小时前
Java 项目实战: 交友项目 - 安装开发工具与环境准备
java·开发语言·交友
用户21296835261685 小时前
开源了一个 Java 多智能体 LLM 框架 Baton:一个依赖,把 Agent 循环、工具调用、记忆、编排全配齐
java·llm