Mybatis-技术专区-Mapper接口以及Example的实例函数及详解
一.mapper接口中的方法解析
mapper接口中的函数及方法
- 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 按主键查询
- List selectByExample(UserExample example) thorws SQLException 按条件查询
- List selectByExampleWithBLOGs(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的字段
二.example实例解析
mybatis的逆向工程中会生成实例及实例对应的example,example用于添加条件,相当where后面的部分
xxxExample example = new xxxExample();
Criteria criteria = new Example().createCriteria();
- example.setOrderByClause(“字段名 ASC”); 添加升序排列条件,DESC为降序
- example.setDistinct(false) 去除重复,boolean型,true为选择不重复的记录。
- criteria.andXxxIsNull 添加字段xxx为null的条件
- criteria.andXxxIsNotNull 添加字段xxx不为null的条件
- criteria.andXxxEqualTo(value) 添加xxx字段等于value条件
- criteria.andXxxNotEqualTo(value) 添加xxx字段不等于value条件
- criteria.andXxxGreaterThan(value) 添加xxx字段大于value条件
- criteria.andXxxGreaterThanOrEqualTo(value) 添加xxx字段大于等于value条件
- criteria.andXxxLessThan(value) 添加xxx字段小于value条件
- criteria.andXxxLessThanOrEqualTo(value) 添加xxx字段小于等于value条件
- criteria.andXxxIn(List<?>) 添加xxx字段值在List<?>条件
- criteria.andXxxNotIn(List<?>) 添加xxx字段值不在List<?>条件
- criteria.andXxxLike(“%”+value+”%”) 添加xxx字段值为value的模糊查询条件
- criteria.andXxxNotLike(“%”+value+”%”) 添加xxx字段值不为value的模糊查询条件
- criteria.andXxxBetween(value1,value2) 添加xxx字段值在value1和value2之间条件
- criteria.andXxxNotBetween(value1,value2) 添加xxx字段值不在value1和value2之间条件
三、应用举例
selectByExample() 和 selectByExampleWithBLOGs()
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("wyw");
criteria.andUsernameIsNull();
example.setOrderByClause("username asc,email desc");
List<?>list = XxxMapper.selectByExample(example);
//相当于:select * from user where username = 'wyw' and username is null order by username asc,email desc
注:在mybatis逆向工程生成的文件XxxExample.java中包含一个static的内部类Criteria,Criteria中的方法是定义SQL 语句where后的查询条件。
2.插入数据
①insert()
User user = new User();
user.setId("dsfgsdfgdsfgds");
user.setUsername("admin");
user.setPassword("admin")
user.setEmail("wyw@163.com");
XxxMapper.insert(user);
//相当于:insert into user(ID,username,password,email) values ('dsfgsdfgdsfgds','admin','admin','wyw@126.com');
3.修改数据
③ updateByExample() 和 updateByExampleSelective()
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("admin");
User user = new User();
user.setPassword("wyw");
XxxMapper.updateByPrimaryKeySelective(user,example);
//相当于:update user set password='wyw' where username='admin'
updateByExample()更新所有的字段,包括字段为null的也更新,建议使用 updateByExampleSelective()更新想更新的字段
4.删除数据
deleteByExample()
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("admin");
XxxMapper.deleteByExample(example);
//相当于:delete from user where username='admin'
5.查询数据数量
①countByExample()
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("wyw");
int count = XxxMapper.countByExample(example);
//相当于:select count(*) from user where username='wyw'
Mybatis-技术专区-Mapper接口以及Example的实例函数及详解的更多相关文章
- MyBatis的Mapper接口以及Example的实例函数及详解
来源:https://blog.csdn.net/biandous/article/details/65630783 一.mapper接口中的方法解析 mapper接口中的函数及方法 方法 功能说明 ...
- MyBatis逆向工程中的Mapper接口以及Example的实例函数及详解
一.mapper接口中的方法解析 mapper接口中的函数及方法 方法 功能说明 int countByExample(UserExample example) thorws SQLException ...
- mybatis中的mapper接口文件以及example类的实例函数以及详解
##Example example = new ##Example(); example.setOrderByClause("字段名 ASC"); //升序排列,desc为降序排列 ...
- Mybatis的逆向工程以及Example的实例函数及详解
Mybatis-generator是Mybatis的逆向工程 (根据数据库中的表生成java代码) Mybatis的逆向工程会生成实例及实例对应的example,example用于添加条件,相当于w ...
- mybatis如何根据mapper接口生成其实现类
SpringBoot集成mybatis mybatis的statement的解析与加载 mybatis如何根据mapper接口生成其实现类 mybatis的mapper返回map结果集 mybatis ...
- idea插件(mybatis框架下mapper接口快速跳转对应xml文件)亲测好用!
我相信目前在绝大部分公司里,主要使用的框架是S(spring)S(spring MVC)M(mybatis),其中mybatis总体架构是编写mapper接口,框架扫描其对应的mapper.xml文件 ...
- iBatis——自动生成DAO层接口提供操作函数(详解)
iBatis——自动生成DAO层接口提供操作函数(详解) 在使用iBatis进行持久层管理时,发现在使用DAO层的updateByPrimaryKey.updateByPrimaryKeySelect ...
- mybatis中的mapper接口文件以及selectByExample类的实例函数详解
记录分为两个部分,第一部分主要关注selectByExample类的实例函数的实现:第二部分讨论Mybatis框架下基本的实例函数. (一)selectByExample类的实例函数的实现 当你启动项 ...
- mybatis如何根据mapper接口生成其实现类(springboot)
序 mybatis里头给sqlSession指定执行哪条sql的时候,有两种方式,一种是写mapper的xml的namespace+statementId,如下: public Student fin ...
随机推荐
- linux强制用户下线命令
linux强制用户下线命令 前提:必须是root权限操作:(1)使用who查看目前有哪些用户登录了服务器,见下图(2)使用pkill -kill -t pts/1命令踢出第一个用户.命令解释:pt ...
- windows2008R2-AD域控组策略设置与其它相关设置
防火墙设置 修改>计算机配置>策略>安全设置>高级安全windows防火墙>高级安全windows防火墙 修改入站规则 1.组名-文件和打印机共享(SMB-In)> ...
- CSS基础知识总结之css样式引用的三种方式
在html中增加css样式有三种: 1.在标签中增加style属性: <!DOCTYPE html> <html lang="en" xmlns="ht ...
- [USACO16JAN]愤怒的奶牛Angry Cows (单调队列优化dp)
题目链接 Solution 应该可以用二分拿部分分,时间 \(O(n^2logn)\) . 然后可以考虑 \(n^2\) \(dp\) ,令 \(f_i\) 代表 \(i\) 点被激活,然后激活 \( ...
- 关于CSS你应该知道的基础知识 - 样式应用篇
上一篇简单总结了一下选择器,如果一个元素被多个选择器选中,元素的样式就会以级联方式被应用到.要搞清最终那个样式被应用到元素上了,首先要明白引用CSS代码的几种方式. CSS代码引用方式 如何应用CSS ...
- laravel5.6 操作数据 Eloquent ORM
建立Users模型 <?php namespace App\Model\Eloquent\Admin; use Illuminate\Database\Eloquent\Model; class ...
- 关于C(n,m) 的奇偶 ,与C(n,0),C(n,1),C(n,2)…C(n,n).当中有多少个奇数
(n & m) == m 为奇数 C(n,0),C(n,1),C(n,2)…C(n,n).当中有多少个奇数 第一种想法是Lucas定理推导,我们分析一下 C(n,m)%2,那么由lucas定 ...
- 第九届ECNU Coder A.足球锦标赛
题目链接:http://acm.ecnu.edu.cn/contest/16/problem/A/ 题目: A. 足球锦标赛 Time limit per test: 2.0 seconds Time ...
- crazyflie四轴飞行器
源地址:http://www.bitcraze.se/2013/02/pre-order-has-started/ Crazyflie是一个开源的纳米四旋翼 来几张靓照 开发平台是开源的,所以原理图和 ...
- 【CDN+】 Hbase入门 以及Hbase shell基础命令
前言 大数据的基础离不开Hbase, 本文就hbase的基础概念,特点,以及框架进行简介, 实际操作种需要注意hbase shell的使用. Hbase 基础 官网:https://hbase.ap ...