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的映射文件,只需要添加更多的、
例:
<
select
id
=
""getMaxUserid""
resultclass
=
""java.lang.Integer""
>
<!--[CDATA[select max(id) from user]]>
</select-->
</
select
>
<!--[CDATA[select distinct username from user]]>
</select-->
然后在UserDAOImpl.java中实现如下的两个函数:
public
Integer getMaxUserid()
throws
SQLException{
return
(Integer)sqlMapClient.queryForObject(
"users.getMaxUserid"
);
}
public
List<!--?-->getUsernameList()
throws
SQLException{
List<!--?-->list = sqlMapClient.queryForList(“users.getUsernameList”);
return
list;
}
- iBator Example工具类
- iBatis 代码自动生成工具 iBator 及 Example 使用
- iBatis 代码自动生成工具 iBator 及 Example 使用
- iBatis 代码自动生成工具 iBator 及 Example 使用
- 用iBator工具生成Ibatis相关类包,其中Example类文件的Criteria类无法使用的问题
- Myeclipse--ibator工具
- ibator 工具补充类,自动生成Manager接口,ManagerImpl类
- ibator
- ibator
- 为 Ibatis 2.3.4 构建增强的 Apache Ibator 实体类生成工具
- iBatis 代码自动生成工具 iBator
- ibatis代码自动生成工具ibator
- iBatis 代码自动生成工具 iBator
- iBatis 代码自动生成工具 iBator
- ibatis代码自动生成工具ibator修改备忘
- iBATOR介绍,什么是iBATOR?
- ibator 插件 abator配置详解(ibatis自动生成实体类)
- ibatis用ibator工具生成数据库表xml映射文件注意增加
- 其他-HDU-1029-Ignatius and the Princess IV
- vim 折叠
- Valid Anagram
- listview中的视频播放实现,类似网易新闻客户的视频
- 文件下载
- iBator Example工具类
- Android各种轮子
- 第一天
- ImageView.ScaleType属性
- 致 Embarcadero 客户及经销伙伴信函
- 常用的硬件电路器件的选型及资料下载网站
- 九度王道考研机试真题 2011-3哈工 题目1052:找x 查找
- easyUI使用实例讲解
- java中JVM的原理2