Mybatis代码生成器生成的Example中的Criteria的使用,你会几种?

1.查询数量

AgentOmmissionExample example = new AgentOmmissionExample();
AgentOmmissionExample.Criteria criteria = example.createCriteria();
long count = agentOmmissionMapper.countByExample(example);

2.where条件查询及多条件查询

 AgentOmmissionExample example = new AgentOmmissionExample();
  AgentOmmissionExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(startTime))
            criteria.andGmtCreateGreaterThanOrEqualTo(sf.parse(startTime));
        if (!StringUtils.isEmpty(endTime))
            criteria.andGmtCreateLessThanOrEqualTo(sf.parse(endTime));

类似于SQL语句
gent_ommission` WHERE gmt_create >= '2017-09-12 12:02:30'  AND '2017-09-12 15:07:42' >= gmt_create

3.or 条件的查询
例如SQL语句
gent_ommission` WHERE gmt_create >= '2017-09-12 12:02:30'  AND '2017-09-12 15:07:42' >= gmt_create AND (agent_id=1  OR parent_agent_id=1 OR grandfather_agent_id=1) ORDER BY gmt_create LIMIT 0,10

AgentOmmissionExample example = new AgentOmmissionExample();
AgentOmmissionExample.Criteria criteria = example.createCriteria();

    if (!StringUtils.isEmpty(startTime))
        criteria.andGmtCreateGreaterThanOrEqualTo(sf.parse(startTime));
    if (!StringUtils.isEmpty(endTime))
        criteria.andGmtCreateLessThanOrEqualTo(sf.parse(endTime));
    criteria.andTypeEqualTo(1);

    if (agentId != null) {
        criteria.andAgentIdEqualTo(agentId);

        AgentOmmissionExample.Criteria criteria1 = example.createCriteria();
        criteria1.andParentAgentIdEqualTo(agentId);
        if (!StringUtils.isEmpty(startTime))
            criteria1.andGmtCreateGreaterThanOrEqualTo(sf.parse(startTime));
        if (!StringUtils.isEmpty(endTime))
            criteria1.andGmtCreateLessThanOrEqualTo(sf.parse(endTime));

        AgentOmmissionExample.Criteria criteria2 = example.createCriteria();
        criteria2.andGrandfatherAgentIdEqualTo(agentId);
        if (!StringUtils.isEmpty(startTime))
            criteria2.andGmtCreateGreaterThanOrEqualTo(sf.parse(startTime));
        if (!StringUtils.isEmpty(endTime))
            criteria2.andGmtCreateLessThanOrEqualTo(sf.parse(endTime));
        example.or(criteria1);
        example.or(criteria2);

4.模糊查询及分页查询

  AgentExample agentExample = new AgentExample();
        AgentExample.Criteria criteria = agentExample.createCriteria();
        if (!StringUtils.isEmpty(name))
            criteria.andAgentNameLike("%" + name + "%");---模糊查询
        if (!StringUtils.isEmpty(agentLevel))
            criteria.andAgentLevelEqualTo(agentLevel);

        int startNum = (page - 1) * rows;

        agentExample.setPageSize(rows);   --分页查询中的一页数量
        agentExample.setStartRow(startNum);  --开始查询的位置

你可能感兴趣的:(mybatis)