q2617426
无感五
2019-03-26 18:44
采纳率: 73.3%
浏览 1.1k
已采纳

这段代码我是直接写在Controller上面得,但是我想改为写在Service上请问应该怎么改呢

 //查询
    @ApiOperation(value="船东发布展示信息列表", notes="船东发布展示信息列表的接口")
    @RequestMapping(value = "/findContent" , method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name="startDate", value="开始时间", required=false),
            @ApiImplicitParam(name="endDate", value="结束时间", required=false),
            @ApiImplicitParam(name="startLoading", value="最小吨数", required=false),
            @ApiImplicitParam(name="endLoading", value="最大吨数", required=false),
            @ApiImplicitParam(name="typeShip", value="船舶类型", required=false),
            @ApiImplicitParam(name="emptyPort", value="港口", required=false)
    })
    public SysResult find(Date startDate,Date endDate , String startLoading , String endLoading,
                             Long typeShip , Long emptyPort){
        try{
            EntityWrapper<ShipRelease> wrapper = new EntityWrapper<>();
            //只能状态为发布中的数据展示出来
            wrapper.where( "state = {0}  ", 4 );
            //筛选时间日期
            if(startDate != null && endDate != null) {
                wrapper.where( "empty_date >= {0}  ", startDate );
                wrapper.and( " empty_date <= {0}", endDate );
            }
            //筛选两数值之剑
            if(startLoading != null && endLoading != null) {
            wrapper.where( "tonnage_loading >= {0}  ", startLoading );
            wrapper.and( " tonnage_loading <= {0}", endLoading );
            }
            //筛选类型1
            if(typeShip != null) wrapper.where("type_ship={0}", typeShip);
            //筛选类型2
            if(emptyPort != null) wrapper.where("empty_port={0}", emptyPort);
            wrapper.setSqlSelect("id, title,empty_date,aircraft_sky,tonnage_loading,type_ship");
            List<ShipRelease> shipReleaseList =  webShipownerReleaseShowMapper.selectList( wrapper );
            return SysResult.ok(shipReleaseList);
        }catch (Exception e){
            e.printStackTrace();
            return SysResult.build(201,"查询失败");
        }
    }

使用得是spring-boot+mybatisPlus

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • q2617426
    无感五 2019-04-29 05:21
    已采纳

    方法重写之后,之后解决了这个问题

    点赞 评论
  • sinat_30356443
    云锦の 2019-03-26 19:26

    将find方法的代码整体搬迁到Service中,然后在Controller中,调用这个Service的find就行了

    点赞 评论
  • devmiao
    devmiao 2019-03-26 23:35

    public SysResult find(Date startDate,Date endDate , String startLoading , String endLoading,
    Long typeShip , Long emptyPort){
    try{
    EntityWrapper wrapper = new EntityWrapper<>();
    //只能状态为发布中的数据展示出来
    wrapper.where( "state = {0} ", 4 );
    //筛选时间日期
    if(startDate != null && endDate != null) {
    wrapper.where( "empty_date >= {0} ", startDate );
    wrapper.and( " empty_date <= {0}", endDate );
    }
    //筛选两数值之剑
    if(startLoading != null && endLoading != null) {
    wrapper.where( "tonnage_loading >= {0} ", startLoading );
    wrapper.and( " tonnage_loading <= {0}", endLoading );
    }
    //筛选类型1
    if(typeShip != null) wrapper.where("type_ship={0}", typeShip);
    //筛选类型2
    if(emptyPort != null) wrapper.where("empty_port={0}", emptyPort);
    wrapper.setSqlSelect("id, title,empty_date,aircraft_sky,tonnage_loading,type_ship");
    List shipReleaseList = webShipownerReleaseShowMapper.selectList( wrapper );
    return SysResult.ok(shipReleaseList);
    }catch (Exception e){
    e.printStackTrace();
    return SysResult.build(201,"查询失败");
    }
    把这个代码放在service里

    把wrapper对应的包也放进去

    点赞 评论
  • wojiushiwo945you
    毕小宝 2019-03-28 09:30

    如果挪动到Service中定义,那么find方法的每个参数都应该进行判空处理。
    其次,这个webShipownerReleaseShowMapper变量是在哪里定义的,有没有可能是空指针?
    最后,空指针应该是很容易定位的,看到异常的那行的代码是对那个对象进行的.操作。

    点赞 评论

相关推荐