欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis中Mapper標簽總結(jié)大全

 更新時間:2020年06月15日 14:44:46   作者:懷初心攀高峰  
這篇文章主要介紹了Mybatis中Mapper標簽總結(jié)大全,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

一、標簽分類

定義SQL語句

  • insert
  • delete
  • update
  • select

配置關聯(lián)關系

  • collection
  • association

配置java對象屬性與查詢結(jié)果集中列名的對應關系

  • resultMap

控制動態(tài)SQL拼接

  • foreach
  • if
  • choose

格式化輸出

  • where
  • set
  • trim

定義常量

  • sql

其他

  • include

二、標簽總結(jié)

1. 基礎SQL標簽

1.1 查詢select

標簽屬性

  • id 唯一的名稱,對應dao中mapper的接口名稱
  • paramterType 定義傳入的參數(shù)類型
  • resultType  返回數(shù)據(jù)類型對應實體類
  • resultMap 外部 resultMap 的命名引用。結(jié)果集的映射是 MyBatis 最強大的特性,對其有一個很好的理解的話,許多復雜映射的情形都能迎刃而解。使用 resultMap 或 resultType,但不能同時使用
  • flushCache 將其設置為 true,任何時候只要語句被調(diào)用,都會導致本地緩存和二級緩存都會被清空,默認值:false
  • useCache 將其設置為 true,將會導致本條語句的結(jié)果被二級緩存,默認值:對 select 元素為 true
  • timeout 這個設置是在拋出異常之前,驅(qū)動程序等待數(shù)據(jù)庫返回請求結(jié)果的秒數(shù)。默認值為 unset(依賴驅(qū)動)
  • fetchSize 這是嘗試影響驅(qū)動程序每次批量返回的結(jié)果行數(shù)和這個設置值相等。默認值為 unset(依賴驅(qū)動)。
  • statementType  STATEMENT,PREPARED 或 CALLABLE 的一個。這會讓 MyBatis 分別使用 Statement,PreparedStatement 或 CallableStatement,默認值:PREPARED。
  • resultSetType  FORWARD_ONLY,SCROLL_SENSITIVE 或 SCROLL_INSENSITIVE 中的一個,默認值為 unset (依賴驅(qū)動)。
  • databaseId 如果配置了 databaseIdProvider,MyBatis 會加載所有的不帶 databaseId 或匹配當前 databaseId 的語句;如果帶或者不帶的語句都有,則不帶的會被忽略。
  • resultOrdered 這個設置僅針對嵌套結(jié)果 select 語句適用:如果為true,就是假設包含了嵌套結(jié)果集或是分組了,這樣的話當返回一個主結(jié)果行的時候,就不會發(fā)生有對前面結(jié)果集的引用的情況。這就使得在獲取嵌套的結(jié)果集的時候不至導致內(nèi)存不夠用。默認值:false。
  • resultSets 這個設置僅對多結(jié)果集的情況適用,它將列出語句執(zhí)行后返回的結(jié)果集并每個結(jié)果集給一個名稱,名稱是逗號分隔的。
 /**
  * 根據(jù)條件查詢用戶集合
  */
 List<User> selectUsers(@Param("cond")Map<String, Object> map);
<!-- 返回的是List,resultType給定的值是List里面的實體類而不是list,mybatis會自動把結(jié)果變成List -->
 <select id="selectUsers" parameterType="map" resultType="con.it.bean.User">
  select id, username, password, sex, birthday, address from user u
  <where>
   <trim suffixOverrides=",">
    <if test="cond.username != null and cond.username != ''">
     u.username = #{cond.username},
    </if>
    <if test="cond.sex != null">
     and u.sex = #{cond.sex},
    </if>
     <if test="cond.beginTime != null">
     <![CDATA[ and DATE_FORMAT(u.birthday, '%Y-%m-%d %H:%T:%s') >= DATE_FORMAT(#{beginTime}, '%Y-%m-%d %H:%T:%s'), ]]>
    </if>
    <if test="cond.endTime != null">
     <![CDATA[ and DATE_FORMAT(u.birthday, '%Y-%m-%d %H:%T:%s') <= DATE_FORMAT(#{endTime}, '%Y-%m-%d %H:%T:%s'), ]]>
    </if>
    <if test="cond.address != null and cond.address != ''">
     and u.addrerss like '%' || #{cond.address} || '%',
    </if>
   </trim>
  </where>
 </select>

1.2 增刪改

標簽屬性

  • id 唯一的名稱,對應dao中mapper的接口名稱
  • parameterType 將要傳入語句的參數(shù)的完全限定類名或別名。這個屬性是可選的,因為 MyBatis 可以通過 TypeHandler 推斷出具體傳入語句的參數(shù),默認值為 unset。
  • flushCache 將其設置為 true,任何時候只要語句被調(diào)用,都會導致本地緩存和二級緩存都會被清空,默認值:true(對應插入、更新和刪除語句)。
  • timeout 這個設置是在拋出異常之前,驅(qū)動程序等待數(shù)據(jù)庫返回請求結(jié)果的秒數(shù)。默認值為 unset(依賴驅(qū)動)。
  • statementType STATEMENT,PREPARED 或 CALLABLE 的一個。這會讓 MyBatis 分別使用 Statement,PreparedStatement 或 CallableStatement,默認值:PREPARED。
  • useGeneratedKeys(僅對 insert 和 update 有用)這會令 MyBatis 使用 JDBC 的 getGeneratedKeys 方法來取出由數(shù)據(jù)庫內(nèi)部生成的主鍵(比如:像 MySQL 和 SQL Server這樣的關系數(shù)據(jù)庫管理系統(tǒng)的自動遞增字段, oracle使用序列是不支持的,通過selectKey可以返回主鍵),默認值:false。
  • keyProperty (僅對 insert 和 update 有用)唯一標記一個屬性,MyBatis 會通過 getGeneratedKeys 的返回值或者通過 insert 語句的 selectKey子元素設置它的鍵值,默認:unset。如果希望得到多個生成的列,也可以是逗號分隔的屬性名稱列表。
  • keyColumn(僅對 insert 和 update 有用)通過生成的鍵值設置表中的列名,這個設置僅在某些數(shù)據(jù)庫(像PostgreSQL)是必須的,當主鍵列不是表中的第一列的時候需要設置。如果希望得到多個生成的列,也可以是逗號分隔的屬性名稱列表。
  • databaseId 如果配置了 databaseIdProvider,MyBatis 會加載所有的不帶 databaseId 或匹配當前 databaseId 的語句;如果帶或者不帶的語句都有,則不帶的會被忽略。
 <insert id="insert" parameterType="com.it.bean.User">
 
  <!-- 使用序列插入oracle數(shù)據(jù)庫返回主鍵,MYSQL數(shù)據(jù)庫無需添加selectKey -->
  <selectKey resultType="long" order="BEFORE" keyProperty="id">
   SELECT user_seq.NEXTVAL as id from DUAL
  </selectKey>
  
  insert into User (ID, USERNAME, PASSWORD, SEX, ADRESS, CREATED_BY, CREADTED_DATE)
  values (#{id}, #{username}, #{password}, #{sex}, #{adress}, #{createdBy}, SYSDATE)
 </insert>

1.3 其他基礎標簽

1.3.1 sql 標簽

定義一些常用的sql語句片段

<sql id="selectParam">
 id, username, password, sex, birthday, address
</sql>

1.3.2 include 標簽

引用其他的常量,通常和sql一起使用

<select>
 select <include refid="selectParam"></include>
 from user
</select>

1.3.3 if 標簽

基本都是用來判斷值是否為空,注意Integer的判斷,mybatis會默認把0變成 ‘'

<if test="item != null and item != ''"></if>

<!-- 如果是Integer類型的需要把and后面去掉或是加上or-->
<if test="item != null"></if>
<if test="item != null and item != '' or item == 0"></if>

1.3.4 別名

經(jīng)常使用的類型可以定義別名,方便使用,mybatis也注冊了很多別名方便我們使用,詳情見底部附錄

<typeAliases>
  <typeAlias type="com.it.bean.User" alias="User"/>
</typeAliases>

2. collection與association標簽

collection與association的屬性一樣,都是用于resultMap返回關聯(lián)映射使用,collection關聯(lián)的是集合,而association是關聯(lián)單個對象

標簽屬性

  • property resultMap返回實體類中字段和result標簽中的property一樣
  • column 數(shù)據(jù)庫的列名或者列標簽別名,是關聯(lián)查詢往下一個語句傳送值。注意: 在處理組合鍵時,您可以使用column=“{prop1=col1,prop2=col2}”這樣的語法,設置多個列名傳入到嵌套查詢語句。這就會把prop1和prop2設置到目標嵌套選擇語句的參數(shù)對象中。
  • javaType 一般為ArrayList或是java.util.List
  • ofType java的實體類,對應數(shù)據(jù)庫表的列名稱,即關聯(lián)查詢select對應返回的類
  • select 執(zhí)行一個其他映射的sql語句返回一個java實體類型
/**
 *問題表
 */
public class Question {
 
 private Long id; //問題id

 private String question; //問題
 
 private Integer questionType; //問題類型
 
 private List<QuestionAnswer> answerList; //問題選項集合
 
 //Getter和Setter省略
}

/**
 *問題選項表
 */
public class QuestionAnswer {
 
 private Long id; //選項id
 
 private Long questionId; //問題id

 private String answer; //選項
 
 //Getter和Setter省略
}

<!-- 具體可參考下面ResultMap -->
<collection property="answerList" javaType="java.util.List"
    ofType="com.it.bean.QuestionAnswer" column="id" 
    select="setlectQuestionAnswerByQuestionId"/>

3. resultMap標簽

resultMap屬性

  • id  唯一標識
  • type  返回類型
  • extends  繼承別的resultMap,可選

關聯(lián)其他標簽

  • id 設置主鍵使用,使用此標簽配置映射關系(可能不止一個)
  • result 一般屬性的配置映射關系,一般不止一個
  • association 關聯(lián)一個對象使用
  • collection 關聯(lián)一個集合使用
<!-- 返回關聯(lián)查詢的問題 -->
<resultMap id="detail_result" type="com.it.bean.Question">
 <id column="id" property="id" />
 <result column="question" property="question" />
 <result column="question_type" property="questionType" />
 <collection property="answerList" javaType="java.util.List"
    ofType="com.it.bean.QuestionAnswer" column="id" 
    select="setlectQuestionAnswerByQuestionId"/>
</resultMap>

<!-- 查詢問題集 -->
<select id="selectQuestions" parameterType="map" resultMap="detail_result">
 select q.id, q.question, q.question_type 
 from question q 
 <where>
  <if test="cond.id != null">
   q.id = #{cond.id}
  </if>
  <if test="cond.idList != null and cond.idList.size() != 0">
   q.id in 
   <foreach collection="cond.idList" item="id" open="(" separator="," close=")">
    #{id}
   </foreach>
  </if>
 </where>
</select>

<!-- 查詢對應問題的答案集 -->
<select id="setlectQuestionAnswerByQuestionId" parameterType="long" resultType="com.it.bean.QuestionAnswer">
 select a.id, a.answer from question_answer a where a.question_id = #{id}
</select>

4. foreach標簽

foreach屬性

  • collection 循環(huán)的集合。傳的是集合為list,數(shù)組為array, 如果是map為java.util.HashMap
  • item 循環(huán)的key
  • index 循環(huán)的下表順序
  • open 循環(huán)的開頭
  • close 循環(huán)結(jié)束
  • separator 循環(huán)的分隔符
<sql id="base_column">id, question_id, answer</sql>

<!-- oracle的批量插入 -->
<insert id="insertBatchOracle" parameterType="list">
 insert into question_answer ( <include refid="base_column" /> ) 
 select question_answer_seq.NEXTVAL, A.* from (
  <foreach collection="list" item="item" separator="union all">
   select #{item.questionId}, #{item.answer} from dual
  </foreach>
 ) A 
</insert>

<!-- Mysql的批量插入,主鍵自增 -->
<insert id="insertBatchMysql" parameterType="list">
 insert into question_answer ( <include refid="base_column" /> ) 
 values 
  <foreach collection="list" item="item" open="(" separator="union all" close=")">
   #{item.id}, #{item.questionId}, #{item.answer}
  </foreach>
</insert>

5. where標簽

where用來去掉多條件查詢時,開頭多余的and

 <select id="selectUserList" parameterType="com.it.bean.User" resultType="com.it.bean.User">
  <!-- 引用Sql片段 -->
  select <include refid="selectParam"> from user u
  <where>
   <!--where 可以自動去掉條件中的第一個and-->
   <if test="id != null">
    and u.id = #{id}
   </if>
   <if test="name != null and name != ''">
    and u.name = #{name}
   </if>
  </where>
 </select>

6. set標簽

set是mybatis提供的一個智能標記,當在update語句中使用if標簽時,如果前面的if沒有執(zhí)行,則或?qū)е露禾柖嘤噱e誤。使用set標簽可以將動態(tài)的配置SET 關鍵字,和剔除追加到條件末尾的任何不相關的逗號。
沒有使用if標簽時,如果有一個參數(shù)為null,都會導致錯誤,如下示例:

 <update id="updateUser" parameterType="com.it.bean.user">
  update user
  <set>
   <if test="username != null and username != ''">
    username = #{username},
   </if>
   <if test="sex != null and sex == 0 or sex == 1">
    sex = #{sex},
   </if>
   <if test="birthday != null "> 
    birthday = #{birthday},
   </if > 
   <if test="address != null and address != ''">
    address = #{address},
   </if>
   <if test="lastModifiedBy != null and lastModifiedBy != ''">
    last_modified_by = #{lastModifiedBy},
    last_modified_date = SYSDATE,
   </if>
  </set>
  <where>
   id = #{id}
  </where>
 </update>

7. trim標簽

trim標記是一個格式化的標記,可以完成set或者是where標記的功能

標簽屬性

  • prefix、suffix 表示再trim標簽包裹部分的前面或后面添加內(nèi)容(注意:是沒有prefixOverrides,suffixOverrides的情況下)
  • prefixOverrides,suffixOverrides 表示覆蓋內(nèi)容,如果只有這兩個屬性表示刪除內(nèi)容
<update id="test" parameterType="com.it.bean.User">
 update user
  <!-- 開頭加上set,結(jié)尾去除最后一個逗號 -->
  <trim prefix="set" suffixOverrides=",">
   <if test="username!=null and username != ''">
    name= #{username},
   </if>

   <if test="password!=null and password != ''">
    password= #{password},
   </if>

  </trim>
  <where>
   id = #{id}
  </where>
 </update>

8. choose、when、otherwise標簽

有時候我們并不想應用所有的條件,而只是想從多個選項中選擇一個。MyBatis提供了choose 元素,按順序判斷when中的條件出否成立,如果有一個成立,則choose結(jié)束。當choose中所有when的條件都不滿則時,則執(zhí)行 otherwise中的sql。類似于Java 的switch 語句,choose為switch,when為case,otherwise則為default。if是與(and)的關系,而choose是或(or)的關系

<select id="getUserList" resultType="com.it.bean.User" parameterType="com.it.bean.User"> 
 SELECT <include refid="resultParam"></include> FROM User u 
 <where> 
  <choose> 
   <when test="username !=null and username != ''"> 
    u.username LIKE CONCAT(CONCAT('%', #{username}),'%') 
   </when > 
   <when test="sex != null"> 
    AND u.sex = #{sex} 
   </when > 
   <when test="birthday != null "> 
    AND u.birthday = #{birthday} 
   </when > 
   <otherwise> 
   </otherwise> 
  </choose> 
 </where> 
</select> 

附Mybatis已經(jīng)注冊好的別名表

別名 映射類型
_byte byte
_long long
_short short
_int int
_integer int
_double double
_float float
_boolean boolean
string String
byte Byte
long Long
short Short
int Integer
integer Integer
double Double
float Float
boolean Boolean
date Date
decimal BigDecimal
bigdecimal BigDecimal
map Map
hashmap HashMap
list list
arraylist ArrayList
collection Collection
iterator Iterator

二、寫在后面

在網(wǎng)上看了很多標簽的解釋,但不是很全,我就自己總結(jié)了一份,搭配示例更好理解標簽的含義,如有什么遺漏或是錯誤還望多多發(fā)言補充,我會繼續(xù)完善。

注: 關于參數(shù)指定jdbcType,是因為當傳參為null時候,mybatis無法自動判斷類型,就必須要顯示指定它的類型,多用于insert中 

到此這篇關于Mybatis中Mapper標簽總結(jié)大全的文章就介紹到這了,更多相關Mybatis Mapper標簽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 淺談Java并發(fā)中ReentrantLock鎖應該怎么用

    淺談Java并發(fā)中ReentrantLock鎖應該怎么用

    本文主要介紹了ava并發(fā)中ReentrantLock鎖的具體使用,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Java算法練習題,每天進步一點點(2)

    Java算法練習題,每天進步一點點(2)

    方法下面小編就為大家?guī)硪黄狫ava算法的一道練習題(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-07-07
  • 淺談JVM之類的加載鏈接和初始化

    淺談JVM之類的加載鏈接和初始化

    有了java class文件之后,為了讓class文件轉(zhuǎn)換成為JVM可以真正運行的結(jié)構(gòu),需要經(jīng)歷加載,鏈接和初始化的過程。這三個過程是怎么工作的呢?在本文中你將會找到答案。
    2021-06-06
  • 一文搞懂JMeter engine中HashTree的配置問題

    一文搞懂JMeter engine中HashTree的配置問題

    本文主要介紹了JMeter engine中HashTree的配置,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Eolink上傳文件到Java后臺進行處理的示例代碼

    Eolink上傳文件到Java后臺進行處理的示例代碼

    這篇文章主要介紹了Eolink上傳文件到Java后臺進行處理,這里是上傳的excel表格數(shù)據(jù)并轉(zhuǎn)換為java集合對象、然后進行業(yè)務邏輯處理判斷最后保存到數(shù)據(jù)庫?,需要的朋友可以參考下
    2022-12-12
  • java selenium 操作瀏覽器實例

    java selenium 操作瀏覽器實例

    本文主要介紹java selenium 操作瀏覽器,這里整理了相關資料,并附示例代碼,有需要的小伙伴可以參考下
    2016-08-08
  • SpringBoot中配置雙數(shù)據(jù)源的實現(xiàn)示例

    SpringBoot中配置雙數(shù)據(jù)源的實現(xiàn)示例

    在許多應用程序中,可能會遇到需要連接多個數(shù)據(jù)庫的情況,本文主要介紹了SpringBoot中配置雙數(shù)據(jù)源的實現(xiàn)示例,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-08-08
  • SpringBoot Bean花式注解方法示例下篇

    SpringBoot Bean花式注解方法示例下篇

    這篇文章主要介紹了SpringBoot Bean花式注解方法,很多時候我們需要根據(jù)不同的條件在容器中加載不同的Bean,或者根據(jù)不同的條件來選擇是否在容器中加載某個Bean
    2023-02-02
  • java中循環(huán)遍歷刪除List和Set集合中元素的方法(推薦)

    java中循環(huán)遍歷刪除List和Set集合中元素的方法(推薦)

    下面小編就為大家?guī)硪黄猨ava中循環(huán)遍歷刪除List和Set集合中元素的方法(推薦)。小編覺得挺不錯的,在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12
  • Spring?Boot?中的?@HystrixCommand?注解原理及使用方法

    Spring?Boot?中的?@HystrixCommand?注解原理及使用方法

    通過使用 @HystrixCommand 注解,我們可以輕松地實現(xiàn)對方法的隔離和監(jiān)控,從而提高系統(tǒng)的可靠性和穩(wěn)定性,本文介紹了Spring Boot 中的@HystrixCommand注解是什么,其原理以及如何使用,感興趣的朋友跟隨小編一起看看吧
    2023-07-07

最新評論