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'
MyBatis 通用Mapper接口 Example的实例的更多相关文章
- spring boot集成MyBatis 通用Mapper 使用总结
spring boot集成MyBatis 通用Mapper 使用总结 2019年 参考资料: Spring boot集成 MyBatis 通用Mapper SpringBoot框架之通用mapper插 ...
- springboot学习笔记:8. springboot+druid+mysql+mybatis+通用mapper+pagehelper+mybatis-generator+freemarker+layui
前言: 开发环境:IDEA+jdk1.8+windows10 目标:使用springboot整合druid数据源+mysql+mybatis+通用mapper插件+pagehelper插件+mybat ...
- springboot学习笔记:9.springboot+mybatis+通用mapper+多数据源
本文承接上一篇文章:springboot学习笔记:8. springboot+druid+mysql+mybatis+通用mapper+pagehelper+mybatis-generator+fre ...
- mybatis通用mapper的使用
项目中持久层封装了两套,一个hibernate,一个是mybatis.hibernate中封装了一些通用的方法,但是mybatis中没有,基于这个需求开始使用mybatis的通用mapper. ...
- Mybatis的Mapper接口方法不能重载
今天给项目的数据字典查询添加通用方法,发现里边已经有了一个查询所有数据字典的方法 List<Dict> selectDictList(); 但我想设置的方法是根据数据字典的code查询出所 ...
- SpringBoot 3.SpringBoot 整合 MyBatis 逆向工程以及 MyBatis 通用 Mapper
一.添加所需依赖,当前完整的pom文件如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&qu ...
- Spring boot集成 MyBatis 通用Mapper
配置 POM文件 <parent> <groupId>org.springframework.boot</groupId> <artifactId>sp ...
- 5.7 Liquibase:与具体数据库独立的追踪、管理和应用数据库Scheme变化的工具。-mybatis-generator将数据库表反向生成对应的实体类及基于mybatis的mapper接口和xml映射文件(类似代码生成器)
一. liquibase 使用说明 功能概述:通过xml文件规范化维护数据库表结构及初始化数据. 1.配置不同环境下的数据库信息 (1)创建不同环境的数据库. (2)在resource/liquiba ...
- springboot 使用mybatis 通用Mapper,pagehelper
首先需要maven导入需要的包,这里用的是sqlserver,druid,jtds连接数据库 <dependency> <groupId>com.alibaba</gro ...
随机推荐
- 【转】C# 根据当前时间获取,本周,本月,本季度等时间段 .Net中Exception
1 DateTime dt = DateTime.Now; //当前时间 2 3 DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayO ...
- 1115 Counting Nodes in a BST (30 分)
1115 Counting Nodes in a BST (30 分) A Binary Search Tree (BST) is recursively defined as a binary tr ...
- python中将HTTP头部中的GMT时间转换成datetime时间格式
原文: https://blog.csdn.net/zoulonglong/article/details/80585716 需求背景:目前在做接口的自动化测试平台,由于接口用例执行后返回的结果中的时 ...
- [UE4]运行时创建Actor
- [UE4]C++中引用(&)的用法和应用实例
对于习惯使用C进行开发的朋友们,在看到c++中出现的&符号,可能会犯迷糊,因为在C语言中这个符号表示了取地址符,但是在C++中它却有着不同的用途,掌握C++的&符号,是提高代码执行效率 ...
- 解决provisional headers are shown的过程
前言 学习Angular时写了一个音乐播放器oschina地址github地址为了兼容android chrome,参考了这篇文章; 在数据读取中要中断的时候,可以把audio.src设为null,并 ...
- (转)linux查找技巧: find grep xargs
在当前目录下所有.cpp文件中查找efg函数 find . -name "*.cpp" | xargs grep 'efg' xargs展开find获得的结果,使其作为grep的参 ...
- 自己写的jQuery颜色插件
界面效果: 插件js代码: ;(function ($) { //122种颜色 var aColors = [ "ff0000", "ffff00", &quo ...
- VS自动编译脚本
rem ************************************************rem * Script to compile the solutions of IdealOE ...
- java编译器
编译: .java变成.class 前端编译 Sun javac Eclipse ECJ .class变成机器码 运行期编译 等HostSpot VM c1,c2 .jav ...