iBatis——自動生成DAO層接口提供操作函數(詳解)
在使用iBatis進行持久層管理時,發現在使用DAO層的updateByPrimaryKey、updateByPrimaryKeySelective方法進行數據更新時,運行結果不一。因之前沒有仔細研究過iBatis框架,現特此查詢相關文章整理並記錄自動生成的DAO層接口提供操作函數詳細使用方式與區別如下:
iBator生成的DAO層的接口提供了以下操作函數:
序號 | 方法名 | 參數 | 返回值 | 異常 | 作用 | 備注 |
1 | countByExample | UserExample example | int | √ | 按條件計數 | |
2 | deleteByPrimaryKey | Integer id | int | √ | 按主鍵刪除 | |
3 | deleteByExample | UserExample example | int | √ | 按條件刪除 | |
4 | insert | User record | String/Integer | √ | 插入 (返回值為id值) | |
5 | selectByPrimaryKey | Integer id | User | √ | 按主鍵查詢 | |
6 | selectByExample | UserExample example | List<?> | √ | 按條件查詢 | |
7 | selectByExampleWithBLOGs | UserExample example | List<?> | √ | 按條件查詢(包括BLOB字段) | 當數據表中的字段類型有為二進制的才會產生。 |
8 | updateByPrimaryKey | User record | int | √ | 按主鍵更新 | |
9 | updateByPrimaryKeySelective | User record | int | √ | 按主鍵更新值不為null的字段 | |
10 | updateByExample | User record, UserExample example | int | √ | 按條件更新 | |
11 | updateByExampleSelective | User record, UserExample example | int | √ | 按條件更新值不為null的字段 |
詳解:
UserDAOImpl userDAO = new UserDAOImpl(SqlMapClientFactory.getSqlMapClient());
注:SqlMapClientFactory.getSqlMapClient():是自定義的類和方法,目的是獲取SqlMapClient.
① selectByPrimaryKey()
User user = userDAO.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 = userDAO.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");
userDAO.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");
userDAO.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");
userDAO.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");
userDAO.updateByPrimaryKeySelective(user,example);
相當於:update user set password='123' where username='joe'
⑥ deleteByPrimaryKey()
userDAO.deleteByPrimaryKey(101); 相當於:delete from user where id=101
⑦ deleteByExample()
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
userDAO.deleteByExample(example);
相當於:delete from user where username='joe'
⑧ countByExample()
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
int count = userDAO.countByExample(example);
相當於:select count(*) from user where username='joe'
注:該隨筆根據《iBatis——自動生成DAO層接口提供操作函數詳解》整理而成。