• tkMybatis的Example使用


    Criteria,包含一个Cretiron的集合,每一个Criteria对象内包含的Cretiron之间是由AND连接的,是逻辑与的关系。

    List<Example.Criterion> criteria;

    oredCriteria,Example内有一个成员叫oredCriteria,是Criteria的集合,就想其名字所预示的一样,这个集合中的Criteria是由OR连接的,是逻辑或关系。oredCriteria就是ORed Criteria。

    List<Example.Criteria> oredCriteria;

    or()方法,会产生一个新的Criteria对象,添加到oredCriteria中,并返回这个Criteria对象,从而可以链式表达,为其添加Criterion。

    public Example.Criteria or() {
            Example.Criteria criteria = this.createCriteriaInternal();
            criteria.setAndOr("or");
            this.oredCriteria.add(criteria);
            return criteria;
        }

    1、selectOneByExample的使用

    Example userExample = new Example(User.class);
    userExample.createCriteria().andEqualTo("userName",traceabilitySlice.getCreateBy()).andEqualTo("delFlag",0);
    User sendUser = userMapper.selectOneByExample(userExample);

     2、selectByExample的使用

    Example example = new Example(User.class);
    example.createCriteria().andEqualTo("userName", user.getUserName());
    List<User> list = userMapper.selectByExample(example);

     3、排序

    Example添加查询条件时,除了使用andEqualTo外,还可设置字段降序和日期范围查询:

    Example example = new Example(OfficialAccout.class);
    //设置排序,STAT_DATE字段为降序
    example.setOrderByClause("STAT_DATE ASC");
    example.createCriteria().andBetween("statDate", last, now);
    List<OfficialAccout> list = officialAccoutMapper.selectByExample(example);

     当排序单个字段时的使用方法

    example.setOrderByClause("user_type ASC"); 

    当排序多个字段时的使用方法,中间用逗号隔开

    example.setOrderByClause("user_type ASC, level DESC");

    或者

    example.setOrderByClause("user_type , level DESC");

    设置字段升序或降序的另一种方法:

    Example example = new Example(IntelDrugStore.class);
    example.orderBy("createTime").desc().orderBy("updateTime").desc();

    4、andBetween的使用

    Example example = new Example(PlantGrowIrrigation.class);
    Example.Criteria criteria = example.createCriteria();
    criteria.andEqualTo("plantGrowId", plantGrowId).andBetween("irrigationDate", DateUtil.parse(startDate), DateUtil.parse(endDate));
    List<PlantGrowIrrigation> list = plantGrowIrrigationMapper.selectByExample(example);

    5、andIn的使用

    根据一个集合去数据库查询多条记录。

    List<String> menuCodeList = new ArrayList<String>();
            menuCodeList.add("index@index");
            menuCodeList.add("backstage@table");
            menuCodeList.add("backstage@role@index");
            menuCodeList.add("backstage@department@list");
            menuCodeList.add("backstage@user@index");
            menuCodeList.add("admin@404");
    
            Example example = new Example(Menu.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andIn("menuCode", menuCodeList);
            List<Menu> mList =  mMapper.selectByExample(example);

    6、and或or的使用

    and和or的使用主要是如下三种场景:

    1)、where (条件1 and 条件2) or (条件3 and 条件4)

    // 条件1 and 条件2
    example.createCriteria()
            .andEqualTo("isDeleted",IsDeleted.NOT_DELETED)
            .andEqualTo("name", projectCatalogEntity.getName());
    // or (条件3 and 条件4)
    example.or(example.createCriteria()
            .andEqualTo("isDeleted",IsDeleted.NOT_DELETED)
            .andEqualTo("code", projectCatalogEntity.getCode()));

    即:WHERE ( is_deleted = ? and name = ? ) or ( is_deleted = ? and code = ? )

    2)、where (条件1 and 条件2) and (条件3 or 条件4)

    // 条件1 and 条件2
    example.createCriteria()
            .andEqualTo("isDeleted",IsDeleted.NOT_DELETED))
            .andEqualTo("parentId", projectCatalogEntity.getParentId());
    // and (条件3 or 条件4)
    example.and(example.createCriteria()
            .andEqualTo("name", projectCatalogEntity.getName())
            .orEqualTo("code", projectCatalogEntity.getCode()));

    即:WHERE ( is_deleted = ? and parent_id = ? ) and ( name = ? or code = ? )

    注意:条件3既可以用andEqualTo,也可以用orEqualTo。

    案例:

    Example example = new Example(Prepare.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("supplyId", prepare.getSupplyId());
            Example.Criteria c = example.createCriteria();
            c.orEqualTo("status", "0");
            c.orEqualTo("status", "1");
            example.and(c);
            List<Prepare> list = prepareMapper.selectByExample(example);

    即:WHERE ( supply_id = ? )  AND ( status = ? OR status = ? ) 



  • 相关阅读:
    数据分析的5层解读,报表仍是有效的落地实践!
    rex 传文件改变用户属主
    rex 通过--parameter1=dbcdefg传参
    rex 给shell 脚本传参
    rex run 传参
    rex ssh公钥认证
    eclipse maven工程中src/main/resources目录下创建的文件夹是包图标的解决方法
    数据分析的三层需求
    com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value
    java.sql.SQLException: Can not issue data manipulation statements with executeQuery()
  • 原文地址:https://www.cnblogs.com/zwh0910/p/13898826.html
Copyright © 2020-2023  润新知