MySQL语句可以查询到数据,为什么用MyBatis实现就查不到数据了

小刘
  • 526

查询语句没错

我MySQL查询语句没错,可以查询到数据

SELECT rtp.license_number, rtp.begin_time, rtp.entry_time
FROM refuse_treatment_plant rtp
where rtp.license_number="京OFV501FV606" 
and DATE_FORMAT(entry_time,'%Y-%m-%d') BETWEEN "2021-05-15" and "2021-05-18"

在代码里实现

<select
            id="searchRefuseTreatmentPlantV1"
            resultType="com.bulldozer.entity.RefuseTreatmentPlant"
    >
        SELECT <include refid="Base_Column_List" />
        FROM refuse_treatment_plant
        <where>
            <!--添加if条件如果参数为空则不拼接sql-->
            <if test=" flagDelete!= null and flagDelete!= '' and flagDelete == 0">
                AND flag_delete = #{flagDelete}
            </if>
            <if test=" countType!= null and countType!= '' and countType == 'greater'">
                AND leave_count>entry_count
            </if>
            <if test=" countType!= null and countType!= '' and countType == 'less'">
                AND leave_count<![CDATA[ < ]]>entry_count
            </if>
            <if test=" licenseNumber != null and licenseNumber != '' ">
                AND license_number like concat('%',#{licenseNumber},'%')
            </if>
            <if test=" licenseNumber != null and licenseNumberType != null and
            licenseNumber != '' and licenseNumberType == 'equal'">
                AND license_number = #{licenseNumber}
            </if>
            <if test=" dateType != null and dateType== 'curDay'">
                AND to_days(warning_time) = to_days(NOW())
            </if>
            <if test=" timeStart != null and timeStart != '' and timeEnd != null and timeEnd != ''">
                AND DATE_FORMAT(entry_time, "%Y-%m-%d") BETWEEN #{timeStart} and #{timeEnd}
            </if>
            <if test=" timeStart != null and timeStart != '' ">
                <foreach collection="timeList" index="index" item="timeItem" open="AND (" close=")" separator="or" >
                    date(entry_time) = #{timeItem}
                </foreach>
            </if>

            <if test="startDateStr!=endDateStr">
                <if test="query.startDateStr != null and query.startDateStr != '' ">
                    and log_operate_time &gt;  DATE_FORMAT( #{query.startDateStr}, '%Y-%m-%d 00:00:00' )
                </if>
                <if test="query.endDateStr != null and query.endDateStr != '' ">
                    and log_operate_time &lt; DATE_FORMAT( #{query.endDateStr}, '%Y-%m-%d 23:59:59' )
                </if>
            </if>

        </where>

        <choose>
            <when test="sort=='ASC'">
                ORDER BY entryTime ASC limit #{start}, #{dataLength}
            </when>
            <otherwise>
                ORDER BY entryTime DESC limit #{start}, #{dataLength}
            </otherwise>
        </choose>
    </select>
@ApiOperation("搜索进消纳厂记录")
    @RequestMapping(value="/v1/search/refuse-treatment-plant", method={ RequestMethod.POST })
    public ResponseResult searchRefuseTreatmentPlantV1(
            HttpServletRequest request,
            @RequestBody Map<String, Object> map) {

//        System.out.println(map);
//        final List timeList = new ArrayList();

        if(map.get("timeStart") != null) {
            System.out.println(map.get("timeStart").toString());
            if(map.get("timeStart").getClass().isArray()) {
//                System.out.println("数组");
            } else {
//                System.out.println("字符串");
                String timeStartStr = map.get("timeStart").toString();
                map.put("timeStart", timeStartStr);
                timeStartStr = timeStartStr.substring(1, timeStartStr.length()-1);
//                System.out.println("timeStartStr == " + timeStartStr);
//                ArrayList<String> timeList = new ArrayList<>(Arrays.asList(
//                        timeStartStr.split(",")
//                ));
                String timeStartArray[] = timeStartStr.split(",");
                List<String> timeList = Arrays.asList(timeStartArray);
                map.put("timeList", timeList);
            }
        }

        System.out.println(map);
        List<RefuseTreatmentPlant> list = refuseTreatmentPlantService.searchRefuseTreatmentPlantV1(map);

        String message = "SUCCESS";
        String detail = "SUCCESS";
        JSONObject jsonResultObject = new JSONObject();
        jsonResultObject.put("list", list);
        return ResponseResult.ok(message, jsonResultObject, detail);
    }
    

自己打印了下语句

<if test=" timeStart != null and timeStart != '' and timeEnd != null and timeEnd != ''">
  AND DATE_FORMAT(entry_time, "%Y-%m-%d") BETWEEN #{timeStart} and #{timeEnd}
           </if>
           <if test=" timeStart != null and timeStart != '' ">
               <foreach collection="timeList" index="index" item="timeItem" open="AND (" close=")" separator="or" >
                   date(entry_time) = #{timeItem}
               </foreach>
           </if>

是这个的问题

回复
阅读 1.1k
3 个回答

sql打出来调一下就知道了

控制台打印一下sql看看
还有一种可能,看看连的数据库是不是一样

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏