请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

按id搜索返回为空

老师,我在自测的时候,按照id来搜索的话,返回为空,如果不填写productName和productId那么返回结果也为空,不应该是返回所有嘛,我对照了老师的代码就是这样写的,没找到原因呢??

xml文件:

<select id="selectProductByNameAndId" resultMap="BaseResultMap" parameterType="map">
 SELECT
 <include refid="Base_Column_List"/>
 from mmall_product
 <where>
   <if test="productName !=null">
     and name like #{productName}
   </if>
   <if test="productId !=null">
     and id=#{productId}
   </if>
 </where>
</select>

controller文件:

@RequestMapping(value = "search.do",method = RequestMethod.POST)
@ResponseBody
public ServerResponse search(HttpSession session,String productName,Integer productId,@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
   User user = (User) session.getAttribute(Const.CURRENT_USER);
   if (user ==null){
       return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
   }
   if (iUserService.checkAdminRole(user).isSuccess()){
       return iProductService.getSearchResult(productName,productId,pageNum,pageSize);
   }

返回结果:

{

"status": 0,

"data":{

"pageNum": 1,

"pageSize": 20,

"size": 0,

"orderBy": null,

"startRow": 0,

"endRow": 0,

"total": 0,

"pages": 0,

"list":[

],

"firstPage": 0,

"prePage": 0,

"nextPage": 0,

"lastPage": 0,

"isFirstPage": true,

"isLastPage": false,

"hasPreviousPage": false,

"hasNextPage": false,

"navigatePages": 8,

"navigatepageNums":[]

}

}


正在回答 回答被采纳积分+3

3回答

提问者 wendy_z 2018-01-22 16:43:00

不输入productName和productId,或者只输入productId的话,size怎么都是0呢,我看了我的sql语句跟老师的是一样的呀,但是根据debug结果来看,感觉有点问题,不输入productId的话,默认的productId值就是null,然后sql语句写的是如果product !=null才执行id=#{productId};但是为什么我输入productId,还是返回size为0呢?

https://img1.sycdn.imooc.com//szimg/5a659f530001777318390916.jpg

https://img1.sycdn.imooc.com//szimg/5a659f540001c61613930918.jpg

如果输入productId,debug结果为:

https://img1.sycdn.imooc.com//szimg/5a659f960001148f10780829.jpg



0 回复 有任何疑惑可以回复我~
Geely 2018-01-20 15:28:27

你好,同学这个主要看service的实现,可以打一个断点看一下。这样还能学习一下debug,建议实操一下。以后工作这种场景会很多。



课程项目思维导图及线上环境、测试环境、部署linux和windows等解答
http://www.imooc.com/article/20193 

部署环境的话 ,建议看下文章最后一小段哟~首先跟着课程阿里云部署那个章节过一遍,可以先不跟着操作,做到心里有数,环境部署都做了什么。

尽快进入咱们QQ群哟~~进群方法下面思维导图那个帖子有。非常详细

课程项目思维导图及线上环境、测试环境、部署linux和windows等解答
http://www.imooc.com/article/20193 

【重点】问答区常见问题整理
http://www.imooc.com/article/18998

课程项目QQ群分享手记
http://www.imooc.com/article/19094


找工作的季节之简历及找工作的分享
http://www.imooc.com/article/19998

项目环境、vsftpd、linux、mysql等各种配置、软件下载
http://learning.happymmall.com


mybatis-plugin插件安装
http://coding.imooc.com/learn/questiondetail/36007.html

0 回复 有任何疑惑可以回复我~
星辰Iron 2018-01-15 22:03:58

你好同学,在idea里面进入DEBUG模式打几个断点调试一下看看前端的参数是否正确传递到后台了,把idea控制台里面的日志信息也贴上看看。

0 回复 有任何疑惑可以回复我~
  • 提问者 wendy_z #1
    回复这里不能贴图吗??
    回复 有任何疑惑可以回复我~ 2018-01-22 16:21:12
  • 提问者 wendy_z #2
    JDBC Connection [jdbc:mysql://192.168.65.2:3306/mmall_learning?characterEncoding=utf-8, UserName=mmall@192.168.65.1, MySQL Connector Java] will not be managed by Spring
    [16:46:45.918][DEBUG][c.m.d.P.selectProductByNameAndId_COUNT][http-bio-8088-exec-2] ==>  Preparing: SELECT count(*) FROM mmall_product WHERE name LIKE ? AND id = ? 
    [16:46:45.918][DEBUG][c.m.d.P.selectProductByNameAndId_COUNT][http-bio-8088-exec-2] ==> Parameters: (String), 30(Integer)
    [16:46:45.920][DEBUG][c.m.d.P.selectProductByNameAndId_COUNT][http-bio-8088-exec-2] <==      Total: 1
    回复 有任何疑惑可以回复我~ 2018-01-22 16:48:21
  • Geely 回复 提问者 wendy_z #3
    你好同学,我仔细看了一下,输入那个productid之后 有没有从mapper里获取到具体的product对象呢。
    还有这段sql返回的是total=1 是有1个对象的呢
    回复 有任何疑惑可以回复我~ 2018-01-26 00:21:13
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号