mybatis Example 使用方法

一、mapper接口中的方法解析
mapper接口中的函數及方法

方法 功能說明
int countByExample(UserExample example) thorws SQLException 按條件計數
int deleteByPrimaryKey(Integer id) thorws SQLException 按主鍵刪除
int deleteByExample(UserExample example) thorws SQLException 按條件查詢
String/Integer insert(User record) thorws SQLException 插入數據(返回值為ID)
User selectByPrimaryKey(Integer id) thorws SQLException 按主鍵查詢
ListselectByExample(UserExample example) thorws SQLException 按條件查詢
ListselectByExampleWithBLOGs(UserExample example) thorws SQLException 按條件查詢(包括BLOB字段)。只有當數據表中的字段類型有為二進制的才會產生。
int updateByPrimaryKey(User record) thorws SQLException 按主鍵更新
int updateByPrimaryKeySelective(User record) thorws SQLException 按主鍵更新值不為null的字段
int updateByExample(User record, UserExample example) thorws SQLException 按條件更新
int updateByExampleSelective(User record, UserExample example) thorws SQLException 按條件更新值不為null的字段
二,example實例解析
mybatis的逆向工程中會生成實例及實例對應的example,example用于添加條件,相當where后面的部分
xxxExample example = new xxxExample();
Criteria criteria = new Example().createCriteria();

方法 說明
example.setOrderByClause(“字段名 ASC”); 添加升序排列條件,DESC為降序
example.setDistinct(false) 去除重復,boolean型,true為選擇不重復的記錄。
criteria.andXxxIsNull 添加字段xxx為null的條件
criteria.andXxxIsNotNull 添加字段xxx不為null的條件
criteria.andXxxEqualTo(value) 添加xxx字段等于value條件
criteria.andXxxNotEqualTo(value) 添加xxx字段不等于value條件
criteria.andXxxGreaterThan(value) 添加xxx字段大于value條件
criteria.andXxxGreaterThanOrEqualTo(value) 添加xxx字段大于等于value條件
criteria.andXxxLessThan(value) 添加xxx字段小于value條件
criteria.andXxxLessThanOrEqualTo(value) 添加xxx字段小于等于value條件
criteria.andXxxIn(List<?>) 添加xxx字段值在List<?>條件
criteria.andXxxNotIn(List<?>) 添加xxx字段值不在List<?>條件
criteria.andXxxLike(“%”+value+”%”) 添加xxx字段值為value的模糊查詢條件
criteria.andXxxNotLike(“%”+value+”%”) 添加xxx字段值不為value的模糊查詢條件
criteria.andXxxBetween(value1,value2) 添加xxx字段值在value1和value2之間條件
criteria.andXxxNotBetween(value1,value2) 添加xxx字段值不在value1和value2之間條件
三、應用舉例
1.查詢
① selectByPrimaryKey()

User user = XxxMapper.selectByPrimaryKey(100); //相當于select * from user where id = 100
1
② selectByExample() 和 selectByExampleWithBLOGs()

UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo(“wyw”);
criteria.andUsernameIsNull();
example.setOrderByClause(“username asc,email desc”);
List<?>list = XxxMapper.selectByExample(example);
//相當于:select * from user where username = ‘wyw’ and username is null order by username asc,email desc
1
2
注:在iBator逆向工程生成的文件XxxExample.java中包含一個static的內部類Criteria,Criteria中的方法是定義SQL 語句where后的查詢條件。

2.插入數據
①insert()

User user = new User();
user.setId(“dsfgsdfgdsfgds”);
user.setUsername(“admin”);
user.setPassword(“admin”)
user.setEmail(“wyw@163.com”);
XxxMapper.insert(user);
//相當于:insert into user(ID,username,password,email) values (‘dsfgsdfgdsfgds’,‘admin’,‘admin’,‘wyw@126.com’);
1
2
3.更新數據
①updateByPrimaryKey()

User user =new User();
user.setId(“dsfgsdfgdsfgds”);
user.setUsername(“wyw”);
user.setPassword(“wyw”);
user.setEmail(“wyw@163.com”);
XxxMapper.updateByPrimaryKey(user);
//相當于:update user set username=‘wyw’, password=‘wyw’, email=‘wyw@163.com’ where id=‘dsfgsdfgdsfgds’
1
2
②updateByPrimaryKeySelective()

User user = new User();
user.setId(“dsfgsdfgdsfgds”);
user.setPassword(“wyw”);
XxxMapper.updateByPrimaryKey(user);
//相當于:update user set password=‘wyw’ where id=‘dsfgsdfgdsfgds’
1
2
③ updateByExample() 和 updateByExampleSelective()

UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo(“admin”);
User user = new User();
user.setPassword(“wyw”);
XxxMapper.updateByPrimaryKeySelective(user,example);
//相當于:update user set password=‘wyw’ where username=‘admin’
1
2
updateByExample()更新所有的字段,包括字段為null的也更新,建議使用 updateByExampleSelective()更新想更新的字段

4.刪除數據
①deleteByPrimaryKey()

XxxMapper.deleteByPrimaryKey(1); //相當于:delete from user where id=1
1
②deleteByExample()

UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo(“admin”);
XxxMapper.deleteByExample(example);
//相當于:delete from user where username=‘admin’
1
2

5.查詢數據數量
①countByExample()

UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo(“wyw”);
int count = XxxMapper.countByExample(example);
//相當于:select count(*) from user where username=‘wyw’

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/249300.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/249300.shtml
英文地址,請注明出處:http://en.pswp.cn/news/249300.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

gulp + browsersync實現頁面自動刷新

寫習慣了vue&#xff0c;特別喜歡vue的自動刷新功能&#xff0c;于是琢磨在node中如何自動刷新&#xff0c;使用過nodemon&#xff0c; 但是感覺效果差點&#xff0c;看到網上有gulp livereload的方案和gulp browsersync的方案&#xff0c;但都是褒貶不一&#xff0c;先簡單記…

[JZOJ5836] Sequence

Problem 題目鏈接 Solution 吼題啊吼題&#xff01; 首先如何求本質不同的子序列個數就是 \(f[val[i]]1\sum\limits_{j1}^k f[j]\) 其中 \(f[i]\) 表示的是以 \(i\) 結尾的子序列個數 先把原數列的不同子序列個數求出來&#xff0c;然后觀察一下這個轉移&#xff0c;貪心的發現…

numpy和pandas的基礎索引切片

Numpy的索引切片 索引 In [72]: arr np.array([[[1,1,1],[2,2,2]],[[3,3,3],[4,4,4]]]) In [73]: arr Out[73]: array([[[1, 1, 1],[2, 2, 2]],[[3, 3, 3],[4, 4, 4]]])In [74]: arr.nd…

mybatis的Example[Criteria]的使用

https://blog.csdn.net/u014756578/article/details/86490052

Thunar 右鍵菜單等自定義

Thunar 右鍵菜單等自定義 可以使用圖形界面或者直接編輯配置文件&#xff0c;二者是等價的。 圖形界面&#xff1a; 以給“zip&#xff0c;rar&#xff0c;7z”等文件添加“在此位置使用unar解壓縮”的右鍵菜單為例&#xff1a;&#xff08;unar可以很好地處理編碼問題&#xf…

JavaScript設計模式(二)之單例模式

一、單例模式的定義 單例就是保證一個類只有一個實例&#xff0c;實現的方法一般是先判斷實例存在與否&#xff0c;如果存在直接返回&#xff0c;如果不存在就創建后再返回&#xff0c;這就確保了一個類只有一個實例對象。在JavaScript里&#xff0c;單例作為一個命名空間的提…

python全棧開發_day10_函數的實參和形參

一&#xff1a;函數的實參和形參 實參是在調用函數時()出現的外界的實際的值 形參不能再函數外部直接使用 1&#xff09;實參的兩種形式 實參是調用函數時()中傳入的參數 1.位置實參 def a(a):print(a)a(1)#得到返回值:1 2.關鍵字實參 def a(a,b):print(a,b)a(b3,a5)#得到返回值…

JAVA的(PO,VO,TO,BO,DAO,POJO)解釋

JAVA的(PO,VO,TO,BO,DAO,POJO)解釋 O/R Mapping 是 Object Relational Mapping&#xff08;對象關系映射&#xff09;的縮寫。通俗點講&#xff0c;就是將對象與關系數據庫綁定&#xff0c;用對象來表示關系數據。在O/R Mapping的世界里&#xff0c;有兩個基本的也是重要的東東…

使用wsimport命令生成webService客戶端代碼實例

https://blog.csdn.net/qq_39459412/article/details/79079865

學習網站大匯集

一.綜合類學習網站&#xff08;中文&#xff09; 1.網易公開課&#xff1a;https://open.163.com/。上面有TED、可汗學院、國內外高校公開課的免費資源。站內內容完全免費&#xff0c;良心推薦。 2.網易云課堂&#xff1a;http://study.163.com/。網易旗下付費學習平臺&#…

ios怎樣在一個UIImageButton的里面加一些自己定義的箭頭

能夠採用例如以下方法&#xff0c;寫一個函數&#xff1a; -(UIImage*) getOneImageButtonWithArrow{//tmpView做附控件UIView *tmpView [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 38.0f, 32.0f)];tmpView.backgroundColor [UIColor clearColor];//bgImg作為背景…

vue從入門到精通之基礎篇(一)語法概要

(1).vue起步 1:引包2:啟動 new Vue({el:目的地,template:模板內容});options 目的地 el內容 template數據 data 保存數據屬性 數據驅動視圖 (2).插值表達式 {{ 表達式 }} 對象 (不要連續3個{{ {name:‘jack’} }})字符串 {{ ‘xxx’ }}判斷后的布爾值 {{ true }}三元表達式…

dede 文章列表頁如何倒序排列

{dede:arclist row6 typeid18 orderwayasc} <li>;<a href"[field:arcurl/]">[field:title/]</a></li> {/dede:arclist} 正常排列&#xff1a;orderwayasc倒序排列&#xff1a;orderwaydesc轉載于:https://www.cnblogs.com/php-qiuwei/p/1062…

Chapter 5 Blood Type——24

"Shes just a little faint," he reassured the startled nurse. "Theyre blood typing in Biology." "她只是有點頭暈&#xff0c;" 他讓護士放心的說道。“他們再生物課上測血型。” The nurse nodded sagely. "Theres always one."…

vue從入門到精通之基礎篇(二)組件

(1).局部組件的使用 ? 渲染組件-父使用子組件 1: 創建子組件(對象) var Header { template:模板 , data是一個函數,methods:功能,components:子組件們 } 2: 在父組件中聲明,根屬性components:{ 組件名:組件對象 }3: 在父組件要用的地方使用 <組件名></組件名> …

@Scheduled

Scheduled注解的使用這里不詳細說明&#xff0c;直接對8個參數進行講解。 參數詳解 cron 該參數接收一個cron表達式&#xff0c;cron表達式是一個字符串&#xff0c;字符串以5或6個空格隔開&#xff0c;分開共6或7個域&#xff0c;每一個域代表一個含義。 cron表達式語法 […

eclipse2019-03設置代碼編輯區背景為圖片

一、我的主題設置如下所示 二、找到如下所示或類似的文件夾 三、在該文件夾里的images文件夾里添加圖片 四、在CSS目錄下的e4-dark_win.css文件中添加如下代碼   .MPart StyledText {     background-image: url(./bg.jpg);     background-repeat: no-repeat;  …

idea集成gitlab使用ssh免密登錄

網上有很多介紹ssh免密登錄的文章&#xff0c;具體步驟如下&#xff1a; 1. 生成SSH Key ssh-keygen -t rsa -C "your_emailexample.com" 默認會在相應路徑下&#xff08;/your_home_path&#xff09;生成id_rsa和id_rsa.pub兩個文件&#xff0c;此時終端會顯示&…

vue從入門到精通之基礎篇(三)生命周期

生命周期 定義&#xff1a; 每個 Vue 實例在被創建時都要經過從創建倒掛載再到更新、卸載的一系列過程&#xff0c;同時在這個過程中也會運行一些叫做生命周期鉤子的函數&#xff0c;可以讓我們用自己注冊的js方法控制整個大局&#xff0c;在這些事件響應方法中的this直接指向…

libcurl庫進行http通訊網絡編程

https://www.cnblogs.com/lifan3a/articles/7479256.html