1 ##Example example = new ##Example(); 2 example.setOrderByClause("欄位名 ASC"); //升序排列,desc為降序排列。 3 example.setDistinct(false)//去除重複,boolean型,true為選擇不重複的記錄。 4 Criteria criteria = new Example().createCriteria(); 5 is null;is not null; 6 equal to(value);not equal to(value); 7 GreaterThan(value);GreaterThanOrEqualTo(value); 8 LessThan(value); LessThanOrEqualTo(value); 9 in(item,item,item,...);not in(item,item,item,...); 10 like("%"+value+"%");not like("%"+value+"%"); 11 Between(value1,value2);not between(value1,value2) 12 13 14 15 mybatis中mapper的例項函式: 16 int countByExample(UserExample example) thorws SQLException:按條件計數。 17 int deleteByPrimaryKey(Integer id) thorws SQLException:按主鍵刪除。 18 int deleteByExample(UserExample example) thorws SQLException:按條件刪除。 19 String/Integer insert(User record) thorws SQLException:插入(返回值為id值) 20 User selectByPrimaryKey(Integer id) thorws SQLException:按主鍵查詢。 21 List<?>selectByExample(UserExample example) thorws SQLException:按條件查詢 22 List<?>selectByExampleWithBLOGs(UserExample example) thorws SQLException:按 23 24 條件查詢(包括BLOB欄位)。只有當資料表中的欄位型別有為二進位制的才會產生。 25 int updateByPrimaryKey(User record) thorws SQLException:按主鍵更新 26 int updateByPrimaryKeySelective(User record) thorws SQLException:按主鍵更新 27 28 值不為null的欄位 29 int updateByExample(User record, UserExample example) thorws SQLException: 30 31 按條件更新 32 int updateByExampleSelective(User record, UserExample example) thorws 33 34 SQLException:按條件更新值不為null的欄位 35 36 mybatis中mapper的例項函式詳解: 37 ① selectByPrimaryKey() 38 39 User user = ##Mapper.selectByPrimaryKey(100); 相當於select * from user where 40 41 id = 100 42 43 ② selectByExample() 和 selectByExampleWithBLOGs() 44 45 UserExample example = new UserExample(); 46 Criteria criteria = example.createCriteria(); 47 criteria.andUsernameEqualTo("joe"); 48 criteria.andUsernameIsNull(); 49 example.setOrderByClause("username asc,email desc"); 50 List<?>list = ##Mapper.selectByExample(example); 51 相當於:select * from user where username = 'joe' and username is null order 52 53 by username asc,email desc 54 55 注:在iBator 生成的檔案UserExample.java中包含一個static 的內部類 Criteria , 56 57 在Criteria中有很多方法,主要是定義SQL 語句where後的查詢條件。 58 59 ③ insert() 60 61 User user = new User(); 62 user.setId(101); 63 user.setUsername("test"); 64 user.setPassword("123") 65 user.setEmail("joe@163.com"); 66 ##Mapper.insert(user); 67 相當於:insert into user(ID,username,password,email) values 68 69 (101,'test','123','joe@163.com'); 70 71 ④ updateByPrimaryKey() 和 updateByPrimaryKeySelective() 72 73 User user =new User(); 74 user.setId(101); 75 user.setUsername("joe"); 76 user.setPassword("joe"); 77 user.setEmail("joe@163.com"); 78 ##Mapper.updateByPrimaryKey(user); 79 相當於:update user set username='joe',password='joe',email='joe@163.com' 80 81 where id=101 82 83 User user = new User(); 84 user.setId(101); 85 user.setPassword("joe"); 86 ##Mapper.updateByPrimaryKeySelective(user); 87 相當於:update user set password='joe' where id=101 88 89 ⑤ updateByExample() 和 updateByExampleSelective() 90 91 UserExample example = new UserExample(); 92 Criteria criteria = example.createCriteria(); 93 criteria.andUsernameEqualTo("joe"); 94 User user = new User(); 95 user.setPassword("123"); 96 ##Mapper.updateByPrimaryKeySelective(user,example); 97 相當於:update user set password='123' where username='joe' 98 99 ⑥ deleteByPrimaryKey() 100 101 ##Mapper.deleteByPrimaryKey(101); 相當於:delete from user where id=101 102 103 ⑦ deleteByExample() 104 105 UserExample example = new UserExample(); 106 Criteria criteria = example.createCriteria(); 107 criteria.andUsernameEqualTo("joe"); 108 ##Mapper.deleteByExample(example); 109 相當於:delete from user where username='joe' 110 111 ⑧ countByExample() 112 113 UserExample example = new UserExample(); 114 Criteria criteria = example.createCriteria(); 115 criteria.andUsernameEqualTo("joe"); 116 int count = ##Mapper.countByExample(example); 117 相當於:select count(*) from user where username='joe'