iBator Example工具类

来源:互联网 发布:斯托克顿生涯数据 编辑:程序博客网 时间:2024/06/15 17:15

iBator 数据库操作

通过iBator导出的文件包括映射文件、Domain类、DAO类。它导出的Domain类和DAO类是依据iBator设计的框架生成的,其中包括了各种函数。我们要基于这些类来开发Service层代码。

新生成的DAO层的接口提供了以下操作函数:

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:按主键查询。
ListselectByExample(UserExample example) thorws SQLException:按条件查询

ListselectByExampleWithBLOGs(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的字段

详解:

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");
Listlist = 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'

扩展DAO类实现更复杂的SQL

iBator插件只是给我们产生了一个满足基本功能的代码框架,比如:增、删、改、查、条件、排序的使用,这些都是iBator工具导出的函数实现的。但iBator不能给我们提供所有的函数,但由于iBatis框架是基于原生SQL的。因此,我们只需要在iBator代码插件产生的代码基础上进行扩展即可。扩招的方法当然是基于iBatis的映射文件,只需要添加更多的、

例:

1
2
3
4
5
6
<selectid=""getMaxUserid""resultclass=""java.lang.Integer"">
             <!--[CDATA[select max(id) from user]]>
</select-->
</select>
             <!--[CDATA[select distinct username from user]]>
</select-->

然后在UserDAOImpl.java中实现如下的两个函数:

1
2
3
4
5
6
7
publicInteger getMaxUserid() throwsSQLException{
         return(Integer)sqlMapClient.queryForObject("users.getMaxUserid");
}
publicList<!--?-->getUsernameList() throwsSQLException{
         List<!--?-->list = sqlMapClient.queryForList(“users.getUsernameList”);
         returnlist;
}
0 0
原创粉丝点击