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

2023-11-10

一、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

② 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

注:在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');

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'

②updateByPrimaryKeySelective()

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

③ 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.删除数据

①deleteByPrimaryKey()

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

②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'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MyBatis的Mapper接口以及Example的实例函数及详解 的相关文章

随机推荐

  • Windows安装Anaconda,创建pytorch环境,pycharm配置环境

    目录 1 简介 2 安装Anaconda 3 创建一个独立的环境 4 安装依赖的库 5 安装pytorch 6 pycharm中使用conda环境 7 到这里安装就结束了 希望对您有所帮助 如有什么错误请指正 1 简介 安装Anaconda
  • 前端开发中一些常用的正则表达式、手机号验证、身份证号码验证、邮箱验证等

    前端开发中一些常用的正则表达式 手机号验证 身份证号码验证 邮箱验证等 一 常用的五个 1 验证手机号码 var phoneReg 1 d 10 0 9 d 7 if phoneReg test phoneVal alert 手机号码格式错
  • 利用ROS采集VLP-16激光雷达数据

    入手VLP 16激光雷达 想用ROS采集雷达数据 按照现有教程总有些小问题 现在把自己成功采集到数据的经过分享一下 希望能对刚入坑的有所帮助 本人使用的Ubuntu16 04 kinetic系统 1 安装驱动 sudo apt get in
  • kube-prometheus 系列2 初始配置

    kube prometheus 安装完后每个组件会都有默认配置 但是如果要满足基本的生产可用 默认配置是不够的 如数据持久化存储等 这篇文章介绍一些常见的需要初始化的配置 prometheus相关配置 kubectl n monitorin
  • 【FreerRTOS按键队列的实现】

    在Freertos中实现单按键和多按键的的读取 1 单按键 include FreeRTOS h include task h include queue h include stm32f4xx hal h define KEY DEBOU
  • 刷脸支付驱动新零售构建全新的消费场景

    毋庸置疑 刷脸支付是一场属于商家与用户的双赢技术 逐渐成为零售行业新的增长点 不但改变着人们的生活方式 支付也将更方便 更快捷 更安全 同时还能为商家带来更多的便利 较大地降低了人工和管理成本 除此之外 3D视觉作为AI感知核心技术 还可以
  • 矩阵图有何用处?XMind完美展示多对多对象间的关系!

    矩阵图是思维导图中运用非常广泛的图形 通俗来说 它其实就是一个表格 但却能非常紧凑的展现出对象之间的多对多关系 它提供的维度至少比其他树形图多一个维度 矩阵图 下图称为MD 表示多对多关系 但是 如何使MD与XMind中的其他图表区别开来
  • 【git】git报错:git checkout xxx error: The following untracked working tree files would be overwritten b

    git报错 git checkout xxx error The following untracked working tree files would be overwritten by checkout README md Pleas
  • SpringBoot实现导入功能

    实体类 gt MeetRestaurantArrange package com krt meet entity import com baomidou mybatisplus annotation TableName import com
  • 小米手机如何安装fiddler证书

    在手机浏览器输入ip port 1 找到设置 2 更多设置 3 系统安全 4 从存储设备安装 以上问题可以解决在fiddler抓包https的问题
  • python 类的__str__方法

    转载自文章 str 方法 当使用print输出对象的时候 只要类中自己定义了 str self 方法 那么就会打印从在这个方法中return的数据 例如 class Cat 定义一个猫类 def init self new name new
  • OpenCV移植到ARM全过程-III

    gt 目录 在上一篇文章里面我们已经交叉编译好了opencv的第三方依赖库 并且解压好了opencv源码 现在开始正式的交叉编译opencv源码 进入opencv源码目录下 上一篇在源码的的根目录下建立了2个文件夹build和output
  • 基于Spring-Data-Elasticsearch 优雅的实现 多字段搜索 + 高亮 + 分页 + 数据同步✨

    持续创作 加速成长 这是我参与 掘金日新计划 10 月更文挑战 的第17天 点击查看活动详情 系列说明 本系列文章基于我的开源微服务项目 校园博客 进行分析和讲解 所有源码均可在GitHub仓库上找到 系列文章地址请见我的 校园博客专栏 G
  • LinearEyeDepth 定义

    UnityCG cginc中原函数如下 Z buffer to linear 0 1 depth 0 at eye 1 at far plane inline float Linear01Depth float z return 1 0 Z
  • 重学STM32---(六)DAC+DMA+TIM

    这两天复习了DAC DMA再加上把基本定时器TIM6和TIM7看了一下 打算写一个综合点的程序 就在网上找了一些关于DAC DMA和定时器相关的程序 最终打算写了输出正弦波的程序 由于没有示波器 也就不能显示出效果了 本来是打算用软件调试看
  • linux获取ipv6公网ip,Linux 获取IPv6网关

    基于hisi3536实现的 ubuntu下只要找到对应的配置文件 ipv6 route 即可 include include include include include include include include include i
  • #ifdef #if defined #ifndef和#if !defined区别 详解-覆盖所有说明

    首先 让我们先从头文件开始 在很多头文件里 我们会看到这样的语句 ifndef MYHEADFILE H define MYHEADFILE H 语句 endif MYHEADFILE H 为了避免同一个文件被include多次 我们常使用
  • 浅谈构建iOS一个动态化页面的思路

    随着产品的不断迭代 功能的不断完善 我们的项目的中会给用户分成区域呈现出越来越多的东西 咕咚的精选给用户一种信息广场的概念 让用户可以快速的抵达我们感兴趣的点 既然如此 那么每一个项目的综合信息的页面经常会被改动 出现位置的调整 出现新的模
  • STM32中断与事件的理解

    推荐文档 事件与中断区别 目录 事件与中断区别 举例 膝跳反射 人们看见火灾之后打119 事件与中断区别 很多时候 我们经常使用到中断 但是STM32还有一个东西叫做事件 那么这个事件是什么呢 看了上面这个文档我们知道 1 中断是需要CPU
  • MyBatis的Mapper接口以及Example的实例函数及详解

    一 mapper接口中的方法解析 mapper接口中的函数及方法 方法 功能说明 int countByExample UserExample example thorws SQLException 按条件计数 int deleteByPr