1. 程式人生 > >mybatis mapper介面以及example用法

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
  • 1

② 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

注:在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("[email protected]");
XxxMapper.insert(user);
//相當於:insert into user(ID,username,password,email) values ('dsfgsdfgdsfgds','admin','admin','[email protected]');
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.更新資料

①updateByPrimaryKey()

User user =new User();
user.setId("dsfgsdfgdsfgds");
user.setUsername("wyw");
user.setPassword("wyw");
user.setEmail("[email protected]");
XxxMapper.updateByPrimaryKey(user);
//相當於:update user set username='wyw', password='wyw', email='[email protected]' where id='dsfgsdfgdsfgds'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

②updateByPrimaryKeySelective()

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

③ 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'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

updateByExample()更新所有的欄位,包括欄位為null的也更新,建議使用 updateByExampleSelective()更新想更新的欄位

4.刪除資料

①deleteByPrimaryKey()

XxxMapper.deleteByPrimaryKey(1);  //相當於:delete from user where id=1
  • 1

②deleteByExample()

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

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用法

一、mapper介面中的方法解析mapper介面中的函式及方法方法功能說明int countByExample(UserExample example) thorws SQLException按條件計數int deleteByPrimaryKey(Integer id) th

MyBatis中通用Mapper介面以及Example的方法解析

一、通用Mapper中的方法解析 方法 功能說明 int countByExample(UserExample example) thorws SQLException 按條件計數

MyBatisMapper介面以及Example的例項函式及詳解

一、mapper介面中的方法解析mapper介面中的函式及方法方法功能說明int countByExample(UserExample example) thorws SQLException按條件計數int deleteByPrimaryKey(Integer id) th

MybatisMapper介面Example例項函式使用詳解

宣告:本文章部分內容源自於CSDN博主biandous的部落格文章,在其基礎上進行了部分修正和程式碼修改。 一、Mapper介面方法 方法 功能說明 int countByExample(UserExample example) throws SQLExce

MyBatis逆向工程-根據資料庫表自動生成bean、mapper介面以及對映檔案

說明:偶然看到一個視訊,講到了使用mybatis的逆向工程實現自動生成程式碼的部分(根據資料表生成相應的實體類、對映檔案、介面),因為之前沒有學習過這類東西,今天照著弄了下,然後自己寫了一個測試案例,特來記錄。。。。==============================

springboot之通用mapper擴充套件通用介面Example 用法

專案中提供了大量現成的方法,這些方法可以作為擴充套件時的參考。例如 selectAll 方法。首先定義介面:@RegisterMapperpublic interface SelectAllMapper<T> {    /**     * 查詢全部結果     *

利用mybatis generator生成實體類、Mapper介面以及對應的XML檔案

專案中通常會遇到資料的持久化,如果是採用mybatis的orm,就會涉及到生成xml的問題,剛好mybatis官網提供了這麼個外掛

解決Mybatis @Mapper 介面名字衝突導致springboot程式啟動不起來的問題

有兩個同名的Mapper: package com.clear.ims4.business.material.program.layout; @Mapper public interface LayoutRepository { }   package com.clear

mybatis mapper介面中使用@select 註解 @select中使用模糊查詢like

不使用concat會出現 Parameter index out of range (1 > number of parameters, which is 0)這個錯誤 正確示例:

myBatis mapper介面方法過載問題

在mybatis框架中,寫dao層的mapper介面時,是不可以進行方法的過載的,下面是截圖證明: 當mapper介面中有方法的過載時,會出現異常: 這是mapper介面中定義的兩個方法,進行過載:

Mybatis mapper介面與xml檔案路徑分離

為什麼分離   對於Maven專案,IntelliJ IDEA預設是不處理src/main/java中的非java檔

mybatis自動生成entity層和dao層中Mapper介面中的各個方法的意義及example實體類的用法

package cn.lichenyang.emall.dao; import cn.lichenyang.emall.entity.TbContent; import cn.lichenyang.emall.entity.TbContentExample; import

mybatis中的mapper介面檔案以及example類的例項函式以及詳解

##Example example = new ##Example(); example.setOrderByClause("欄位名 ASC"); //升序排列,desc為降序排列。 example.setDistinct(false)//去

mybatis逆向工程的Example用法==筆記==【單表操作只需呼叫,多表查詢需要自定義sql+mapper介面方法(待補全)】

======上程式碼: ===版本1: @Service public class BaseDictServiceImpl implements BaseDictService { //查詢資料字典表,注入資料字典表mapper介面代理物件 @Autowired

mybatis中的mapper接口文件以及example類的實例函數以及詳解

lean boolean sql 語句 sql amp keys value per lec ##Example example = new ##Example(); example.setOrderByClause("字段名 ASC"); //升序排列,de

MyBatisMapper接口以及Example的實例函數及詳解

記錄 sele record esc BE tor ins -a IT 來源:https://blog.csdn.net/biandous/article/details/65630783 一、mapper接口中的方法解析 mapper接口中的函數及方法 方法功能說明

使用mybatis自帶工具,自動生成表對應domain、mapper.xml以及dao

upd stat javac sch val char 數據庫名 tab decimal 引用:http://blog.csdn.net/p793049488/article/details/40422975 1、需要手動建表,如T_PRI_USER 2、新建配置文件g

Mybatis Generator生成Mybatis Dao接口層*Mapper.xml以及對應實體類

utf property -c 鏈接 dbcc span count com 架構 http://www.cnblogs.com/qixiaoyizhan/p/7597315.html 【前言】   使用Mybatis-Generator自動生成Dao、Model、Ma

JavaEE MyBatis與Spring的整合——基於mapper介面方式開發(教材學習筆記)

在MyBatis與Spring的整合開發中雖然可以通過傳統的DAO開發方式,但是採用DAO方式會產生大量的重複程式碼,因此學習另外一種程式設計方式就很重要了,即Mapper介面程式設計(本章程式碼是基於上一篇部落格的點這裡) 一、基於MapperFactoryBean的整合 Mapper

mybatis 詳解(六)------通過mapper介面載入對映檔案

目錄 1、定義 userMapper 介面 2、在全域性配置檔案 mybatis-configuration.xml 檔案中載入 UserMapper 介面(單個載入對映檔案) 3、編寫UserMapper.xml 檔案 4、測試 5、批量載入對映檔案 6、注意