Home > Java > javaTutorial > mybatis like query

mybatis like query

(*-*)浩
Release: 2019-11-01 15:48:41
forward
3607 people have browsed it

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;
    }
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template