1. 程式人生 > >mybatis中的mapper介面檔案以及example類的例項函式以及詳解

mybatis中的mapper介面檔案以及example類的例項函式以及詳解

    ##Example example = new ##Example();  
    example.setOrderByClause("欄位名 ASC"); //升序排列,desc為降序排列。  
    example.setDistinct(false)//去除重複,boolean型,true為選擇不重複的記錄。  
    Criteria criteria = new Example().createCriteria();  
    is null;is not null;  
    equal to(value);not equal to(value);  
    GreaterThan(value);GreaterThanOrEqualTo(value);  
    LessThan(value); LessThanOrEqualTo(value);  
    in(item,item,item,...);not in(item,item,item,...);  
    like("%"+value+"%");not like("%"+value+"%");  
    Between(value1,value2);not between(value1,value2)  
      
       
      
    mybatis中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:按主鍵查詢。  
    List<?>selectByExample(UserExample example) thorws SQLException:按條件查詢  
    List<?>selectByExampleWithBLOGs(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的欄位  
      
    mybatis中mapper的例項函式詳解:  
    ① selectByPrimaryKey()  
      
    User user = ##Mapper.selectByPrimaryKey(100); 相當於select * from user where  
      
    id = 100  
      
    ② selectByExample() 和 selectByExampleWithBLOGs()  
      
    UserExample example = new UserExample();  
    Criteria criteria = example.createCriteria();  
    criteria.andUsernameEqualTo("joe");  
    criteria.andUsernameIsNull();  
    example.setOrderByClause("username asc,email desc");  
    List<?>list = ##Mapper.selectByExample(example);  
    相當於:select * from user where username = 'joe' and username is null order  
      
    by username asc,email desc  
      
    注:在iBator 生成的檔案UserExample.java中包含一個static 的內部類 Criteria ,  
      
    在Criteria中有很多方法,主要是定義SQL 語句where後的查詢條件。  
      
    ③ insert()  
      
    User user = new User();  
    user.setId(101);  
    user.setUsername("test");  
    user.setPassword("123")  
    user.setEmail("
[email protected]
"); ##Mapper.insert(user); 相當於:insert into user(ID,username,password,email) values (101,'test','123','[email protected]'); ④ updateByPrimaryKey() 和 updateByPrimaryKeySelective() User user =new User(); user.setId(101); user.setUsername("joe"); user.setPassword("joe"); user.setEmail("
[email protected]
"); ##Mapper.updateByPrimaryKey(user); 相當於:update user set username='joe',password='joe',email='[email protected]' where id=101 User user = new User(); user.setId(101); user.setPassword("joe"); ##Mapper.updateByPrimaryKeySelective(user); 相當於:update user set password='joe' where id=101 ⑤ updateByExample() 和 updateByExampleSelective() UserExample example = new UserExample(); Criteria criteria = example.createCriteria(); criteria.andUsernameEqualTo("joe"); User user = new User(); user.setPassword("123"); ##Mapper.updateByPrimaryKeySelective(user,example); 相當於:update user set password='123' where username='joe' ⑥ deleteByPrimaryKey() ##Mapper.deleteByPrimaryKey(101); 相當於:delete from user where id=101 ⑦ deleteByExample() UserExample example = new UserExample(); Criteria criteria = example.createCriteria(); criteria.andUsernameEqualTo("joe"); ##Mapper.deleteByExample(example); 相當於:delete from user where username='joe' ⑧ countByExample() UserExample example = new UserExample(); Criteria criteria = example.createCriteria(); criteria.andUsernameEqualTo("joe"); int count = ##Mapper.countByExample(example); 相當於:select count(*) from user where username='joe'

相關推薦

MyBatisMapper介面以及Example例項函式

一、mapper介面中的方法解析mapper介面中的函式及方法方法功能說明int countByExample(UserExample example) thorws SQLException按條件計數int deleteByPrimaryKey(Integer id) th

MyBatisMapper對映檔案的輸入(parameterType)和輸出(resultType)對映

Mapper.xml對映檔案中定義了操作資料庫的sql,每個sql是一個statement,對映檔案是mybatis的核心。 輸入型別parameterType 1)傳遞簡單型別 傳遞簡單型別,前兩節課都見過,這裡只給出案例: 2)傳遞pojo物件 MyBat

MybatisMapper介面Example例項函式使用

宣告:本文章部分內容源自於CSDN博主biandous的部落格文章,在其基礎上進行了部分修正和程式碼修改。 一、Mapper介面方法 方法 功能說明 int countByExample(UserExample example) throws SQLExce

Mybatis Mapper XML 檔案 的學習(強烈推薦)

Auto-mapping As you have already seen in the previous sections, in simple cases MyBatis can auto-map the results for you and in others you will need

MyBatismapper介面方法多引數傳入

測試一個更新使用者密碼的mapper方法的時候出現了這個異常: Caused by: org.apache.ibatis.binding.BindingException: Parameter 'pa

HTML關於class內容空格多名的問題

get 關於 兩個類 轉載 http 一個 限制 一次 com 之所以想談談這個,不明所以。所以轉載下來方便自己看看。 問:像 class="info fl" 這種class定義是何意思? 答:這裏的空格隔開後,它們所代表的是兩個類名,分別為info和fl。 問:這樣寫的

宣告子例項化物件

測試程式碼: public class Employee { public String name; public String getType(){ return "Employee"; } } public class Manager extends Employee{

mybatismapper介面檔案以及example例項函式以及

##Example example = new ##Example(); example.setOrderByClause("欄位名 ASC"); //升序排列,desc為降序排列。 example.setDistinct(false)//去

mybatismapper接口文件以及example的實例函數以及

lean boolean sql 語句 sql amp keys value per lec ##Example example = new ##Example(); example.setOrderByClause("字段名 ASC"); //升序排列,de

mybatis自動生成entity層和dao層Mapper介面的各個方法的意義及example實體的用法

package cn.lichenyang.emall.dao; import cn.lichenyang.emall.entity.TbContent; import cn.lichenyang.emall.entity.TbContentExample; import

mybatis學習筆記之——mybatisMapper XML檔案select元素

select元素: Select元素用來定義查詢操作,常用屬性如下。 id:唯一識別符號。用來引用這條語句,需要和介面的方法名一致。 parameterType:將會傳入這條語句的引數類的完全限定名或別名。這個屬性是可選的,因為 MyBatis 可以通過 TypeHandler 推斷出具

mybatis學習筆記之——mybatisMapper XML檔案resultMap屬性

resultMap resultMap:自定義結果集對映規則,自定義某個JavaBean的封裝規則。 id:唯一id,方便引用。 type:自定義規則的Java類。 具體其他屬性詳細資訊和配置程式碼如下: <resultMap id="MyEmp" type="com.te

mybatismapper對映檔案insert的用法

insert元素,這個標籤還有如下屬性: 1,id:名稱空間中的唯一標誌符,可用來代表這個語句。 2,parameterType:即將傳入的語句引數的完全限定類名或者別名 3,flushCash:預設值為true,任何時候只要語句被呼叫都會清空一級快取和二級快取。 4,stateme

Mybatismapper.xml檔案插入資料返回自增主鍵

使用MyBatis往MySQL資料庫中插入一條記錄後,返回該條記錄的自增主鍵值。Mapper檔案應該怎麼寫呢? Mybatis的Mapper的標籤中有一個屬性,我們一起來看看: useGenerateKeys這個屬性,意思就是使用自增。我們需要將這個欄位設定為 true 。 同時,還需

Mybatismapper.xml檔案重要註解說明

#{}和${}的區別及使用參考: Mybatis中#{}和${}的區別以及對sql注入、預編譯、jdbcType的說明 Mybatis方法各種情況的傳參和取參參考:Mybatis的處理引數原始碼分析和方法傳參取參分析 1、<mapper> namespace:名稱空間;指定為

mybatis***Mapper.xml對映檔案的配置細節

mapper對映檔案9大元素 對映檔案是以<mapper>作為根節點,在根節點中支援9個元素,分別為 insert、update、delete、select(增刪改查); cache、cache-ref、resultMap、parameterMap、sql。 例子:

springboot 整合mybatismapper介面和對應的mapper對映檔案放在同一個包下的配置

一、springboot整合mybatis後,需要進行幾個步驟的配置: 1、mapper包下的mapper介面都需要新增@Mapper註解。 2、啟動類上面新增@MapperScan(basepackages={"com.web.mapper"})註解。 3、需要在po

mybatismapper檔案判斷屬性是否為空

在mybatis的mapper檔案中判斷物件屬性或者字串是否為空的時候常用以下判斷條件: <if test="type!=null and type!=''">       AND typ

使用mybatisMapper.xml檔案如何判斷多個引數不為空和null

第一種:使用where標籤 <select id="***" resultMap="BaseResultMap" parameterType="java.util.Map">select

mybatismapper的用法以及一些注意事項

        這幾天在公司做專案,到了收尾的階段,但是發現自己在使用mybatis的時候給自己留下了很多的坑,於是乎花了兩天的時間來除錯和重新構思自己寫下的mapper.xml檔案,總算是有些收穫,