java干几年,回到解放前 2021-11-26 01:19 采纳率: 66.7%
浏览 56
已结题

mybatis将参数null识别为"",不明白是为什么

mybatis将参数null识别为"",不明白是为什么
post请求:
POST http://localhost:8080/companyList
Content-Type: application/x-www-form-urlencoded

comName="销售"&enterpriseCategory=&isDangersource=&districtId=&ownerType=

controller:

@RequestMapping("/companyList")
    @ResponseBody
    public List<HwCompanyInfo> companyList(String comName,
                                           String enterpriseCategory,
                                           String isDangersource,
                                           String districtId,
                                           String ownerType){
        return hwCompanyInfoService.selectHwCompanyInfosByParam(comName,enterpriseCategory,isDangersource,districtId,ownerType);
    }

servieImpl:

    @Override
    public List<HwCompanyInfo> selectHwCompanyInfosByParam(String comName, String enterpriseCategory, String isDangersource, String districtId, String ownerType) {
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("comName",comName);
        param.put("enterpriseCategory",enterpriseCategory);
        param.put("isDangersource",isDangersource);
        param.put("districtId",districtId);
        param.put("ownerType",ownerType);

        System.out.println(param.toString());
        return hwCompanyInfoMapper.selectHwCompanyInfosByParam(param);
    }

mapper.xml:

  <select id="selectHwCompanyInfosByParam" parameterType="java.util.Map" resultMap="BaseResultMap">
    select COMCODE, BISINESS_NUM, CITY_ID, COM_LEVEL, COM_NAME, COM_TYPE, COMPANY_SCALE,
    CORPORATION_NAME, COUNTY, DANGOODS_NAME, DISTRICT_ID, ECONOMY_TYPE, EMERGENCY_TEL,
    EMPLOY_NUM, ENTERPRISE_AREA, ENTERPRISE_CATEGORY, ENTERPRISE_SORT, FIXED_ASSETS,
    FOUNDATE, GNAME_UN_NUMBER, GPRODUCT_ALLOW, GPRODUCT_BEGIN, GPRODUCT_END, GPRODUCT_NUM,
    INDUSTRIAL_PARK, IS_ACCESS, IS_AMMONIA, IS_HEALTH, IS_MAIN, LAST_YEAR_SALES, LATITUDE,
    LEGAL_PERSON, LEGAL_SAFE_NUM, LEGAL_TEL, LONGITUDE, MAIN_MOBILE, MAIN_PERSON, MAIN_TEL,
    OPERATE_ALLOW, OPERATE_BEGIN, OPERATE_END, OPERATE_NUM, OWNER_TYPE, PRODUCT_ADDRESS,
    PRODUCT_ALLOW, PRODUCT_BEGIN, PRODUCT_END, PRODUCT_NUM, REGIST_ADDRESS, REGISTERED_CAPITAL,
    REVIEW_UNIT, SAFE_CHECK_DATE, SAFE_MOBILE, SAFE_PERSON, SAFE_TEL, SAFETY_ENGINEER_NUM,
    SAFETY_MANAGER_NUM, SECTORS_MTYPE, SECTORS_TYPE, SECURITY_AGENCIES, SECURITY_MANAGER,
    STANDARD_LEVEL, STANDARD_TIME, STREET_ID, SUPERVISE_STYPE, SUPERVISE_TYPE, SUPERVISORY_AUTHORITY,
    TOWNS, TRANSPORT, USE_ALLOW, USE_BEGIN, USE_END, USE_NUM, VERIFY_CONTENT, VERIFY_STATUS,
    VERIFY_TYPE, VERSION_INFO, WHETHER_CORPORATION, ZIP_CODE, IS_DANGERSOURCE
    from hw_company_info where 1=1
    <if test="comName != null and comName != ''">
      and COM_NAME like concat('%',${comName},'%')
    </if>
    <if test=' enterpriseCategory != ""'>
      and ENTERPRISE_CATEGORY = #{enterpriseCategory,jdbcType=VARCHAR}
    </if>
    <if test='isDangersource !="" '>
      and IS_DANGERSOURCE = #{isDangersource,jdbcType=VARCHAR}
    </if>
    <if test='districtId !="" '>
      and DISTRICT_ID = #{districtId,jdbcType=VARCHAR}
    </if>
    <if test='ownerType !="" '>
      and OWNER_TYPE = #{ownerType,jdbcType=VARCHAR}
    </if>
  </select>
运行的sql:
==>  Preparing: select COMCODE, BISINESS_NUM, CITY_ID, COM_LEVEL, COM_NAME, COM_TYPE, COMPANY_SCALE, CORPORATION_NAME, COUNTY, DANGOODS_NAME, DISTRICT_ID, ECONOMY_TYPE, EMERGENCY_TEL, EMPLOY_NUM, ENTERPRISE_AREA, ENTERPRISE_CATEGORY, ENTERPRISE_SORT, FIXED_ASSETS, FOUNDATE, GNAME_UN_NUMBER, GPRODUCT_ALLOW, GPRODUCT_BEGIN, GPRODUCT_END, GPRODUCT_NUM, INDUSTRIAL_PARK, IS_ACCESS, IS_AMMONIA, IS_HEALTH, IS_MAIN, LAST_YEAR_SALES, LATITUDE, LEGAL_PERSON, LEGAL_SAFE_NUM, LEGAL_TEL, LONGITUDE, MAIN_MOBILE, MAIN_PERSON, MAIN_TEL, OPERATE_ALLOW, OPERATE_BEGIN, OPERATE_END, OPERATE_NUM, OWNER_TYPE, PRODUCT_ADDRESS, PRODUCT_ALLOW, PRODUCT_BEGIN, PRODUCT_END, PRODUCT_NUM, REGIST_ADDRESS, REGISTERED_CAPITAL, REVIEW_UNIT, SAFE_CHECK_DATE, SAFE_MOBILE, SAFE_PERSON, SAFE_TEL, SAFETY_ENGINEER_NUM, SAFETY_MANAGER_NUM, SECTORS_MTYPE, SECTORS_TYPE, SECURITY_AGENCIES, SECURITY_MANAGER, STANDARD_LEVEL, STANDARD_TIME, STREET_ID, SUPERVISE_STYPE, SUPERVISE_TYPE, SUPERVISORY_AUTHORITY, TOWNS, TRANSPORT, USE_ALLOW, USE_BEGIN, USE_END, USE_NUM, VERIFY_CONTENT, VERIFY_STATUS, VERIFY_TYPE, VERSION_INFO, WHETHER_CORPORATION, ZIP_CODE, IS_DANGERSOURCE from hw_company_info where 1=1 and COM_NAME like concat('%',"销售",'%')
==> Parameters: 
我把后面四个参数判断里的!=null删掉了,只留下了!="",结果sql还是不拼接判断语句。而且当我传参为""时,反而会在sql拼接上条件语句
麻烦大家帮忙解答一下,为什么会出现这个情况,

爱你们的小张

  • 写回答

3条回答 默认 最新

  • 大鹏cool Java领域优质创作者 2021-11-26 08:40
    关注

    其实原因很简单,并不是 mybatis 把 null 识别为空字符串 '',而是请求中的 query 字符串指定没有值的参数后,ServletRequest#getParameter 方法获取到的就是空字符串 '',题主可以自己打个断点测试下。有帮助麻烦给个采纳哈。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 12月4日
  • 已采纳回答 11月26日
  • 修改了问题 11月26日
  • 创建了问题 11月26日

悬赏问题

  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃