1. 程式人生 > >mybatis中關於example類詳解

mybatis中關於example類詳解

這幾天剛接觸example,很多內容都是破碎的,寫一篇博文加深理解。

一、什麼是example類

     mybatis-generator會為每個欄位產生如上的Criterion,如果表的欄位比較多,產生的Example類會十分龐大。理論上通過example類可以構造你想到的任何篩選條件。在mybatis-generator中加以配置,配置資料表的生成操作就可以自動生成example了。具體配置可以參考MBG有關配置
     下面是mybatis自動生成example的使用。

二、瞭解example成員變數

     //升序還是降序
     //引數格式:欄位+空格+asc(desc)
     protected String orderByClause;
     //去除重複
     //true是選擇不重複記錄
     protected boolean distinct;
     //自定義查詢條件
     //Criteria的集合,集合中物件是由or連線
     protected List<Criteria> oredCriteria;
     //內部類Criteria包含一個Cretiron的集合,
     //每一個Criteria物件內包含的Cretiron之間
     //是由AND連線的
     public static class Criteria extends GeneratedCriteria {
         protected Criteria() {
             super(); 
         }
     }
     //是mybatis中逆向工程中的程式碼模型
     protected abstract static class GeneratedCriteria
     {…..}
     //是最基本,最底層的Where條件,用於欄位級的篩選
     public static class Criterion {……}

三、example使用前的準備

     比如我的example是根據user表生成的,UserMapper屬於dao層,UserMapper.xml是對應的對映檔案
     UserMapper介面:

long countByExample(CompetingStoreExample example);
List<CompetingStore> selectByExample(CompetingStoreExample example);

    在我們的測試類裡:

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

四、查詢使用者數量

 long count = UserMapper.countByExample(example);

      類似於:select count(*) from user

五、where條件查詢或多條件查詢

     example.setOrderByClause("age asc");//升序
     example.setDistinct(false);//不去重

     if(!StringUtils.isNotBlank(user.getName())){
          Criteria.andNameEqualTo(user.getName());
     }

     if(!StringUtils.isNotBlank(user.getSex())){
          Criteria.andSexEqualTo(user.getSex());
     }

     List<User> userList=userMapper.selectByExample(example);

     類似於:select * from user where name={#user.name} and sex={#user.sex} order by age asc;

     UserExample.Criteria criteria1 = example.createCriteria();
     UserExample.Criteria criteria2 = example.createCriteria();

     if(!StringUtils.isNotBlank(user.getName())){
          Criteria1.andNameEqualTo(user.getName());
     }

     if(!StringUtils.isNotBlank(user.getSex())){
          Criteria2.andSexEqualTo(user.getSex());
     }

     Example.or(criteria2);
     List<User> userList=userMapper.selectByExample(example);

     類似於:select * from user where name={#user.name} or sex={#user.sex} ;

六、模糊查詢

      if(!StringUtils.isNotBlank(user.getName())){
           criteria.andNameLIke(‘%’+name+’%’);
      }

      List<User>  userList=userMapper.selectByExample(example);

      類似於:select * from user where name like %{#user.name}%

七、分頁查詢

        int start = (currentPage - 1) * rows;
        //分頁查詢中的一頁數量
        example.setPageSize(rows);   
        //開始查詢的位置
        example.setStartRow(start);
        List<User> userList=userMapper.selectByExample(example);

        類似於:select * from user limit start to rows