亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

MyBatis?Mapper.XML?標(biāo)簽使用小結(jié)

 更新時(shí)間:2024年10月22日 09:12:09   作者:VipSoft  
在MyBatis中,通過resultMap可以解決字段名和屬性名不一致的問題,對(duì)于復(fù)雜的查詢,引用實(shí)體或使用<sql>標(biāo)簽可以定義復(fù)用的SQL片段,提高代碼的可讀性和編碼效率,使用這些高級(jí)映射和動(dòng)態(tài)SQL技巧,可以有效地處理復(fù)雜的數(shù)據(jù)庫交互場景

直接將值返回給對(duì)象

<select id="list" resultType="com.vipsoft.base.entity.UserInfo">
   SELECT Id,Title FROM User
</select>

如果字段和屬性名不一致時(shí),通過 resultMap 做映射

<resultMap id="StudentResult" type="com.mybatis3.domain.Student">
  <id column="stud_id" property="studId"  jdbcType="VARCHAR"/>
  <result column="user_name" property="name"  jdbcType="VARCHAR"/>
  <result column="email"  property="email"  jdbcType="VARCHAR" />
  <result column="phone"  property="phone"  jdbcType="VARCHAR" />
  <result column="status" property="status" jdbcType="INTEGER" />
  <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
</resultMap>
<select id="findAllStudents" resultMap="StudentResult" >
  SELECT * FROM STUDENTS
</select>

實(shí)體引用另一個(gè)實(shí)體的查詢結(jié)果返回

<resultMap type="Course" id="CourseResult">
  <id column="course_id" property="courseId"/>
  <result column="name" property="name"/>
  <result column="description" property="description"/>
  <result column="start_date" property="startDate"/>
  <result column="end_date" property="endDate"/>
</resultMap>
<resultMap type="Tutor" id="TutorResult">
  <id column="tutor_id" property="tutorId"/>
  <result column="tutor_name" property="name"/>
  <result column="email" property="email"/>
  <collection property="courses" resultMap="CourseResult"/>
</resultMap>
<select id="findTutorById" parameterType="int" resultMap="TutorResult">
  SELECT T.TUTOR_ID, T.NAME AS TUTOR_NAME, EMAIL, C.COURSE_ID, C.NAME, DESCRIPTION, START_DATE, END_DATE
  FROM TUTORS T LEFT OUTER JOIN ADDRESSES A ON T.ADDR_ID=A.ADDR_ID
  LEFT OUTER JOIN COURSES C ON T.TUTOR_ID=C.TUTOR_ID
  WHERE T.TUTOR_ID=#{tutorId}
</select>

choose 該方式適用于多個(gè)條件中選擇一個(gè)滿足條件的來生成sql

<select id="searchCourses" parameterType="hashmap" resultMap="CourseResult">
  SELECT * FROM COURSES
  <choose>
    <when test="searchBy == 'Tutor'">
      WHERE TUTOR_ID= #{tutorId}
    </when>
    <when test="searchBy == 'CourseName'">
      WHERE name like #{courseName}
    </when>
    <otherwise>
      WHERE TUTOR start_date  >= now()
    </otherwise>
  </choose>
</select>

where 適用于從多個(gè)條件中選擇所有滿足條件的來構(gòu)成condtions,

符號(hào)小于小于等于大于大于等于單引號(hào)雙引號(hào)
原符號(hào)<<=>>=&'"
替換符號(hào)&lt;&lt;=&gt;&gt;=&amp;&apos;&quot;
<select id="searchCourses" parameterType="hashmap" resultMap="CourseResult">
  SELECT * FROM COURSES
  <where>  
    <if test=" tutorId != null ">
      TUTOR_ID= #{tutorId}
    </if>
    <if test="courseName != null">
      AND name like #{courseName}
    </if>
    <if test="startDate != null">
      AND start_date  >= #{startDate} <!--大于等于 開始時(shí)間-->
    </if>
    <if test="endDate != null">
      AND end_date  <= #{endDate} <!--小于等于 結(jié)束時(shí)間-->
    </if>
  </where>
</select>

foreach

<select id="selectUserByListId" parameterType="com.ys.vo.UserVo" resultType="com.ys.po.User">
select * from user
  <where>
    <!--
    collection:指定輸入對(duì)象中的集合屬性.該屬性的值有三種:list,array,map,根據(jù)傳入的集合類型而設(shè)定該值。
    item:每次遍歷生成的對(duì)象
    index:當(dāng)前迭代的次數(shù)
    open:開始遍歷時(shí)的拼接字符串
    close:結(jié)束時(shí)拼接的字符串
    separator:遍歷對(duì)象之間需要拼接的字符串
    select * from user where 1=1 and id in (1,2,3)
    -->
    <foreach collection="list" item="id" open="(" separator="," close=")">
     #{id}
    </foreach>
  </where>
</select>

select * from user where id=1 or id=2 or id=3

<select id="selectUserByListId" parameterType="com.ys.vo.UserVo" resultType="com.ys.po.User">
select * from user
  <where>
    <foreach collection="list" item="id" open="and (" close=")" separator="or">
       id=#{id}
    </foreach>
  </where>
</select>

< sql >標(biāo)簽
該標(biāo)簽主要定義復(fù)用的sql語句片段,在執(zhí)行的sql語句標(biāo)簽直接引用即可??梢蕴岣呔幋a效率、簡化代碼和提高可讀性。

需要配置id熟悉,表示該sql片段的唯一標(biāo)識(shí)。

引用:通過<include refid=" " / >標(biāo)簽引用,refid的值就是< sql>的id屬性的值。

<sql id="Base_Column_List">
  id, question, answer 
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
  select 
  <include refid="Base_Column_List" />
  from java
  where id = #{id,jdbcType=BIGINT}
</select>

<set> : 主要用來替換sql語句中的set字段,一般在update中使用。

<update>
  update user 
  <set>
    <if test="name != null and name.length()>0">name = #{name},</if>
    <if test="age != null and age .length()>0">age = #{age },</if>
  </set>
  where id = #{id}
</update>

批量更新,一次執(zhí)行多條SQL

<update>
    <foreach collection="list" item="item" index="index" separator=";">
        update user 
        <set>
            <if test="name != null and name.length()>0">name = #{name},</if>
            <if test="age != null and age .length()>0">age = #{age },</if>
        </set>
        <where>
            <if test="id != null and id != ''">
                AND id = #{id,jdbcType=VARCHAR}
            </if>
            <if test="userName != null and userName != ''">
                <bind name="bindUserName" value="'%' + userName + '%'" />
                AND UserName like #{bindUserName}
            </if> 
            <if test="status != null">
                AND Status = #{status,jdbcType=INTEGER}
            </if>
        </where>
    </foreach>
</update>

<trim> : 是一個(gè)格式化的標(biāo)記,可以完成set或者是where標(biāo)記的功能。

select * from user 
<trim prefix="WHERE" prefixoverride="AND |OR">
  <if test="name != null and name.length()>0"> AND name=#{name}</if>
  <if test="age != null and age.length()>0"> AND age=#{age}</if>
</trim> 
假如說name和age的值都不為null的話打印的SQL為:select * from user where name = ‘xx' and age = ‘xx'
在where的后面是不存在第一個(gè)and的,上面兩個(gè)屬性的意思如下:
  prefix:前綴      
  prefixoverride:去掉第一個(gè)and或者是or
update user
<trim prefix="set" suffixoverride="," suffix=" where id = #{id} ">
  <if test="name != null and name.length()>0"> name=#{name} , </if>
  <if test="age!= null and age.length()>0"> age=#{age} ,  </if>
</trim>
假如說name和age的值都不為null的話打印的SQL為:update user set name=‘xx' , age=‘xx' where id=‘x'
在age='xx'的后面不存在逗號(hào),而且自動(dòng)加了一個(gè)set前綴和where后綴,上面三個(gè)屬性的意義如下,其中prefix意義如上:
   suffixoverride:去掉最后一個(gè)逗號(hào)(也可以是其他的標(biāo)記,就像是上面前綴中的and一樣)
   suffix:后綴

 choose & foreach list 有一個(gè)值,條件=,有多個(gè)值 in 

<update id="updateStatus" parameterType="java.util.ArrayList" >
  update User
  set Status = 1,UpdateTime=(select GETDATE())
  <choose>
    <when test="idList != null and idList.size==1">
      WHERE Id= #{id,jdbcType=VARCHAR}
    </when>
    <when test="idList != null and idList.size>1">
      WHERE Id in
      <foreach collection="idList" item="id" open="(" close=")" separator=",">
        <if test="id != null and id != '' ">
          #{id,jdbcType=VARCHAR}
        </if>
      </foreach>
    </when>
    <otherwise>
      WHERE 1=0
    </otherwise>
  </choose>
</update>

到此這篇關(guān)于MyBatis Mapper.XML 標(biāo)簽使用小結(jié)的文章就介紹到這了,更多相關(guān)MyBatis Mapper.XML 標(biāo)簽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot項(xiàng)目配置數(shù)據(jù)庫密碼加密相關(guān)代碼

    SpringBoot項(xiàng)目配置數(shù)據(jù)庫密碼加密相關(guān)代碼

    這篇文章主要介紹了SpringBoot項(xiàng)目配置數(shù)據(jù)庫密碼加密的相關(guān)資料,本文介紹了在Springboot項(xiàng)目中配置數(shù)據(jù)庫連接時(shí)存在的安全問題,即用戶名和密碼以明文形式存儲(chǔ),容易泄露,提出了一種簡單的加密方案,需要的朋友可以參考下
    2024-11-11
  • spring+mybatis 通過@ResponseBody返回結(jié)果中文亂碼的解決方法

    spring+mybatis 通過@ResponseBody返回結(jié)果中文亂碼的解決方法

    下面小編就為大家分享一篇spring+mybatis 通過@ResponseBody返回結(jié)果中文亂碼的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • MybatisPlus如何調(diào)用count函數(shù)

    MybatisPlus如何調(diào)用count函數(shù)

    這篇文章主要介紹了MybatisPlus如何調(diào)用count函數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • java線程封閉之棧封閉和ThreadLocal

    java線程封閉之棧封閉和ThreadLocal

    這篇文章主要介紹了java線程封閉之棧封閉和ThreadLocal,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • JMeter自定義日志與日志分析的實(shí)現(xiàn)

    JMeter自定義日志與日志分析的實(shí)現(xiàn)

    JMeter與Java程序一樣,會(huì)記錄事件日志,本文就介紹一下JMeter自定義日志與日志分析的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • 解決線程池中ThreadGroup的坑

    解決線程池中ThreadGroup的坑

    這篇文章主要介紹了解決線程池中ThreadGroup的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • SpringBoot錯(cuò)誤處理流程深入詳解

    SpringBoot錯(cuò)誤處理流程深入詳解

    在項(xiàng)目開發(fā)中出現(xiàn)異常時(shí)很平常不過的事情,我們處理異常也有很多種方式。本文將詳細(xì)為大家講解SpringBoot實(shí)現(xiàn)異常處理幾種方法,感興趣的可以學(xué)習(xí)一下
    2022-10-10
  • java Socket無法完全接收返回內(nèi)容的解決方案

    java Socket無法完全接收返回內(nèi)容的解決方案

    這篇文章主要介紹了java Socket無法完全接收返回內(nèi)容的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 關(guān)于Java中ArrayList的源碼分析

    關(guān)于Java中ArrayList的源碼分析

    這篇文章主要從源碼角度帶大家深入了解一下Java中ArrayList的構(gòu)造方法和屬性等知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-05-05
  • Java防止非法盜鏈的幾種解決方案

    Java防止非法盜鏈的幾種解決方案

    防止別人通過一些技術(shù)手段盜用本站的資源,本文主要介紹了Java防止非法盜鏈的幾種解決方案,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10

最新評(píng)論