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 = ")
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 = ");
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层接口提供操作函数详解》整理而成。
iBatis——自动生成DAO层接口提供操作函数(详解)的更多相关文章
- Ibatis自动生成dao sqlmapper文件和domain文件过程
generator自动生成mybatis的xml配置.model.map等信息: 1.下载mybatis-generator-core-1.3.2.jar包. 网址:http://cod ...
- C-I/O操作函数详解
EOF: End Of File, 文字流结尾, 这里的文字流可以是文件(file), 也可以是标准输入(stdin), 它的值在任何可能出现的字符之外(-1) 先列出三种基本类型操作函数 这里面返回 ...
- mybatis generator 自动生成dao层映射代码
资源: doc url :http://www.mybatis.org/generator/ download:https://github.com/mybatis/generator/release ...
- OpenGL的glPushMatrix和glPopMatrix矩阵栈顶操作函数详解
OpenGL中图形绘制后,往往需要一系列的变换来达到用户的目的,而这种变换实现的原理是又通过矩阵进行操作的.opengl中的变换一般包括视图变换.模型变换.投影变换等,在每次变换后,opengl将会呈 ...
- c语言文件操作函数详解
一.文件操作注意点: 1 打开文件时,如果打开方式加“+”,表示该文件可以“写” ; 2 退出程序一般用exit函数,正常退出参数为0,非正常退出参数为正零值 ; 3 文件的读写操作:按字符.字符串. ...
- C-常用字符串操作函数详解
//使用字符串操作应时刻注意字符串或者数组长度!!避免溢出!! 1. size_t strlen(char const *str); //计算字符串长度, 注意返回size_t类型的值, 即unsig ...
- IDEA利用mybatis-generator自动生成dao和mapper
pom.xml配置 <properties> <java.version>1.8</java.version> <mybatis-generator-core ...
- MyBatis框架的XML数据访问Dao层接口的组合使用
MyBatis 的前生为Apache的开源项目iBatis.其优势在于灵活,几乎可以替代JDBC,同时提供了编程接口.目前MyBatis的数据访问Dao层不需要实现类,也不需要像JDBC那样拼接Hql ...
- mapper.xml是怎样实现Dao层接口
上午写了一个简单的 从xml读取信息实例化一个Bean对象.下午就开始想mybatis是怎么通过xml文件来实现dao层接口的,一开始想直接用Class.forName(String name)然后调 ...
随机推荐
- linux 服务器与客户端异常断开连接问题
服务器与客户端连接,客户端异常断掉之后服务器端口仍然被占用, 到最后是不是服务器端达到最大连接数就没法连接了?领导让我测试这种情况,我用自己的电脑当TCP Client,虚拟机当服务器,连接之后能正常 ...
- 部署Tomcat服务器
部署Tomcat服务器,具体内容如下: 1.安装部署JDK基础环境; 2.安装部署Tomcat服务器; 3.创建JSP测试页面,文件名为test.jsp,显示服务器当前时间. 然后客户机访问Web服务 ...
- luogu2046 海拔
题目链接[NOI2010]海拔 首先有个性质就是海拔只会有\(0\)和\(1\)两种. 证明:海拔下降和人数乘积为总消耗,确定了海拔下降总数,如果有个地方可以使得单位消耗最小,那么全部消耗不会更劣. ...
- git & gerrit & shell
g公司使用Gerrit改善评审流程. 比较麻烦.gerrit提交后会触发vertifyCI, 实施代码扫描. 这一堆过程, 打印出一堆信息, 都在log中, 所以处理log就需要自己写shell了. ...
- 容器(collection)初步
容器(集合)的分类: 泛型(generic):本质是数据类型的参数化(提前告诉编译器,在调用泛型时必须传入实际类型) 例:E即为在主函数中定义的传入的实际类型 class MyCollection&l ...
- No Spring Session store is configured: set the 'spring.session.store-type'
发现session store type使用来存放session的存储方式,目前Spring boot中只支持Redis方式. 由于本应用暂无需将session放入redis的需求,故这里就可以将se ...
- ubuntu 18.04设置系统自带系统截图快捷键
0.前言 ubuntu 18.04自带一个截图工具gnome-screenshot,有三种模式,全屏截图.当前活动窗口截图.选取活动区域截图 1.设置快捷键 Setting->Devices-& ...
- 给网页标题添加icon小图标
so easy: 给网页标题添加icon小图标: 直接在html文件的head部分添加下面代码(注意href的路径): <link rel="icon" type=" ...
- Python3.5自带venv创建虚拟环境
为每个程序单独创建虚拟环境可以保证程序只能访问虚拟环境中的包,保持全局解释器的干净整洁,使其只作为创建(更多)虚拟环境的源. windows下创建虚拟环境 Python3.5自带venv,只需执行py ...
- 【大前端攻城狮之路·二】Javascript&QA⼯程师
今天给大家分享的主题的是Javascript&QA⼯程师.看到这个主题,可能有人问:前端开发完就OK了,剩下的丢给测试就行,哪里还需要关心这些?但事实上呢,测试是前端开发非常重要的环节,也是迈 ...