使用ssm+sql server多条件查询sql 动态拼接未成功

Mapper
//根据条件查询信息
List selectSaleInfoByData(@Param("typename")String typename,@Param("productnum")String productnum,
@Param("productname")String productname,@Param("storename")String storename);
mapper.xml

select * from saleinfo


typename=#{typename}


and productnum=#{productnum}


and productname like '%'+#{productname}+'%'


and storename=#{storename}



controller
/**
* 根据条件查询信息
* @param typename
* @param productnum
* @param productname
* @param session
* @param request
* @return
*/
@RequestMapping(value="/selectSaleInfo.html",method=RequestMethod.GET)
public String selectSaleInfoByDatas(@RequestParam(value="typename",required=false)String typename,@RequestParam(value="productnum",required=false)String productnum,
@RequestParam(value="productname",required=false)String productname,HttpSession session){

    Store store=(Store) session.getAttribute(Constants.USERINFO_SESSION);
    String storename=store.getStorename();
    System.out.println(typename);
    System.out.println(productnum);
    System.out.println(productname);
    System.out.println("=========>"+storename);
    List<SaleInfo> list1=saleInfoService.selectSaleInfoByData(typename, productnum, productname, storename);
    session.setAttribute("saleinfodatalist", list1);


    return "selectSaleInfo";
}

jsp

品类查询: 商品代码: 商品名称:

控制台

  • (39749 ms) - 2019-4-8 15:06:55DEBUG ==> Preparing: select * from saleinfo WHERE typename=? and productnum=? and productname like '%'+?+'%' and storename=?
  • (39749 ms) - 2019-4-8 15:06:55DEBUG ==> Parameters: (String), 111413170(String), (String), 003石碣店(String)
  • (39783 ms) - 2019-4-8 15:06:55DEBUG <== Total: 0
  • (39784 ms) - 2019-4-8 15:06:55DEBUG Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@221de751]
  • (39785 ms) - 2019-4-8 15:06:55DEBUG Returning JDBC Connection to DataSource 查不到结果

controller 传进的参数有两个是空的啊, SQL上如果没加判空的话应该就查询不到吧

like 是这样写的 LIKE CONCAT('%',#{name},'%') xml中没有+号
https://www.cnblogs.com/ysocean/p/7289529.html

你是sqlserver的话,应该就是like那块的问题,需要使用concat去配合做模糊查询的,具体的你可以百度下concat的使用方法

1.首先,你先试试直接在数据库使用这条SQL能不能查询到数据:

select * from saleinfo WHERE typename=? and productnum=? and productname like '%'+?+'%' and storename=?

参数就是使用能查询到结果的参数

2.要是SQL在数据库中能查到数据,那就要看看你传进去的参数是不是有效的,会不会是空了。

3.你先试试全部参数不为空看看能不能查到结果,然后再试试一个两个参数为空能不能查询到结果。



and typename=#{typename}


and productnum=#{productnum}


and productname like '%'+#{productname}+'%'


and storename=#{storename}


这样判断传参是否有值,有值才加入条件查询。
有用的话请采纳。
图片说明
提交答案发现标签显示不出来,参照图片。