一.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的实例函数及详解的更多相关文章

  1. MyBatis的Mapper接口以及Example的实例函数及详解

    来源:https://blog.csdn.net/biandous/article/details/65630783 一.mapper接口中的方法解析 mapper接口中的函数及方法 方法 功能说明 ...

  2. MyBatis逆向工程中的Mapper接口以及Example的实例函数及详解

    一.mapper接口中的方法解析 mapper接口中的函数及方法 方法 功能说明 int countByExample(UserExample example) thorws SQLException ...

  3. mybatis中的mapper接口文件以及example类的实例函数以及详解

    ##Example example = new ##Example(); example.setOrderByClause("字段名 ASC"); //升序排列,desc为降序排列 ...

  4. Mybatis的逆向工程以及Example的实例函数及详解

    Mybatis-generator是Mybatis的逆向工程  (根据数据库中的表生成java代码) Mybatis的逆向工程会生成实例及实例对应的example,example用于添加条件,相当于w ...

  5. mybatis如何根据mapper接口生成其实现类

    SpringBoot集成mybatis mybatis的statement的解析与加载 mybatis如何根据mapper接口生成其实现类 mybatis的mapper返回map结果集 mybatis ...

  6. idea插件(mybatis框架下mapper接口快速跳转对应xml文件)亲测好用!

    我相信目前在绝大部分公司里,主要使用的框架是S(spring)S(spring MVC)M(mybatis),其中mybatis总体架构是编写mapper接口,框架扫描其对应的mapper.xml文件 ...

  7. iBatis——自动生成DAO层接口提供操作函数(详解)

    iBatis——自动生成DAO层接口提供操作函数(详解) 在使用iBatis进行持久层管理时,发现在使用DAO层的updateByPrimaryKey.updateByPrimaryKeySelect ...

  8. mybatis中的mapper接口文件以及selectByExample类的实例函数详解

    记录分为两个部分,第一部分主要关注selectByExample类的实例函数的实现:第二部分讨论Mybatis框架下基本的实例函数. (一)selectByExample类的实例函数的实现 当你启动项 ...

  9. mybatis如何根据mapper接口生成其实现类(springboot)

    序 mybatis里头给sqlSession指定执行哪条sql的时候,有两种方式,一种是写mapper的xml的namespace+statementId,如下: public Student fin ...

随机推荐

  1. 消灭 Java 代码的“坏味道”

    消灭 Java 代码的“坏味道” 原创: 王超 阿里巴巴中间件 昨天 导读 明代王阳明先生在<传习录>谈为学之道时说: 私欲日生,如地上尘,一日不扫,便又有一层.着实用功,便见道无终穷,愈 ...

  2. c++ sizeof的实现

    c++中的sizeof,可以通过以下宏定义实现. #include <stdio.h> #define sizeof_T(T) ((size_t)((T*)0+1)) ///求类型的大小 ...

  3. 优化Nginx并发访问量

    通过修改Nginx配置文件,优化linux内核参数,实现高并发测试. 1.优化前使用ab高并发测试 [root@proxy ~]# ab -n 2000 -c 2000 http://192.168. ...

  4. [python 学习] 使用 xml.etree.ElementTree 模块处理 XML

    ---恢复内容开始--- 导入数据(读文件和读字符串) 本地文件 country_data.xml <?xml version="1.0"?> <data> ...

  5. luogu2046 海拔

    题目链接[NOI2010]海拔 首先有个性质就是海拔只会有\(0\)和\(1\)两种. 证明:海拔下降和人数乘积为总消耗,确定了海拔下降总数,如果有个地方可以使得单位消耗最小,那么全部消耗不会更劣. ...

  6. ES集群health为yellow解决办法

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11766147.html Logstash导入数据后,Cerebro显示集群health的状态为yell ...

  7. Delphi GridPanel Percent百分比设置

    可能很多人都有这个困扰,为什么每次设置一个百分比后,值都会改变,只有设置成absolute​才会正常,经摸索发现,是因为精度引起,设置percent的时候,需要将精确到多个小数位.如要有3列,需要设置 ...

  8. python学习笔记(九)内置函数

    print(all([1,2,3,4]))#判断可迭代的对象里面的值是否都为真 True print(any([0,1,2,3,4]))#判断可迭代的对象里面的值是否有一个为真 True print( ...

  9. Dubbo学习-4-dubbo简单案例-2-服务提供者和消费者配置

    在上一篇帖子的基础上,开始使用dubbo来实现RPC调用: 根据dubbo的架构图可知,需要做以下几件事情: 1.将服务提供者注册到注册中心(暴露服务) (1)引入dubbo依赖, 这里依赖2.6.2 ...

  10. ueditor 复制word里面带图文的文章,图片可以直接显示

    图片的复制无非有两种方法,一种是图片直接上传到服务器,另外一种转换成二进制流的base64码 目前限chrome浏览器使用,但是项目要求需要支持所有的浏览器,包括Windows和macOS系统.没有办 ...