Java中mybatis關(guān)于example類(lèi)的使用詳解
這幾天剛接觸example,很多內(nèi)容都是破碎的,寫(xiě)一篇博文加深理解。
一、什么是example類(lèi)
mybatis-generator會(huì)為每個(gè)字段產(chǎn)生如上的Criterion,如果表的字段比較多,產(chǎn)生的Example類(lèi)會(huì)十分龐大。理論上通過(guò)example類(lèi)可以構(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; //自定義查詢(xún)條件 //Criteria的集合,集合中對(duì)象是由or連接 protected List<Criteria> oredCriteria; //內(nèi)部類(lè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è)試類(lèi)里:
UserExample example = new UserExample(); UserExample.Criteria criteria = example.createCriteria();
四、查詢(xún)用戶(hù)數(shù)量
long count = UserMapper.countByExample(example);
類(lèi)似于:select count(*) from user
五、where條件查詢(xún)或多條件查詢(xún)
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);
類(lèi)似于: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);
類(lèi)似于:select * from user where name={#user.name} or sex={#user.sex} ;
六、模糊查詢(xún)
if(!StringUtils.isNotBlank(user.getName())){ criteria.andNameLIke(‘%’+name+’%’); } List<User> userList=userMapper.selectByExample(example);
類(lèi)似于:
select * from user where name like %{#user.name}%
七、分頁(yè)查詢(xún)
int start = (currentPage - 1) * rows;//分頁(yè)查詢(xún)中的一頁(yè)數(shù)量example.setPageSize(rows); //開(kāi)始查詢(xún)的位置example.setStartRow(start);List<User> userList=userMapper.selectByExample(example);
類(lèi)似于:
select * from user limit start to rows
到此這篇關(guān)于Java中mybatis中關(guān)于example類(lèi)的使用詳解的文章就介紹到這了,更多相關(guān)Java mybatis中example類(lèi)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向2. Python importlib動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)代碼3. android studio 打包自動(dòng)生成版本號(hào)與日期,apk輸入路徑詳解4. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法5. 淺談python出錯(cuò)時(shí)traceback的解讀6. 在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解7. .NET中l(wèi)ambda表達(dá)式合并問(wèn)題及解決方法8. Nginx+php配置文件及原理解析9. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解10. JSP數(shù)據(jù)交互實(shí)現(xiàn)過(guò)程解析
