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

② 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'


--------------------- 

转自:https://blog.csdn.net/biandous/article/details/65630783 
 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/447840.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

《学习R》笔记:科学计算器、检查变量和工作区、向量、矩阵和数组、列表和数据框...

一、第二章 科学计算器 要检查两个数字是否一样&#xff0c;要使用 all.equal() ,不要使用 &#xff0c; 符号仅用于比较两个整型数是否存在相同 。 > all.equal(sqrt(2)^2,2) [1] TRUE > all.equal(sqrt(2) ^ 2,3) [1] "Mean relative difference: 0.5" >…

VUE 解决:Method “xxx“ has already been defined as a data property.

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 1.调用方法报错&#xff1a; Method "changePage" has already been defined as a data property. 代码写法&#xff1a; 2…

尽早理财的8个思维

理财并不单纯的是指通过技巧加经验去让自己手里的钱变成更多的钱&#xff0c;还包括一种思维方式。一个真正的理财高手&#xff0c;思维模式和他人不同。这些人好像天生为了赚钱而生&#xff0c;但我们在羡慕的同时可以将他们的思维拿来借鉴&#xff0c;培养适合自己的理财思维…

解决:java.lang.NoSuchMethodException: gentle.entity.User.<init>()

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 1. 运行程序报错&#xff1a; java.lang.NoSuchMethodException: gentle.entity.User.<init>() 2. 原因和解决&#xff1a; 我…

【转】R函数-diag()函数

版权声明&#xff1a;本文为博主原创文章&#xff0c;遵循 CC 4.0 BY-SA 版权协议&#xff0c;转载请附上原文出处链接和本声明。本文链接&#xff1a;https://blog.csdn.net/lili_wuwu/article/details/51909229 diag()提取或替换一个矩阵的对角线,或构造一个对角矩阵。 语法&…

赚钱的8大定律

赚钱一定有方法&#xff0c;每个人有志于创业的人都应该分析一下&#xff0c;什么生意你最适合做&#xff0c;什么事情你最擅长&#xff0c;自然就是你容易收获的。 8条赚钱定律&#xff1a; 赚钱第一定律&#xff1a;你要做羊&#xff0c;还是做狼&#xff1f; 永远是10%的人…

C# 模拟Windows键盘事件

发送键盘消息 1 [DllImport("user32.dll", EntryPoint "keybd_event", SetLastError true)] 2 public static extern void keybd_event( 3 byte bVk, //虚拟键值 4 byte bScan,// 一般为0 5 int…

js 验证用户输入的是否为数字、检查只能为数字

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 方法一 如果判断输入的是否是数字&#xff0c;如果不是数字弹出一个提示&#xff1a;简易代码如下&#xff1a;<body> <input …

修炼你自己

一、保持沉稳&#xff08;1&#xff09;不要随便显露你的情绪。&#xff08;2&#xff09;不要逢人就诉说你的困难和遭遇。&#xff08;3&#xff09;在征询别人的意见之前&#xff0c;自己先思考&#xff0c;但不要先讲。&#xff08;4&#xff09;不要一有机会就唠叨你的不满…

C# 动态加载资源

在xaml中控件通过绑定静态资源StaticResource来获取样式Style有多种方式&#xff0c;TextBlockStyle.xaml是一个ResourceDictionary&#xff0c;包含了所需样式 通过相对路径引用 通过后台代码向当前程序的资源中动态添加&#xff0c;代码如下&#xff1a; 1 ResourceDicti…

中医3个不花钱的养肾方

深呼吸可通过肺肾的相互作用&#xff0c;对肾起到温煦的效果。 肾为先天之本。如果先天不足&#xff0c;后天失养&#xff0c;就会出现肾精亏虚、肾阳虚衰、肾阴亏虚等证。男女老幼都可能肾虚&#xff1a;小儿肾虚&#xff0c;生长发育迟缓&#xff0c;智力低下;中青年肾虚&…

ACM-ICPC 2018 徐州赛区网络预赛 I. query 树状数组

I. query 题目链接&#xff1a; Problem Description Given a permutation \(p\) of length \(n\), you are asked to answer \(m\) queries, each query can be represented as a pair \((l ,r )\), you need to find the number of pair \((i ,j)\) such that \(l \le i <…

内容分发网络(CDN) 是什么

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 技术原理解说见另一文&#xff1a;https://blog.csdn.net/jiangyu1013/article/details/88795690 内容分发网络 &#xff08;英语&…

7种方法让你养出干净的肺

世界卫生组织(WHO)近日公布的全球1081个城市采集的空气质量数据显示&#xff0c;空气中可吸入颗粒物(PM10)含量最少的前50个城市几乎被加拿大和美国包揽。中国北京&#xff0c;兰州等城市都是重灾区。生活在很多大中型城市&#xff0c;除了空气污染外&#xff0c;香烟、油烟、工…

CDN 的作用与基本过程

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 技术原理解说也可参见另一文&#xff1a;https://blog.csdn.net/jiangyu1013/article/details/88795690 1.简介 CDN&#xff0c;Content …

2019南昌网络赛  I. Yukino With Subinterval 树状数组套线段树

I. Yukino With Subinterval题目链接&#xff1a; Problem Descripe Yukino has an array \(a_1, a_2 \cdots a_n\). As a tsundere girl, Yukino is fond of studying subinterval. Today, she gives you four integers $l, r, x, y $, and she is looking for how many diffe…

健康丨汗从哪里出 病从哪里来

1.额头出汗肝阳上亢如果额头常常出很多汗&#xff0c;中医认为可能是肝阳上亢引起的。建议你去医院检查一下甲状腺激素分泌是否正常&#xff0c;因为这很可能是甲状腺激素分泌过剩造成的。  医师建议&#xff1a;平时尽量保持心境平和&#xff0c;少生气&#xff0c;女人尤其…

CDN(内容分发网络)技术原理

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 1. 前言 Internet的高速发展&#xff0c;给人们的工作和生活带来了极大的便利&#xff0c;对Internet的服务品质和访问速度要求越来越高…

3.0 go mod之远程仓库搭建-代码示例

注意事项 所谓的远程仓库指的是github&#xff0c;个人首次使用go mod在其他云仓库上尝试&#xff0c;并未成功&#xff0c;这浪费了我近2小时的时间&#xff1b; 如果你是初次尝试&#xff0c;那么除了github的地址换一下之外&#xff0c;其他的都按照示例操作&#xff0c;比如…

视界云:CDN{内容分发网络} 知识详解

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 CDN 全称:Content Delivery Network或Content Ddistribute Network&#xff0c;即内容分发网络 基本思路&#xff1a; 尽可能避开互联…