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>
相关推荐
一定要AK8 小时前
Spring 入门核心笔记
java·笔记·spring
A__tao8 小时前
Elasticsearch Mapping 一键生成 Java 实体类(支持嵌套 + 自动过滤注释)
java·python·elasticsearch
KevinCyao8 小时前
java视频短信接口怎么调用?SpringBoot集成视频短信及回调处理Demo
java·spring boot·音视频
一江寒逸8 小时前
零基础从入门到精通MySQL(中篇):进阶篇——吃透多表查询、事务核心与高级特性,搞定复杂业务SQL
数据库·sql·mysql
迷藏4948 小时前
**发散创新:基于Rust实现的开源合规权限管理框架设计与实践**在现代软件架构中,**权限控制(RBAC)** 已成为保障
java·开发语言·python·rust·开源
wuxinyan1239 小时前
Java面试题47:一文深入了解Nginx
java·nginx·面试题
新知图书9 小时前
搭建Spring Boot开发环境
java·spring boot·后端
冰河团队9 小时前
一个拉胯的分库分表方案有多绝望?整个部门都在救火!
java·高并发·分布式数据库·分库分表·高性能
洛_尘9 小时前
Java EE进阶:Linux的基本使用
java·java-ee
宸津-代码粉碎机9 小时前
Spring Boot 4.0虚拟线程实战调优技巧,最大化发挥并发优势
java·人工智能·spring boot·后端·python