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

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

package cn.lichenyang.emall.dao;

import cn.lichenyang.emall.entity.TbContent;
import cn.lichenyang.emall.entity.TbContentExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface TbContentMapper {
	
    //根據條件計算總數,example用來設定當前物件的條件
    int countByExample(TbContentExample example);
    
    //根據條件刪除,example用來設定當前物件的條件
    int deleteByExample(TbContentExample example);

    //根據主鍵刪除,example用來設定當前物件的條件
    int deleteByPrimaryKey(Long id);

    //插入資料(如果當前物件沒有值,也會被插入到資料庫中)
    int insert(TbContent record);
    
    //插入資料,如果當前物件的屬性為null,則當前屬性不會插入資料庫,只向資料庫插入有值的屬性
    int insertSelective(TbContent record);

    //按條件查詢(包括BLOB欄位)。只有當資料表中的欄位型別有為二進位制的才會產生。
    List<TbContent> selectByExampleWithBLOBs(TbContentExample example);

    //根據條件查詢,example就是TbContent的條件
    List<TbContent> selectByExample(TbContentExample example);

    //根據主鍵查詢
    TbContent selectByPrimaryKey(Long id);
    
    //按條件更新值不為null的欄位 			解釋:前臺傳入那個值就修改那個值,example用於新增條件,相當於是where後面的內容			
    int updateByExampleSelective(@Param("record") TbContent record, @Param("example") TbContentExample example);
    
    //按條件進行跟新		 (包括BLOB欄位)。只有當資料表中的欄位型別有為二進位制的才會產生。
    int updateByExampleWithBLOBs(@Param("record") TbContent record, @Param("example") TbContentExample example);

    //根據條件進行更新,所有帶有example的修改,左邊只用設定從介面傳過來的值,右邊設定的是傳過來的物件的條件
    int updateByExample(@Param("record") TbContent record, @Param("example") TbContentExample example);

    //根據主鍵更新當前物件的屬性的值不為null的屬性,如果為null,就不進行修改
    int updateByPrimaryKeySelective(TbContent record);

    //根據主鍵進行更新,(包括BLOB欄位)。只有當資料表中的欄位型別有為二進位制的才會產生。
    int updateByPrimaryKeyWithBLOBs(TbContent record);

    //根據主鍵進行修改
    int updateByPrimaryKey(TbContent record);
}

xxxexample的用法:

xxxExample example = new xxxExample();  
Criteria criteria = example.createCriteria();  
方法說明:  
// 1.添加升序排列條件,DESC為降序  
example.setOrderByClause("欄位名ASC")  
// 2.去除重複,boolean型別,true為選擇不重複的記錄  
example.setDistinct(false)  
// 3.新增欄位xxx為null的條件  
criteria.andXxxIsNull  
// 4.新增欄位xxx不為null的條件  
criteria.andXxxIsNotNull  
// 5.新增xxx欄位等於value條件  
criteria.andXxxEqualTo(value)  
// 6.新增xxx欄位不等於value條件  
criteria.andXxxNotEqualTo(value)  
// 7.新增xxx欄位大於value條件  
criteria.andXxxGreaterThan(value)  
// 8.新增xxx欄位大於等於value條件  
criteria.andXxxGreaterThanOrEqualTo(value)  
// 9.新增xxx欄位小於value條件  
criteria.andXxxLessThan(value)  
// 10.新增xxx欄位小於等於value條件  
criteria.andXxxLessThanOrEqualTo(value)  
// 11.新增xxx欄位值在List  
criteria.andXxxIn(List)  
// 12.不新增xxx欄位值在List  
criteria.andXxxNotIn(List)  
// 13.新增xxx欄位值在之間  
criteria.andXxxBetween(value1,value2)  
// 14.新增xxx欄位值不在之間  
criteria.andXxxNotBetween(value1,value2)

例:執行一下方法

int updateByExample(@Param("record") TbContent record, @Param("example") TbContentExample example);

recore是從介面傳過來的物件(需要修改的屬性封裝成的物件),example就是上面設定好值之後的example

執行查詢的時候:xxxmapper.updateByExample(record,example)

舉更詳細的例子

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'

相關推薦

mybatis自動生成entitydaoMapper介面各個方法意義example實體用法

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

MyBatis自動生成EntityDao、Mapping

  新接的專案使用了MyBatis,Mybatis屬於半自動ORM,在使用這個框架中,工作量最大的就是書寫Mapping的對映檔案,對於其中最基礎的Entity,以及最基礎的CRUD,我們可以利用Mybatis-Generator來幫我們自動生成檔案。   首先下載相關檔案

利用Mybatis-generator自動生成java-beandaomapper.xml

1.首先百度: mybatis generator ,進入mybatis generator GitHub,然後進入http://www.mybatis.org/generator/. 2.點選左邊 Quick Start Guide ,他告訴我們需要匯入相關聯的jar包(

javaAction、ServiceDao的功能區分

封裝 滿足 ont 事務管理 簡單 聲明式 所在 框架搭建 調用接口 Action/Service/DAO簡介:Action是管理業務(Service)調度和管理跳轉的。Service是管理具體的功能的。Action只負責管理,而Service負責實施。DAO只完成增刪改查

Action、ServiceDao的功能區分

Action/Service/DAO簡介: Action是管理業務(Service)排程和管理跳轉的。 Service是管理具體的功能的。 Action只負責管理,而Service負責實施。 DAO只完成增刪改查,雖然可以1-n,n-n,1-1關聯,模糊、動態、子查詢都可以。但是無論多麼複雜的查詢,dao只是

spring的一個事務管理,在controllerdao都可以用

import org.springframework.transaction.support.DefaultTransactionDefinition; public PlatformTransactionManager getTransactionManager() { re

JAVAAction, Service ,modle Dao的功能區分

首先這是現在最基本的分層方式,結合了SSH架構。modle層就是對應的資料庫表的實體類。Dao層是使用了hibernate連線資料庫、操作資料庫(增刪改查)。Service層:引用對應的Dao資料庫操作,在這裡可以編寫自己需要的程式碼(比如簡單的判斷)。Action層:引用

Action, Service Dao的功能區分

1.Action/Service/DAO簡介:     Action是管理業務(Service)排程和管理跳轉的。      Service是管理具體的功能的。     Action只負責管理,而Service負責實施。     DAO只完成增刪改查,雖然可以1-

Mybatis自動生成對映檔案PO物件呼叫示例程式碼工

 src/generatorConfig.properties suppressAllComments=false driverClass=oracle.jdbc.driver.OracleDriver url=jdbc:oracle:thin:@localhost:152

Java分層思想:Action, Service ,modle Dao的功能區分

Dao主要做資料庫的互動工作:DAO層中封裝了資料庫的增刪查改方法 一般分DAO介面(定義資料增刪查改操作)和DAOImpl實現類(具體實現增刪查改方法) Model 是模型 存放你的實

使用MyBatis Generator自動生成實體mapperdao

本人使用: 整合開發環境:idea 專案管理工具:maven 資料庫:oracle 框架:Spring+SpringMVC+myBatis 主要步驟: 在pom.xml中加入外掛依賴 寫mbgConfiguration.xml檔案,jdbc.propertie

Mybatis自動生成實體,對映檔案,以及dao介面

      Mybatis不像Hibernate那樣可以直接通過Myeclipse直接生成相應的對映檔案,它是一個半自動化的ORM框架,所以主要的工作就是配置Mapping對映檔案,但是由於手寫對映檔案很容易出錯,所以可利用MyBatis生成器自動生成實體類、DAO介面和M

mybatis-generator生成modeldao程式碼

1、建立資料夾myibatisGen 2、下載mybatis-generator-core-1.3.1.jar或者其它版本的jar包,到myibatisGen資料夾下 3、為生成程式碼建立配置檔案“generatorConfig.xml” 4、生成程式碼        ja

idea + groovy + mybatis 自動生成 Dao、mappings 實體

背景 在 windows 系統中,idea 在 C:\Users\使用者名稱\.IntelliJIdea2018.2\config\extensions\com.intellij.database\schema 目錄下預設存在如下 Groovy 檔案:Generate POJOs.g

MyBatis自動生成實體DAO介面Mapping對映檔案的程式碼(逆向工程)

MyBatis屬於一種半自動的ORM框架,它需要程式設計師自己編寫sql語句和對映檔案,但是編寫對映檔案和sql語句很容易出錯,所以mybatis官方提供了Generator生成器,自動生成DAO介面。實體類和Mapping。這個生成器是根據單表自動生成myba

MyBatis的mapping.xmldao介面組合使用

Springboot與mybatis整合在application.properties檔案中mybatis.mapper-locations=classpath*:mapping/*.xmlmybatis中的mapping.xml的與Dao層組合使用bean實體類(@Data

Mybatis自動生成實體實體映射工具

ase 忽略 路徑 package mapping decimal rri upd 需要 Mybatis Mysql生成實體類 用到的Lib包: mybatis-generator-core-1.3.2.jarmysql-connector-java-5.1.30.jar

IntelliJ IDEA相關小技巧外掛 | Mybatis自動生成工具

本文簡單介紹一些idea開發常用外掛和工具,以及一些小技巧,不足之處希望大家指出,我改正。不喜勿噴! 一、IDEA開發小技巧 在使用idea開發過程中我們會對介面有一些潔癖(簡稱強迫症),下面簡單介紹一下個人的習慣: 1.idea如何檢視檔案或專案的歷史提交記錄 在

mybatis自動生成dao, model, mapper xml檔案

用mybatis的時候,手寫xml或model檔案是一個力氣活,所以可以用mybatis-gennerator外掛自動生成mybatis所需要的dao、bean、mapper xml檔案 (原文地址:http://blog.csdn.net/tolcf/article/details/50835

JavaWeb三架構ServiceDao物件單例化可行性

宣告:以下個人觀點,僅作參考;    閱讀正文的前提知識:   一. 單例模式:   單例概念(百度): 單例模式,是一種常用的軟體設計模式。在它的核心結構中只包含一個被稱為單例的特殊類。通過單例模式可以保證系統中,應用該模式的類一個類