##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("joe@163.com");
##Mapper.insert(user);
至關於:insert into user(ID,username,password,email) values
(101,'test','123','joe@163.com');
④ updateByPrimaryKey() 和 updateByPrimaryKeySelective()
User user =new User();
user.setId(101);
user.setUsername("joe");
user.setPassword("joe");
user.setEmail("joe@163.com");
##Mapper.updateByPrimaryKey(user);
至關於:update user set username='joe',password='joe',email='joe@163.com'
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' java