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

mysql在系统中常用的查询案例 java项目中常用sql语句 阅读详情

一、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'
mybatis中关于example类详解 一、什么是example类  mybatis-generator会为每个字段产生如上的Criterion,如果表的字段比较多,产生的Example类会十分庞大。理论上通过example类可以构造你想到的任何筛选条件。在mybatis-generator中加以配置,配置数据表的生成操作就可以自动生成example了。  下面是mybatis自动生成example的使用。 二、mapper接口中的方法解析 public interface AdminuserMapper { // 按条件计数 i 阅读详情

相关推荐

Mybatis课堂笔记

Mybatis 环境: JDK1.8 Mysql 5.7 maven 3.6.1 IDEA SSM框架:配置文件的。 最好的方式:看官网文档; 1、简介 1.1、什么是Mybatis MyBatis 是一款优秀的持久层框架 它支持定制化 SQL、存储过程以及高级映射。 MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。 MyBatis 可以使用简单的 XML 或...

qq_40318250的博客 1204

Mybatis中Example的用法

文章目录Example简单介绍Example具体用法1.Example类的生成2.Example类的具体用法Examle在基础中心的应用实例Examle其他用法补充 Example简单介绍 1.example是Mybatis数据层框架中的一个工具,可以帮我们完成sql语句中where条件句的书写,相当于where后面的部分,我们可以根据不同的条件来查询和操作数据库,简化书写sql的过程。 2.用M...

snow0617的博客 1万+

Grey Hack v0.7.3618 保姆级教程:手把手教你用metaxploit.so写一个自动提权脚本

本文详细介绍了如何在Grey Hack v0.7.3618中使用metaxploit.so库编写自动化提权脚本。从环境准备到核心功能解析,再到漏洞利用链构建和权限提升的智能处理,手把手教你掌握游戏中的高级黑客技巧。通过多库联合利用和错误处理优化,提升脚本的效率和成功率。

weixin_42537919的博客 397

Mybatis之Mapper接口及Example实例函数使用详解

  声明:本文章部分内容源自于优快云博主biandous的博客文章,在其基础上进行了部分修正和代码修改。 一、Mapper接口方法 方法 功能说明 int countByExample(UserExample example) throws SQLException 按条件计数 int deleteByPrimaryKey(Integer id) throws SQLE...

Calvin的博客 1356

具身智能中的运动控制如何入门?

本文提出计算机视觉(CV)与强化学习(RL)结合的机器人运动控制学习路径。强调工程实践导向,建议采用"算法-机器人-工程"三阶段学习法:基础阶段掌握RL核心算法(PPO/SAC)和机器人动力学;进阶阶段聚焦Sim2Real技术(域随机化、模仿学习)和仿真环境搭建;深耕阶段结合CV优势开发视觉闭环控制等创新方向。特别指出应避免纯理论学习,提倡"最小知识原则"的实践模式,通过具体项目(如四足机器人控制)串联知识体系。推荐以《现代机器人学》等工具书为参考,配合开源框架(Pi

CyberSoma的博客 324

粒子滤波在基于误差状态量的INS/GNSS组合导航中的使用技巧-On Particle Filter based Error-State INS/GNSS Intergrated Navigation

本文面向以15维惯导(INS)误差为状态量,3维位置误差为量测量的INS/GNSS组合导航系统,​在西工大严恭敏老师的开源工具箱中PF相关库函数基础上实现基于粒子滤波(PF)的惯导误差状态量的递推估计,设计了粒子重置环节并强调了其在本特定场景下的重要性。

I33D33I的博客 1744
上一篇: Java开发中的23种设计模式详解
下一篇: 关于 java.lang.IllegalArgumentException:The valid characters are defined in RFC 7230 and RFC 39错误的解决方法
RookieNote
博客等级 码龄10年 2粉丝 3原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值