iBatis——自动生成DAO层接口提供操作函数(详解)

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("[email protected]");
userDAO.insert(user);
相当于:insert into user(ID,username,password,email) values(101,'test','123','[email protected]');

 ④ updateByPrimaryKey() 和 updateByPrimaryKeySelective()

User user =new User();
user.setId(101);
user.setUsername("joe");
user.setPassword("joe");
user.setEmail("[email protected]");
userDAO.updateByPrimaryKey(user);
相当于:update user set username='joe',password='joe',email='[email protected]' 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层接口提供操作函数详解整理而成。

猜你喜欢

转载自www.cnblogs.com/ylhssn/p/4184974.html