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的实例函数及详解的更多相关文章
- MyBatis的Mapper接口以及Example的实例函数及详解
来源:https://blog.csdn.net/biandous/article/details/65630783 一.mapper接口中的方法解析 mapper接口中的函数及方法 方法 功能说明 ...
- Mybatis-技术专区-Mapper接口以及Example的实例函数及详解
一.mapper接口中的方法解析 mapper接口中的函数及方法 int countByExample(UserExample example) thorws SQLException 按条件 ...
- mybatis中的mapper接口文件以及example类的实例函数以及详解
##Example example = new ##Example(); example.setOrderByClause("字段名 ASC"); //升序排列,desc为降序排列 ...
- Mybatis的逆向工程以及Example的实例函数及详解
Mybatis-generator是Mybatis的逆向工程 (根据数据库中的表生成java代码) Mybatis的逆向工程会生成实例及实例对应的example,example用于添加条件,相当于w ...
- Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring
Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring 非原创[只为记录],原博文地址:https://www.cnblogs.com/ ...
- (转)Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring
Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring Mybatis在与Spring集成的时候可以配置MapperFactoryBea ...
- Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring - 大新博客 - 推酷 - 360安全浏览器 7.1
Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring - 大新博客 时间 2014-02-11 21:08:00 博客园-所有随笔区 ...
- C#获取C# DLL中的指定接口的所有实现实例 - qq_19759475的博客 - CSDN博客
原文:C#获取C# DLL中的指定接口的所有实现实例 - qq_19759475的博客 - CSDN博客 public static List<T> CreateTarInterface& ...
- 2017.2.9 深入浅出MyBatis技术原理与实践-第八章 MyBatis-Spring(二)-----配置文件详解
深入浅出MyBatis技术原理与实践-第八章 MyBatis-Spring(二) ------配置文件详解 8.2 MyBatis-Spring应用 8.2.1 概述 本文主要讲述通过注解配置MyBa ...
随机推荐
- A JAX-WS web service is by itself a Singleton
http://stackoverflow.com/questions/11096310/singleton-object-in-java-web-service http://stackoverflo ...
- 《SQL 进阶教程》 case:在 UPDATE 语句里进行条件分支
1.对当前工资为30万日元以上的员工,降薪10%:2.对当前工资为25万日元以上且不满28万日元的员工,加薪20% update salaries set salary = case when sal ...
- hdu1175-连连看(dfs)
一个一个走,记录方向改变了几次,不能超过两次,两次如果还没到终点return: #include<cstdio> #include<string.h> #define inf ...
- python之is 和 == 的区别//编码和解码
一.is 和 == 的区别: 1 .id() 内存地址 2. == 比较 #比较两边的值 3. is 比较 #比较的是内存地址 数字,字符串,有小数据池 #数字小 ...
- Magic Maze dfs + dp
http://swjtuoj.cn/problem/2387/ 设dp[cur]表示以cur这个节点为起点的时候,能走的最大贡献. 题目保证没环,也就是没回路. 感觉和树dp差不多了. #includ ...
- Java基于springMVC的验证码案例
``` Java验证码案例(基于springMVC方式) 验证码工具类 package com.ekyb.common.util; import java.awt.Color; import java ...
- 《超实用的Node.js代码段》连载二:正确拼接Buffer
对于初学Node.js框架的开发人员来说,可能认为Buffer模块比较易学.重要性也不是那么突出.其实,Buffer模块在文件I/O和网络I/O中应用非常广泛,其处理二进制的性能比普通字符串性能要高出 ...
- Appium基础四:Desired Capabilities详讲
Desired Capabilities在启动session的时候是必须提供的,先看如下代码: Desired Capabilities本质上是key value的对象,他告诉appium serve ...
- Date-DateFormat-Calendar-Math-regex
一.Date类(java.util) 作用:表示时间的类,精确到毫秒,以GMT 1970年1月1日0点0分0秒起算 构造方法: Data() ---获取当前时间 Date(long ...
- C#字段声明部分如何调用该类中的方法进行初始化?
问题描述: 有时,功能需求,需要在初始化字段时,需要视不同情况赋予不同字段值. 解决办法: 将方法设为static即可. e.g. public string str = SetStr(); publ ...