mapper介面中的方法解析

2021-10-22 09:30:58 字數 2446 閱讀 6371

原文:mybatis逆向工程生成的example類的使用_thedarkclouds的部落格-csdn部落格_mybatis逆向工程生成的example類的使用

方法    功能說明

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後面的部分 

***example example = new ***example(); 

criteria criteria = new example().createcriteria();

方法    說明

example.setorderbyclause(「欄位名 asc」);    新增公升序排列條件,desc為降序

example.setdistinct(false)    去除重複,boolean型,true為選擇不重複的記錄。

criteria.and***isnull    新增字段***為null的條件

criteria.and***isnotnull    新增字段***不為null的條件

criteria.and***equalto(value)    新增***字段等於value條件

criteria.and***notequalto(value)    新增***欄位不等於value條件

criteria.and***greaterthan(value)    新增***字段大於value條件

criteria.and***greaterthanorequalto(value)    新增***字段大於等於value條件

criteria.and***lessthan(value)    新增***字段小於value條件

criteria.and***lessthanorequalto(value)    新增***字段小於等於value條件

criteria.and***in(list<?>)    新增***字段值在list<?>條件

criteria.and***notin(list<?>)    新增***字段值不在list<?>條件

criteria.and***like(「%」+value+」%」)    新增***字段值為value的模糊查詢條件

criteria.and***notlike(「%」+value+」%」)    新增***字段值不為value的模糊查詢條件

criteria.and***between(value1,value2)    新增***字段值在value1和value2之間條件

criteria.and***notbetween(value1,value2)    新增***字段值不在value1和value2之間條件

三、應用舉例

1.查詢

① selectbyprimarykey()

2.插入資料

①insert()

①updatebyprimarykey()

4.刪除資料

①deletebyprimarykey()

①countbyexample()

mapper介面引數為map的情況

1 單個引數,那麼直接使用mybatis語法即可。2 多個引數 2.1 多個引數的情況下,如果型別相同的話,比如都是string型別,可以直接使用mybaits的parametertype string 2.1 多個引數,但是型別不相同,比如void add integer factorystatu...

Mapper 中 與 的區別

1 是將傳入的值當做字串的形式,eg select id,name,age from student where id 當前端把id值1,傳入到後台的時候,就相當於 select id,name,age from student where id 1 2 是將傳入的資料直接顯示生成sql語句,eg ...

Mapper的四個方法

1 protected void setup context context 一般用來載入一些初始化的工作,每個job執行一次 protected void setup context context throws ioexception,interruptedexception catch ioe...