Java中mybatis關(guān)于example類的使用詳解
這幾天剛接觸example,很多內(nèi)容都是破碎的,寫一篇博文加深理解。
一、什么是example類
mybatis-generator會(huì)為每個(gè)字段產(chǎn)生如上的Criterion,如果表的字段比較多,產(chǎn)生的Example類會(huì)十分龐大。理論上通過(guò)example類可以構(gòu)造你想到的任何篩選條件。在mybatis-generator中加以配置,配置數(shù)據(jù)表的生成操作就可以自動(dòng)生成example了。具體配置可以參考MBG有關(guān)配置。 下面是mybatis自動(dòng)生成example的使用。
二、了解example成員變量
//升序還是降序 //參數(shù)格式:字段+空格+asc(desc) protected String orderByClause; //去除重復(fù) //true是選擇不重復(fù)記錄 protected boolean distinct; //自定義查詢條件 //Criteria的集合,集合中對(duì)象是由or連接 protected List<Criteria> oredCriteria; //內(nèi)部類Criteria包含一個(gè)Cretiron的集合, //每一個(gè)Criteria對(duì)象內(nèi)包含的Cretiron之間 //是由AND連接的 public static class Criteria extends GeneratedCriteria { protected Criteria() { super(); } } //是mybatis中逆向工程中的代碼模型 protected abstract static class GeneratedCriteria {…..} //是最基本,最底層的Where條件,用于字段級(jí)的篩選 public static class Criterion {……}
三、example使用前的準(zhǔn)備
比如我的example是根據(jù)user表生成的,UserMapper屬于dao層,UserMapper.xml是對(duì)應(yīng)的映射文件 UserMapper接口:
long countByExample(CompetingStoreExample example);List<CompetingStore> selectByExample(CompetingStoreExample example);
在我們的測(cè)試類里:
UserExample example = new UserExample(); UserExample.Criteria criteria = example.createCriteria();
四、查詢用戶數(shù)量
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}%
七、分頁(yè)查詢
int start = (currentPage - 1) * rows;//分頁(yè)查詢中的一頁(yè)數(shù)量example.setPageSize(rows); //開始查詢的位置example.setStartRow(start);List<User> userList=userMapper.selectByExample(example);
類似于:
select * from user limit start to rows
到此這篇關(guān)于Java中mybatis中關(guān)于example類的使用詳解的文章就介紹到這了,更多相關(guān)Java mybatis中example類內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. PHP使用Swagger生成好看的API文檔2. ASP.NET MVC使用jQuery ui的progressbar實(shí)現(xiàn)進(jìn)度條3. Python3 json模塊之編碼解碼方法講解4. Python 制作查詢商品歷史價(jià)格的小工具5. Python 如何調(diào)試程序崩潰錯(cuò)誤6. Python 利用Entrez庫(kù)篩選下載PubMed文獻(xiàn)摘要的示例7. ASP基礎(chǔ)知識(shí)VBScript基本元素講解8. python使用jenkins發(fā)送企業(yè)微信通知的實(shí)現(xiàn)9. Python sublime安裝及配置過(guò)程詳解10. Python 合并拼接字符串的方法

網(wǎng)公網(wǎng)安備