首页 > 其他分享 >MyBatis的Example类详解

MyBatis的Example类详解

时间:2022-12-13 08:33:06浏览次数:49  
标签:name id example 详解 user MyBatis Example User

Example类的定义?

第一次幕课网教程看到关于这方面教时,没有懂example起什么用,感觉不用example也可以查询了,后来认真一看才知道这是查询条件生成器

 

 


mybatis-generator会为每个字段产生Criterion,为底层的mapper.xml创建动态sql。如果表的字段比较多,产生的example类会十分庞大。理论上通过example类可以构造你想到的任何筛选条件。在mybatis-generator中加以配置,配置数据表的生成操作就可以自动生成example了。
mybatis 的mapper接⼝提供了增、删、改、查的⽅法。避免过多使⽤xml来直接写sql。

 


实体类 User:
private Integer id;
private String name;
private String age;
private Integer sex;
Example类的使用:
Example examle = new Example(User.class);
example.setOrderByClause("字段名 asc,字段名 desc");
//去除重复,boolean 型,true 为选择不重复的记录。
example.setDistinct(false);
Criteria criteria = new Example().createCriteria();
is null;is not null;
equal to(value);not equal to(value);
GreaterThan(value);GreaterThanOrEqualTo(value);
LessThan(value); LessThanOrEqualTo(value);
in(item,item,item,...);not in(item,item,item,...);
like("%"+value+"%");not like("%"+value+"%");
Between(value1,value2);not between(value1,value2);
Mybatis的实例函数:

 

核心方法,通过mapper . 弹出方法名

 

 

方法详细说明: 

返回值类型 方法 功能说明
int countByExample(UserExample example) throws SQLException 按条件计数
int deleteByPrimaryKey(Integer id) throws SQLException 按主键删除
int deleteByExample(Example example) throws SQLException 按条件删除
String / Integer insert (User user) throws 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 按主键更新,(注意会把数据库中⾮空的字段更新为null)
int updateByPrimaryKeySelective(User record) thorws SQLException 按主键更新值不为null的字段
int updateByExample(User record, UserExample example) thorws SQLException 按条件更新
int updateByExampleSelective(User record, UserExample example) thorws SQLException 按条件更新值不为null的字段

 

 

以下为示例:

实例方法详解:

1.按条件统计:

Example example = new Example(User.class);
//Criteria criteria = example.createCriteria();

example.createCriteria().andEqualTo("id", "1001" )

UserMapper.countByExample(example);
//等同于:select count(*) from user where id='1001'
2.查询:

(1)主键查询:selectByPrimaryKey

User user = UserMapper.selectByPrimaryKey("1001");
//等同于:select * from user where id = "1001"
(2)条件查询:selectByExample (and条件)

Example example = new Example(User.class);
example.createCriteria().andEqualTo("id", "1001" )
.andEqualTo("name", "小杨");
User user = UserMapper.selectByExample(example);

//等同于:select * from user where id = "1001" and name = '小杨'
selectByExample (or条件)

Example example = new Example(User.class);
example.or.andEqualTo("id", "1001" )
example.or.andEqualTo("name", "小杨");
User user = UserMapper.selectByExample(example);

//等同于:select * from user where id = "1001" or name = '小杨'
selectByExample (and+or多条件查询)

Example example = new Example(User.class);
example.createCriteria().andEqualTo("id", "1001" )
.andEqualTo("name", "小杨");
example.and().orEqualTo("age","18")
.orEqualTo("sex","1");
List<User> user = UserMapper.selectByExample(example);

//等同于:
SELECT id,user_id,user_name,pass_word FROM user WHERE ( id = "1001" and name= "小杨" ) and ( age= "18" or sex= "1" )
3.插入:

User user = new User();
user.setId("1002");
user.setAge("18");
user.setName("小王");
user.setSex("0")
UserMapper.insert(user);
//等同于:
insert into user(id,age,name,sex) values ('1002','18','小王','0');
4.更新:
(1)updateByPrimaryKeyimaryKey 按主键更新(会把没有设置的值设置为空)

User user = new User();
user.setId("1002");
user.setName("小王");
user.setAge("18");
user.setSex("0")
UserMapper.updateByPrimaryKey(user);
//等同于:update user set sex= "0" name='小王', age='18' where id='1002'
(2)updateByExampleSelective 按主键更新(不会把null 更新)

/*注意:updateByExample()更新所有的字段,包括字段为null的,建议使用 updateByExampleSelective()更新需要更新的字段*/

Example example = new Example(User.class);
example.createCriteria().andEqualTo("id","1002");

User user = new User();
user.setName("小王");

UserMapper.updateByExampleSelective(user,example);

//等同于:update user set Name='小王' where id='1002'
5.删除:
(1)deleteByPrimaryKey 按主键删除

UserMapper.deleteByPrimaryKey("1002");
//相当于:delete from user where id="1002"
(2)deleteByExample 按条件删除

Example example = new Example(User.class);
example.createCriteria().andEqualTo("name","小王");
UserMapper.deleteByExample(example);
//等同于:delete from user where name ='小王'
————————————————
版权声明:本文为CSDN博主「leaf__yang」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/leaf__yang/article/details/125273086

标签:name,id,example,详解,user,MyBatis,Example,User
From: https://www.cnblogs.com/fgxwan/p/16977628.html

相关文章

  • webpack优化之代码分割与公共代码提取详解
    前言开发多页应用的时候,如果不对webpack打包进行优化,当某个模块被多个入口模块引用时,它就会被打包多次(在最终打包出来的某几个文件里,它们都会有一份相同的代码)。当项目业......
  • webpack学习笔记之代码分割和按需加载的实例详解
    主要介绍了webpack学习笔记之代码分割和按需加载的实例详解,具有一定的参考价值,有兴趣的可以了解一下为什么需要代码分割和按需加载代码分割就是我们根据实际业务需求将代......
  • django-路由层详解
    django-路由层详解可视化界面之数据增删改查针对数据对象主键字段的获取可以使用更加方便的obj.pk获取在模型类中定义双下str方法可以在数据对象被执行打印操作的时候......
  • GLES3.0中文API-glGetActiveUniform详解
    前言     glGetActiveUniform()函数看了官方的解释,也看了别人的一些帖子,但是基本上都没有实际的代码,没有实操总觉得隔了一层,雾里看花理解不了。下面亲自写段代码验......
  • RFO SIG:openEuler AWS AMI 制作详解
    作者简介王瀚兴,SUSE软件工程师,主要负责Rancher产品线相关的研发工作。欧拉开源社区的RFOSIG正在努力将openEuler与Rancher整合,以推动社区的云原生版图发展。而op......
  • C语言操作符详解
    操作符分类:算术操作符移位操作符位操作符赋值操作符单目操作符关系操作符逻辑操作符条件操作符逗号表达式下标引用,函数调用和结构成员算术操作符(+-*/%)1.除了%操作符......
  • mybatisplus 注入和更新问题
    1、在接口中如果没有存在事务进行插入和更新操作,会存在一个问题,就是可能导致结果异步,出现脏读的问题;比如,针对A表进行插入和更新操作,然后下一步是进行查询操作,当数据量比较......
  • 《Java并发编程详解》读书笔记
    嗯,书是假期开始看的感觉,虽然部分知识以前学过,但是在学一次巩固一下。嗯,加油生活。摘一句子,^_^有一天,突然发现自己没有热爱的东西了。就这样进入浪费时间的年代。秋天一到,候......
  • mybatis动态插入
    当用户注册信息的时候,总是有些信息不需要填,有些必须填,mybatis的动态插入可以只需写入用户插入的信息//动态插入publicStringinsertUser(Useruser){return......
  • JS逆向之浏览器补环境详解
    JS逆向之浏览器补环境详解“补浏览器环境”是JS逆向者升职加薪的必备技能,也是工作中不可避免的操作。为了让大家彻底搞懂“补浏览器环境”的缘由及原理,本文将从以下四个......