Mybatis中example類的使用


要使用example類,先要在項目中導入mybatis.mapper的jar包。

Mapper接口中包含了單表的增刪改查以及分頁功能。

給出實例:

CountryMappermapper = sqlSession.getMapper(Country.class);

//Country.class是實體類

//查詢操作

List<Country>cList = mapper.select(new Country());

現在使用Example查詢

Example example =new Example(Country.class);

example.createCriteria().andEqualTo(“id”,100);

//這里給出查詢為id=100

cList = mapper.selectByExample(example);

 

example.setOrderByClause(“字段名ASC”); 以某字段升序排序

example.setDistinct(false)//去除重復,boolean型,true為選擇不重復的記錄

selectByExample()返回的是一個集合

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()

Country country = ##Mapper.selectByPrimaryKey(100);

相當於select * from user where id = 100

還有一些方法不在這里贅述,可以參考mybatis中的example

 

轉自 http://blog.csdn.net/qq_36743013/article/details/71144508


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM