一、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()

  1.   User user = XxxMapper.selectByPrimaryKey(100); //相当于select * from user where id = 100 

② selectByExample() 和 selectByExampleWithBLOGs()

  1. UserExample example = new UserExample();
  2. Criteria criteria = example.createCriteria();
  3. criteria.andUsernameEqualTo("wyw");
  4. criteria.andUsernameIsNull();
  5. example.setOrderByClause("username asc,email desc");
  6. List<?>list = XxxMapper.selectByExample(example);
  7. //相当于:select * from user where username = 'wyw' and username is null order by username asc,email desc
  1.  

注:在iBator逆向工程生成的文件XxxExample.java中包含一个static的内部类Criteria,Criteria中的方法是定义SQL 语句where后的查询条件。

2.插入数据

①insert()

  1. User user = new User();
  2. user.setId("dsfgsdfgdsfgds");
  3. user.setUsername("admin");
  4. user.setPassword("admin")
  5. user.setEmail("wyw@163.com");
  6. XxxMapper.insert(user);
  7. //相当于:insert into user(ID,username,password,email) values ('dsfgsdfgdsfgds','admin','admin','wyw@126.com');

3.更新数据

①updateByPrimaryKey()

  1. User user =new User();
  2. user.setId("dsfgsdfgdsfgds");
  3. user.setUsername("wyw");
  4. user.setPassword("wyw");
  5. user.setEmail("wyw@163.com");
  6. XxxMapper.updateByPrimaryKey(user);
  7. //相当于:update user set username='wyw', password='wyw', email='wyw@163.com' where id='dsfgsdfgdsfgds'

②updateByPrimaryKeySelective()

  1. User user = new User();
  2. user.setId("dsfgsdfgdsfgds");
  3. user.setPassword("wyw");
  4. XxxMapper.updateByPrimaryKey(user);
  5. //相当于:update user set password='wyw' where id='dsfgsdfgdsfgds'

③ updateByExample() 和 updateByExampleSelective()

  1. UserExample example = new UserExample();
  2. Criteria criteria = example.createCriteria();
  3. criteria.andUsernameEqualTo("admin");
  4. User user = new User();
  5. user.setPassword("wyw");
  6. XxxMapper.updateByPrimaryKeySelective(user,example);
  7. //相当于:update user set password='wyw' where username='admin'

updateByExample()更新所有的字段,包括字段为null的也更新,建议使用 updateByExampleSelective()更新想更新的字段

4.删除数据

①deleteByPrimaryKey()

  1.   XxxMapper.deleteByPrimaryKey(1); //相当于:delete from user where id=1 

②deleteByExample()

  1. UserExample example = new UserExample();
  2. Criteria criteria = example.createCriteria();
  3. criteria.andUsernameEqualTo("admin");
  4. XxxMapper.deleteByExample(example);
  5. //相当于:delete from user where username='admin'

5.查询数据数量

①countByExample()

  1. UserExample example = new UserExample();
  2. Criteria criteria = example.createCriteria();
  3. criteria.andUsernameEqualTo("wyw");
  4. int count = XxxMapper.countByExample(example);
  5. //相当于:select count(*) from user where username='wyw'

MyBatis逆向工程中的Mapper接口以及Example的实例函数及详解的更多相关文章

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

    来源:https://blog.csdn.net/biandous/article/details/65630783 一.mapper接口中的方法解析 mapper接口中的函数及方法 方法 功能说明 ...

  2. Mybatis-技术专区-Mapper接口以及Example的实例函数及详解

    一.mapper接口中的方法解析 mapper接口中的函数及方法 int countByExample(UserExample example) thorws SQLException     按条件 ...

  3. mybatis中的mapper接口文件以及example类的实例函数以及详解

    ##Example example = new ##Example(); example.setOrderByClause("字段名 ASC"); //升序排列,desc为降序排列 ...

  4. Mybatis的逆向工程以及Example的实例函数及详解

    Mybatis-generator是Mybatis的逆向工程  (根据数据库中的表生成java代码) Mybatis的逆向工程会生成实例及实例对应的example,example用于添加条件,相当于w ...

  5. Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring

    Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring 非原创[只为记录],原博文地址:https://www.cnblogs.com/ ...

  6. (转)Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring

    Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring Mybatis在与Spring集成的时候可以配置MapperFactoryBea ...

  7. Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring - 大新博客 - 推酷 - 360安全浏览器 7.1

    Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring - 大新博客 时间 2014-02-11 21:08:00  博客园-所有随笔区 ...

  8. C#获取C# DLL中的指定接口的所有实现实例 - qq_19759475的博客 - CSDN博客

    原文:C#获取C# DLL中的指定接口的所有实现实例 - qq_19759475的博客 - CSDN博客 public static List<T> CreateTarInterface& ...

  9. 2017.2.9 深入浅出MyBatis技术原理与实践-第八章 MyBatis-Spring(二)-----配置文件详解

    深入浅出MyBatis技术原理与实践-第八章 MyBatis-Spring(二) ------配置文件详解 8.2 MyBatis-Spring应用 8.2.1 概述 本文主要讲述通过注解配置MyBa ...

随机推荐

  1. A JAX-WS web service is by itself a Singleton

    http://stackoverflow.com/questions/11096310/singleton-object-in-java-web-service http://stackoverflo ...

  2. 《SQL 进阶教程》 case:在 UPDATE 语句里进行条件分支

    1.对当前工资为30万日元以上的员工,降薪10%:2.对当前工资为25万日元以上且不满28万日元的员工,加薪20% update salaries set salary = case when sal ...

  3. hdu1175-连连看(dfs)

    一个一个走,记录方向改变了几次,不能超过两次,两次如果还没到终点return: #include<cstdio> #include<string.h> #define inf ...

  4. python之is 和 == 的区别//编码和解码

    一.is  和  ==  的区别: 1  .id()   内存地址 2.  ==   比较    #比较两边的值 3.   is    比较   #比较的是内存地址 数字,字符串,有小数据池 #数字小 ...

  5. Magic Maze dfs + dp

    http://swjtuoj.cn/problem/2387/ 设dp[cur]表示以cur这个节点为起点的时候,能走的最大贡献. 题目保证没环,也就是没回路. 感觉和树dp差不多了. #includ ...

  6. Java基于springMVC的验证码案例

    ``` Java验证码案例(基于springMVC方式) 验证码工具类 package com.ekyb.common.util; import java.awt.Color; import java ...

  7. 《超实用的Node.js代码段》连载二:正确拼接Buffer

    对于初学Node.js框架的开发人员来说,可能认为Buffer模块比较易学.重要性也不是那么突出.其实,Buffer模块在文件I/O和网络I/O中应用非常广泛,其处理二进制的性能比普通字符串性能要高出 ...

  8. Appium基础四:Desired Capabilities详讲

    Desired Capabilities在启动session的时候是必须提供的,先看如下代码: Desired Capabilities本质上是key value的对象,他告诉appium serve ...

  9. Date-DateFormat-Calendar-Math-regex

    一.Date类(java.util) 作用:表示时间的类,精确到毫秒,以GMT 1970年1月1日0点0分0秒起算 构造方法:     Data() ---获取当前时间      Date(long ...

  10. C#字段声明部分如何调用该类中的方法进行初始化?

    问题描述: 有时,功能需求,需要在初始化字段时,需要视不同情况赋予不同字段值. 解决办法: 将方法设为static即可. e.g. public string str = SetStr(); publ ...