要使用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