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 天前
微服务化的基石——持续集成
java·大数据·word·powerpoint·.net
俊昭喜喜里3 天前
java中的继承和多态的区别
java
小羊没烦恼!3 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
譕痕3 天前
JSONObject与JSONArray封装数据格式区别
java·json
胡写代码3 天前
别再前后端各写一套表单校验了
java·后端
小鱼能吃糖3 天前
缺陷修复总览 · mall电商项目:5类缺陷,1个病根,4个业务域
java·电商
此时不提桶,更待何时3 天前
01-06-A-JVM排查实战详解
java·jvm
vipxieliang3 天前
ValidX 在 DDD 领域驱动设计中的实践
java·spring boot
ba_pi3 天前
mysql 查询所有表名并授权
java
ProcessOn官方账号3 天前
java函数式编程--入门基础
java·编程·函数式编程