注意:希望通过此篇文章分享 可以使大家对mapper接口以及example 用法更加深入理解

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 按主键查询
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的字段

二、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之间条件

三、应用举例

1.查询

① selectByPrimaryKey()

User user = XxxMapper.selectByPrimaryKey(100); //相当于select * from user where id = 100
  • 1

② 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

注:在iBator逆向工程生成的文件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');
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.更新数据

①updateByPrimaryKey()

User user =new User();
user.setId("dsfgsdfgdsfgds");
user.setUsername("wyw");
user.setPassword("wyw");
user.setEmail("wyw@163.com");
XxxMapper.updateByPrimaryKey(user);
//相当于:update user set username='wyw', password='wyw', email='wyw@163.com' where id='dsfgsdfgdsfgds'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

②updateByPrimaryKeySelective()

User user = new User();
user.setId("dsfgsdfgdsfgds");
user.setPassword("wyw");
XxxMapper.updateByPrimaryKey(user);
//相当于:update user set password='wyw' where id='dsfgsdfgdsfgds'
  • 1
  • 2
  • 3
  • 4
  • 5

③ 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'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

updateByExample()更新所有的字段,包括字段为null的也更新,建议使用 updateByExampleSelective()更新想更新的字段

4.删除数据

①deleteByPrimaryKey()

XxxMapper.deleteByPrimaryKey(1);  //相当于:delete from user where id=1
  • 1

②deleteByExample()

UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("admin");
XxxMapper.deleteByExample(example);
//相当于:delete from user where username='admin'
  • 1
  • 2
  • 3
  • 4
  • 5

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'

Mybateis mapper 接口 example 用法的更多相关文章

  1. Mybatis的mapper接口在Spring中实例化过程

    在spring中使用mybatis时一般有下面的配置 <bean id="mapperScannerConfigurer" class="org.mybatis.s ...

  2. Mybatis是如何将Mapper接口注册到Spring IoC的

    1. 前言 有时候我们需要自行定义一些注解来标记某些特定功能的类并将它们注入Spring IoC容器.比较有代表性的就是Mybatis的Mapper接口.假如有一个新的需求让你也实现类似的功能你该如何 ...

  3. 由一个RABBITMQ监听器死循环引出的SPRING中BEAN和MAPPER接口的注入问题

    1 @Slf4j 2 @RestController 3 @Component 4 public class VouchersReceiverController implements Message ...

  4. Mybatis的mapper接口接受的参数类型

    最近项目用到了Mybatis,学一下记下来. Mybatis的Mapper文件中的select.insert.update.delete元素中有一个parameterType属性,用于对应的mappe ...

  5. MyBatis Mapper 接口如何通过JDK动态代理来包装SqlSession 源码分析

    我们以往使用ibatis或者mybatis 都是以这种方式调用XML当中定义的CRUD标签来执行SQL 比如这样 <?xml version="1.0" encoding=& ...

  6. mybatis的基本配置:实体类、配置文件、映射文件、工具类 、mapper接口

    搭建项目 一:lib(关于框架的jar包和数据库驱动的jar包) 1,第一步:先把mybatis的核心类库放进lib里

  7. Mybatis源码解析-MapperRegistry注册mapper接口

    知识储备 SqlsessionFactory-mybatis持久层操作数据的根本,具体的解析是通过SqlSessionFactoryBean生成的,具体的形成可见>>>Spring ...

  8. mybatis 详解(六)------通过mapper接口加载映射文件

    通过 mapper 接口加载映射文件,这对于后面 ssm三大框架 的整合是非常重要的.那么什么是通过 mapper 接口加载映射文件呢? 我们首先看以前的做法,在全局配置文件 mybatis-conf ...

  9. Mybatis中是否需要依赖配置文件的名称要和mapper接口的名称一致 params错误

    一:当核心配置文件mapper标签下以resource形式指向依赖配置文件时,不需要 这样就可以加载到其相应的依赖配置文件通过namespace找到其相应的方法 二:如果mapper标签下以packa ...

随机推荐

  1. 二叉排序树插入C语言版 递归步骤理解

    //二叉排序树 插入 (纯C语言实现) BTNode * BSTInsert2(BTNode *bt,int key){ //为什么纯C语言实现中 if(bt==NULL){ //要写成 bt-> ...

  2. C语言排序算法学习笔记——交换类排序

    交换类排序:根据序列中两个元素关键字的比较结果来交换他俩在序列中的位置. 冒泡排序:假设待排序表长为n,从后往前(或从前往后)两两比较相邻元素的值,若为逆序(即A[i-1]>A[i])则交换他们 ...

  3. less和sass的区别

    首先sass和less都是css的预编译处理语言,他们引入了mixins,参数,嵌套规则,运算,颜色,名字空间,作用域,JavaScript赋值等 加快了css开发效率,当然这两者都可以配合gulp和 ...

  4. 20165205 《网络对抗技术》 Exp0 Kali安装

    20165205 <网络对抗技术> Exp0 Kali安装 一.下载及安装 打开官方下载网站 我下载的是vm版,64位,如下图 之后继续点击 之后就可以等待下载成功了,成功之后找到.vmx ...

  5. [UNITY 5.4 UGUI] 控件重叠触摸穿透

    问题. imge 和 button重叠时,imge 覆盖在button上面,导致点击事件无法传递到button. 1.给imge 添加 [Canvas Group]组件 2.修改[Canvas Gro ...

  6. SockJS

    1\ 2\ 下载

  7. FPGA笔试必会知识点2—FPGA器件

    FPGA 综合工具并不一定保证能够充分利用芯片结构特点以达到最优目的而且工具本身也不一定非常智能,因为设计本身是复杂多样的且一直在变化,问题总会越来越多,因此在这种情况下,我们必须了解我们的器件结构, ...

  8. python3 在文件确实存在的情况下,运行提示找不到文件

    提示 [Errno 2] No such file or directory: 但是路径下确实存在此文件,在不改动的情况下,再次运行,执行成功. 百思不得其解,看到此链接下的回答 http://bbs ...

  9. IIS下uploadify上传大文件出现404错误(提示上传文件大小超过400M)

    原因:由于IIS7下的默认设置限制了上传大小,所以Web.Config中的大小设置也就失效了. 解决步骤: 1.打开IIS管理器,找到Default Web Site(也就是你的发布站点),先进行停止 ...

  10. mybatis映射文件#与$的使用,及参数传入规则

    mybaits映射文件中使用#与$场景: <select id="getProviders" resultType="com.lazy.bill.pojo.Prov ...