Home  >  Article  >  Java  >  mybatis like query

mybatis like query

(*-*)浩
(*-*)浩forward
2019-11-01 15:48:413554browse

Mybatis automatically generates a like query. You need to add % to the parameters yourself, because mybatis will not automatically add the % sign. This is decided by mybatis in order to provide maximum flexibility, because mybatis does not know which one I want to be in. Add % to position.

mybatis like query

From now on we can draw inferences from one example. For problems like this, mybatis will not automatically add the symbols we expect for us because it does not know where it should be. Wherever it is added, this addition becomes superfluous.

    public PageInfo<StationCardPreBo> findStationCardPres(
            StationCardPreQueryBo stationCardPreQueryBo, PageBo pageBo) {
        StationCardPreQuery query = new StationCardPreQuery();
        Criteria criteria = query.createCriteria();
        if (stationCardPreQueryBo.getId() != null) {
            criteria.andIdEqualTo(stationCardPreQueryBo.getId());
        }
        if (stationCardPreQueryBo.getStationNo() != null) {
            criteria.andStationNoLike("%" + stationCardPreQueryBo.getStationNo() + "%");
        }
        if (stationCardPreQueryBo.getCardNo() != null) {
            criteria.andCardNoLike("%" + stationCardPreQueryBo.getCardNo()+ "%");
        }

        // 设置分页参数
        PageHelper.startPage(pageBo.getPageNum(), pageBo.getPageSize());
        List<StationCardPre> list = stationCardPreMapper.selectByExample(query);

        PageInfo<StationCardPre> tempPageInfo = new PageInfo<>(list);

        PageInfo<StationCardPreBo> resultPage = new PageInfo<StationCardPreBo>();
        BeanMapper.copy(tempPageInfo, resultPage);

        if (CollectionUtils.isNotEmpty(list)) {
            resultPage
                    .setList(BeanMapper.mapList(list, StationCardPreBo.class));
        }

        return resultPage;
    }

To take a step back, if you don’t know what the final sql statement that mybatis spliced ​​for us is, you can print out the final spliced ​​statement of mysql for us, or we can type at the splicing point of mysql Breakpoint, so that we can view our sql statement and analyze the final cause of the problem from here.

mybatis sql construction location:

mybatis like query

The above is the detailed content of mybatis like query. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete