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

myBatis的mapper映射文件之批量處理方式

 更新時(shí)間:2021年09月30日 11:12:31   作者:曾衛(wèi)  
這篇文章主要介紹了myBatis的mapper映射文件之批量處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

#mybatis常見(jiàn)批量處理

在開(kāi)發(fā)當(dāng)中,可能經(jīng)常會(huì)遇到批量處理這種情況,一般都再在java層面進(jìn)行,

其本質(zhì)是節(jié)省數(shù)據(jù)庫(kù)連接打開(kāi)關(guān)閉的的次數(shù),占用更少的運(yùn)行內(nèi)存。

mybatis批量插入

<insert id="saveFeeRuleList" useGeneratedKeys="true" parameterType="java.util.List">
  <selectKey resultType="java.lang.String" keyProperty="id" order="AFTER">
   SELECT
   LAST_INSERT_ID()
 </selectKey>
 INSERT INTO t_product_fee_rule(
  <include refid="Base_Column_List"/>
 )
 VALUES
 <foreach collection="list" item="item" index="index" separator=",">
  (
   #{item.id},#{item.productId},
   #{item.feeCode},#{item.feeValue},
   #{item.remarks}
  )
 </foreach>
</insert>

mybatis批量刪除

<delete id="removeProductAgent" parameterType="java.util.HashMap">
 <foreach collection="maps.agentIds" item="item" index="index" open="" close="" separator=";">
  DELETE FROM t_product_agent
  WHERE 1 = 1
  AND product_id = #{maps.productId}
  AND agent_id = #{item}
 </foreach>
</delete>

此處的maps接口中的@Param值對(duì)應(yīng),屬于自定義變量。

void removeProductAgent(@Param("maps")Map<String, Object> map);

mybatis批量修改

<update id="saveUpdateFeeRuleList" parameterType="java.util.List">
  <foreach collection="list" item="item" index="index" open="" close="" separator=";">
   UPDATE t_product_fee_rule
   SET
 fee_value = #{item.feeValue},
 remarks = #{item.remarks}
   WHERE id = #{item.id}
  </foreach>
 </update>

myBatis mapper文件詳解

本文的寫(xiě)作目的主要是帶大家了解mapper的寫(xiě)法

表結(jié)構(gòu):

CREATE TABLE customer (
id int(11) NOT NULL COMMENT ‘企業(yè)用戶(hù)ID',
name varchar(45) DEFAULT NULL COMMENT ‘名稱(chēng)',
logo varchar(80) DEFAULT ‘' COMMENT ‘企業(yè)標(biāo)識(shí)',
describe varchar(500) DEFAULT ‘' COMMENT ‘企業(yè)班車(chē)說(shuō)明',
is_enable tinyint(1) DEFAULT ‘0' COMMENT ‘是否啟用 1=啟用 0=不啟用',
phone varchar(20) DEFAULT NULL COMMENT ‘客服電話',
admin varchar(50) DEFAULT NULL COMMENT ‘管理員賬號(hào)',
password varchar(80) DEFAULT NULL COMMENT ‘管理員密碼',
uuid varchar(80) DEFAULT NULL COMMENT ‘企業(yè)唯一ID',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ;

Mapper映射文件是在實(shí)際開(kāi)發(fā)過(guò)程中使用最多的。

Mapper文件中包含的元素有

  • cache – 配置給定命名空間的緩存。
  • cache-ref – 從其他命名空間引用緩存配置。
  • resultMap – 映射復(fù)雜的結(jié)果對(duì)象。
  • sql – 可以重用的 SQL 塊,也可以被其他語(yǔ)句引用。
  • insert – 映射插入語(yǔ)句
  • update – 映射更新語(yǔ)句
  • delete – 映射刪除語(yǔ)句
  • select – 映射查詢(xún)語(yǔ)句

本文的代碼都是用mybatis-generator生成的注釋部分是博主自己加的:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
  namespace綁定了與之對(duì)應(yīng)的接口,值是該接口的全限定名;這個(gè)參數(shù)有且只有一個(gè)
-->
<mapper namespace="cn.rainbowbus.dao.CustomerMapper">

  <!--
    用來(lái)描述select語(yǔ)句返回字段與java屬性的映射關(guān)系。
    可以有多個(gè)resultMap標(biāo)簽,用不同id區(qū)分不同標(biāo)簽。
    可以實(shí)現(xiàn)一對(duì)多,多對(duì)多關(guān)系
  -->
  <resultMap id="BaseResultMap" type="cn.rainbowbus.entity.Customer">
    <!--
     column是表中的字段名。
     property是對(duì)應(yīng)的java屬性。
     jdbcTyep: 數(shù)據(jù)庫(kù)中字段類(lèi)型,它與Java中屬性類(lèi)型有對(duì)應(yīng)關(guān)系,詳情看下表。
     id:數(shù)據(jù)庫(kù)主鍵字段。
     result:普通字段。
     一對(duì)多標(biāo)簽 : 
     collection> property:對(duì)應(yīng)的java屬性名  ofType:對(duì)應(yīng)的java屬類(lèi)型
     	<id property="java屬性" column="author_third_id" jdbcType="BIGINT"/>
		<result property="java屬性" column="account_id" jdbcType="VARCHAR"/>
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" javaType="string"  property="name" />
    <result column="logo" jdbcType="VARCHAR" property="logo" />
    <result column="describe" jdbcType="VARCHAR" property="describe" />
    <result column="is_enable" jdbcType="BIT" property="isEnable" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
    <result column="admin" jdbcType="VARCHAR" property="admin" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="uuid" jdbcType="VARCHAR" property="uuid" />
  </resultMap>
  <!--
  可以重用的 SQL 塊,也可以被其他語(yǔ)句引用。

  -->
  <sql id="Example_Where_Clause">
    <where>/* where 可以自動(dòng)去除sql語(yǔ)句where關(guān)鍵字后的and關(guān)鍵字*/
    /*
      向sql傳遞數(shù)組或List,  mybatis使用foreach解析,可以做批量處理。
      collection:傳入的集合的變量名稱(chēng)(要遍歷的值)。
      item:每次循環(huán)將循環(huán)出的數(shù)據(jù)放入這個(gè)變量中。
      open:循環(huán)開(kāi)始拼接的字符串。
      close:循環(huán)結(jié)束拼接的字符串。
      separator:循環(huán)中拼接的分隔符。
    */
      <foreach collection="oredCriteria" item="criteria" separator="or">
        /*
        判斷語(yǔ)句,test值等于true執(zhí)行等于false跳過(guò)
        test可以是一個(gè)值為Boolean型的計(jì)算語(yǔ)句
        */
        <if test="criteria.valid">
        /*
          前綴'and' 被'('  替換
          prefix:前綴覆蓋并增加其內(nèi)容 不寫(xiě)的話默認(rèn)替換為空
          suffix:后綴覆蓋并增加其內(nèi)容 不寫(xiě)的話默認(rèn)替換為空
          prefixOverrides:前綴判斷的條件
          suffixOverrides:后綴判斷的條件
        */
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              /*
                choose 是或(or)的關(guān)系。
                choose標(biāo)簽是按順序判斷其內(nèi)部when標(biāo)簽中的test條件出否成立,如果有一個(gè)成立,則 choose 結(jié)束。
                當(dāng) choose 中所有 when 的條件都不滿(mǎn)則時(shí),則執(zhí)行 otherwise 中的sql。
                類(lèi)似于Java 的 switch 語(yǔ)句,choose 為 switch,when 為 case,otherwise 則為 default。
              */
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    <if test="fields == null">
      id, name, logo, describe, is_enable, phone, admin, password, uuid
    </if>
    <if test="fields != null">
      ${fields}
    </if>
  </sql>

  <!--
  select查詢(xún)語(yǔ)句標(biāo)簽
  id: 與namespace接口中的方法名對(duì)應(yīng)
  parameterType: 參數(shù)類(lèi)型
  resultMap : 返回值類(lèi)型
  自增IDset到對(duì)象中: useGeneratedKeys="true" keyProperty="id" keyColumn="id" 
  支持類(lèi)型簡(jiǎn)寫(xiě),詳情看下表
  -->
  <select id="selectByExample" useGeneratedKeys="true" keyProperty="id" keyColumn="id" parameterType="cn.rainbowbus.entity.CustomerExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />/*引入一個(gè)SQL模塊*/
    from customer
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
    <if test="startRow != null">
      limit #{startRow} , #{pageSize}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    id,name,logo,describe,is_enable,phone,admin,password,uuid
    from customer
    where id = #{id,jdbcType=INTEGER}
  </select>
  <!--
  delete刪除語(yǔ)句標(biāo)簽
  id: 與namespace接口中的方法名對(duì)應(yīng)
  parameterType: 參數(shù)類(lèi)型
  -->
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from customer
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="cn.rainbowbus.entity.CustomerExample">
    delete from customer
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>

  <!--插入語(yǔ)句-->
  <insert id="insert" parameterType="cn.rainbowbus.entity.Customer">
    insert into customer (id, name, logo, 
      describe, is_enable, phone, 
      admin, password, uuid
      )
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{logo,jdbcType=VARCHAR}, 
      #{describe,jdbcType=VARCHAR}, #{isEnable,jdbcType=BIT}, #{phone,jdbcType=VARCHAR}, 
      #{admin,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{uuid,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="cn.rainbowbus.entity.Customer">
    insert into customer
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="name != null">
        name,
      </if>
      <if test="logo != null">
        logo,
      </if>
      <if test="describe != null">
        describe,
      </if>
      <if test="isEnable != null">
        is_enable,
      </if>
      <if test="phone != null">
        phone,
      </if>
      <if test="admin != null">
        admin,
      </if>
      <if test="password != null">
        password,
      </if>
      <if test="uuid != null">
        uuid,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="logo != null">
        #{logo,jdbcType=VARCHAR},
      </if>
      <if test="describe != null">
        #{describe,jdbcType=VARCHAR},
      </if>
      <if test="isEnable != null">
        #{isEnable,jdbcType=BIT},
      </if>
      <if test="phone != null">
        #{phone,jdbcType=VARCHAR},
      </if>
      <if test="admin != null">
        #{admin,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="uuid != null">
        #{uuid,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="cn.rainbowbus.entity.CustomerExample" resultType="java.lang.Long">
    select count(*) from customer
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update customer
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.name != null">
        name = #{record.name,jdbcType=VARCHAR},
      </if>
      <if test="record.logo != null">
        logo = #{record.logo,jdbcType=VARCHAR},
      </if>
      <if test="record.describe != null">
        describe = #{record.describe,jdbcType=VARCHAR},
      </if>
      <if test="record.isEnable != null">
        is_enable = #{record.isEnable,jdbcType=BIT},
      </if>
      <if test="record.phone != null">
        phone = #{record.phone,jdbcType=VARCHAR},
      </if>
      <if test="record.admin != null">
        admin = #{record.admin,jdbcType=VARCHAR},
      </if>
      <if test="record.password != null">
        password = #{record.password,jdbcType=VARCHAR},
      </if>
      <if test="record.uuid != null">
        uuid = #{record.uuid,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update customer
    set id = #{record.id,jdbcType=INTEGER},
      name = #{record.name,jdbcType=VARCHAR},
      logo = #{record.logo,jdbcType=VARCHAR},
      describe = #{record.describe,jdbcType=VARCHAR},
      is_enable = #{record.isEnable,jdbcType=BIT},
      phone = #{record.phone,jdbcType=VARCHAR},
      admin = #{record.admin,jdbcType=VARCHAR},
      password = #{record.password,jdbcType=VARCHAR},
      uuid = #{record.uuid,jdbcType=VARCHAR}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="cn.rainbowbus.entity.Customer">
    update customer
    <set>
      <if test="name != null">
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="logo != null">
        logo = #{logo,jdbcType=VARCHAR},
      </if>
      <if test="describe != null">
        describe = #{describe,jdbcType=VARCHAR},
      </if>
      <if test="isEnable != null">
        is_enable = #{isEnable,jdbcType=BIT},
      </if>
      <if test="phone != null">
        phone = #{phone,jdbcType=VARCHAR},
      </if>
      <if test="admin != null">
        admin = #{admin,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="uuid != null">
        uuid = #{uuid,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="cn.rainbowbus.entity.Customer">
    update customer
    set name = #{name,jdbcType=VARCHAR},
      logo = #{logo,jdbcType=VARCHAR},
      describe = #{describe,jdbcType=VARCHAR},
      is_enable = #{isEnable,jdbcType=BIT},
      phone = #{phone,jdbcType=VARCHAR},
      admin = #{admin,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      uuid = #{uuid,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="batchUpdateByKeys" parameterType="java.util.List">
    update customer 
    <trim prefix="set" suffixOverrides=",">
      <trim prefix="id =case id" suffix="end,">
        <foreach collection="recordList" index="index" item="item">
          <if test="item.id !=null ">
            when #{item.id,jdbcType=INTEGER} then #{item.id,jdbcType=INTEGER}
          </if>
          <if test="item.id ==null ">
            when #{item.id,jdbcType=INTEGER} then customer.id
          </if>
        </foreach>
      </trim>
      <trim prefix="name =case id" suffix="end,">
        <foreach collection="recordList" index="index" item="item">
          <if test="item.name !=null ">
            when #{item.id,jdbcType=INTEGER} then #{item.name,jdbcType=VARCHAR}
          </if>
          <if test="item.name ==null ">
            when #{item.id,jdbcType=INTEGER} then customer.name
          </if>
        </foreach>
      </trim>
      <trim prefix="logo =case id" suffix="end,">
        <foreach collection="recordList" index="index" item="item">
          <if test="item.logo !=null ">
            when #{item.id,jdbcType=INTEGER} then #{item.logo,jdbcType=VARCHAR}
          </if>
          <if test="item.logo ==null ">
            when #{item.id,jdbcType=INTEGER} then customer.logo
          </if>
        </foreach>
      </trim>
      <trim prefix="describe =case id" suffix="end,">
        <foreach collection="recordList" index="index" item="item">
          <if test="item.describe !=null ">
            when #{item.id,jdbcType=INTEGER} then #{item.describe,jdbcType=VARCHAR}
          </if>
          <if test="item.describe ==null ">
            when #{item.id,jdbcType=INTEGER} then customer.describe
          </if>
        </foreach>
      </trim>
      <trim prefix="is_enable =case id" suffix="end,">
        <foreach collection="recordList" index="index" item="item">
          <if test="item.isEnable !=null ">
            when #{item.id,jdbcType=INTEGER} then #{item.isEnable,jdbcType=BIT}
          </if>
          <if test="item.isEnable ==null ">
            when #{item.id,jdbcType=INTEGER} then customer.is_enable
          </if>
        </foreach>
      </trim>
      <trim prefix="phone =case id" suffix="end,">
        <foreach collection="recordList" index="index" item="item">
          <if test="item.phone !=null ">
            when #{item.id,jdbcType=INTEGER} then #{item.phone,jdbcType=VARCHAR}
          </if>
          <if test="item.phone ==null ">
            when #{item.id,jdbcType=INTEGER} then customer.phone
          </if>
        </foreach>
      </trim>
      <trim prefix="admin =case id" suffix="end,">
        <foreach collection="recordList" index="index" item="item">
          <if test="item.admin !=null ">
            when #{item.id,jdbcType=INTEGER} then #{item.admin,jdbcType=VARCHAR}
          </if>
          <if test="item.admin ==null ">
            when #{item.id,jdbcType=INTEGER} then customer.admin
          </if>
        </foreach>
      </trim>
      <trim prefix="password =case id" suffix="end,">
        <foreach collection="recordList" index="index" item="item">
          <if test="item.password !=null ">
            when #{item.id,jdbcType=INTEGER} then #{item.password,jdbcType=VARCHAR}
          </if>
          <if test="item.password ==null ">
            when #{item.id,jdbcType=INTEGER} then customer.password
          </if>
        </foreach>
      </trim>
      <trim prefix="uuid =case id" suffix="end,">
        <foreach collection="recordList" index="index" item="item">
          <if test="item.uuid !=null ">
            when #{item.id,jdbcType=INTEGER} then #{item.uuid,jdbcType=VARCHAR}
          </if>
          <if test="item.uuid ==null ">
            when #{item.id,jdbcType=INTEGER} then customer.uuid
          </if>
        </foreach>
      </trim>
    </trim>
    where id in(
    <foreach collection="recordList" index="index" item="item" separator=",">
      #{item.id,jdbcType=INTEGER}
    </foreach>
    )
  </update>
  <insert id="batchInsert" parameterType="cn.rainbowbus.entity.Customer">
    insert into customer (id, 
      name, logo, describe, 
      is_enable, phone, admin, 
      password, uuid)
    values <foreach collection="list" item="item" index="index" separator="," > (#{item.id,jdbcType=INTEGER}, 
      #{item.name,jdbcType=VARCHAR}, #{item.logo,jdbcType=VARCHAR}, #{item.describe,jdbcType=VARCHAR}, 
      #{item.isEnable,jdbcType=BIT}, #{item.phone,jdbcType=VARCHAR}, #{item.admin,jdbcType=VARCHAR}, 
      #{item.password,jdbcType=VARCHAR}, #{item.uuid,jdbcType=VARCHAR})</foreach>
  </insert>
  <delete id="batchDeleteByKeys" parameterType="java.lang.Integer">
    delete from customer where id in (
    <foreach collection="ids" index="index" item="id" separator=",">
      #{id}
    </foreach>
    )
  </delete>
</mapper>

注意

#{}占位符: 占位

如果傳入的是基本類(lèi)型,那么#{}中的變量名稱(chēng)可以隨意寫(xiě)

如果傳入的參數(shù)是pojo類(lèi)型,那么#{}中的變量名稱(chēng)必須是pojo中的屬性.屬性名(user.username)

$ {}拼接符: 字符串原樣拼接(有sql注入的風(fēng)險(xiǎn))

如果傳入的是基本類(lèi)型,那么中 的 變 量 名 必 須 是 v a l u e 如 果 傳 入 的 參 數(shù) 是 p o j o 類(lèi) 型 , 那 么 {}中的變量名必須是value 如果傳入的參數(shù)是pojo類(lèi)型,那么中的變量名必須是value如果傳入的參數(shù)是pojo類(lèi)型,那么{}中的變量名稱(chēng)必須是pojo中的屬性.屬性名(user.username)

注意:使用拼接符有可能造成sql注入,在頁(yè)面輸入的時(shí)候可以加入校驗(yàn),不可輸入sql關(guān)鍵字,不可輸入空格

注意:如果是取簡(jiǎn)單數(shù)量類(lèi)型的參數(shù),括號(hào)中的值必須為value

例: select * from user where username like ‘%${value}%'

可以這樣寫(xiě)來(lái)解決sql注入:select * from user where username like ‘%' #{name}'%'(開(kāi)發(fā)經(jīng)常使用)

動(dòng)態(tài)SQL

Mybatis提供了9種動(dòng)態(tài)sql標(biāo)簽:trim | where | set | foreach | if | choose | when | otherwise | bind。

1.判斷語(yǔ)句if

判斷語(yǔ)句,test值等于true執(zhí)行等于false跳過(guò),test可以是一個(gè)值為Boolean型的計(jì)算語(yǔ)句

 <if test="true">  </if>

2.修剪語(yǔ)句:trim

  • 前綴'and' 被'(' 替換
  • prefix:前綴覆蓋并增加其內(nèi)容 不寫(xiě)的話默認(rèn)替換為空
  • suffix:后綴覆蓋并增加其內(nèi)容 不寫(xiě)的話默認(rèn)替換為空
  • prefixOverrides:前綴判斷的條件
  • suffixOverrides:后綴判斷的條件
 <trim prefix="(" suffix=")" prefixOverrides="and" ></trim>

3.循環(huán)語(yǔ)句:foreach

  • 向sql傳遞數(shù)組或List, mybatis使用foreach解析,可以做批量處理。
  • collection:傳入的集合的變量名稱(chēng)(要遍歷的值)。
  • item:每次循環(huán)將循環(huán)出的數(shù)據(jù)放入這個(gè)變量中。
  • open:循環(huán)開(kāi)始拼接的字符串。
  • close:循環(huán)結(jié)束拼接的字符串。
  • separator:循環(huán)中拼接的分隔符。
<foreach collection="oredCriteria" item="criteria" separator="or"></foreach>

4.選擇語(yǔ)句:choose

類(lèi)似于Java 的 switch 語(yǔ)句,choose 為 switch,when 為 case,otherwise 則為 default。

choose 是或(or)的關(guān)系。choose標(biāo)簽是按順序判斷其內(nèi)部when標(biāo)簽中的test條件出否成立,如果有一個(gè)成立,則 choose 結(jié)束。當(dāng) choose 中所有 when 的條件都不滿(mǎn)則時(shí),則執(zhí)行 otherwise 中的sql。

    <choose>
                <when test="false">
                  ...
                </when>
                <when test="true">
     ...
                </when>
                <otherwise>
     ...同樣這不是必須的
                </otherwise>               
              </choose>

mapper對(duì)應(yīng)的Java接口文件:

package cn.rainbowbus.dao;
import cn.rainbowbus.entity.Customer;
import cn.rainbowbus.entity.CustomerExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface CustomerMapper {
    long countByExample(CustomerExample example);
    int deleteByExample(CustomerExample example);
    int deleteByPrimaryKey(Integer id);
    int insert(Customer record);
    int insertSelective(Customer record);
    List<Customer> selectByExample(CustomerExample example);
    Customer selectByPrimaryKey(Integer id);
    int updateByExampleSelective(@Param("record") Customer record, @Param("example") CustomerExample example);
    int updateByExample(@Param("record") Customer record, @Param("example") CustomerExample example);
    int updateByPrimaryKeySelective(Customer record);
    int updateByPrimaryKey(Customer record);
    int batchUpdateByKeys(@Param("recordList") List<Customer> recordList);
    void batchInsert(List<Customer> recordLst);
    int batchDeleteByKeys(@Param("ids") Integer[] ids);
}

mybatis支持別名:

別名 映射類(lèi)型
_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

jdbcType與JavaType的映射關(guān)系

jdbcType Java Type
CHAR String
ARCHAR String
ONGVARCHAR String
UMERIC java.math.BigDecimal
ECIMAL java.math.BigDecimal
IT boolean
OOLEAN boolean
INYINT byte
SMALLINT short
INTEGER int
BIGINT long
REAL float
FLOAT double
DOUBLE double
BINARY byte[]
VARBINARY byte[]
LONGVARBINARY byte[]
DATE java.sql.Date
TIME java.sql.Time
TIMESTAMP java.sql.Timestamp
CLOB Clob
BLOB Blob
ARRAY Array
DISTINCT mapping of underlying type
STRUCT Struct
REF Ref
DATALINK java.net.URL[color=red][/color]

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • springboot發(fā)布dubbo服務(wù)注冊(cè)到nacos實(shí)現(xiàn)方式

    springboot發(fā)布dubbo服務(wù)注冊(cè)到nacos實(shí)現(xiàn)方式

    這篇文章主要介紹了springboot發(fā)布dubbo服務(wù)注冊(cè)到nacos實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • java中構(gòu)造方法及this關(guān)鍵字的用法實(shí)例詳解(超詳細(xì))

    java中構(gòu)造方法及this關(guān)鍵字的用法實(shí)例詳解(超詳細(xì))

    大家都知道,java作為一門(mén)內(nèi)容豐富的編程語(yǔ)言,其中涉及的范圍是十分廣闊的,下面這篇文章主要給大家介紹了關(guān)于java中構(gòu)造方法及this關(guān)鍵字用法的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • SpringBoot自動(dòng)裝配之@Import深入講解

    SpringBoot自動(dòng)裝配之@Import深入講解

    由于最近的項(xiàng)目需求,需要在把配置類(lèi)導(dǎo)入到容器中,通過(guò)查詢(xún),使用@Import注解就能實(shí)現(xiàn)這個(gè)功能,@Import注解能夠幫我們吧普通配置類(lèi)(定義為Bean的類(lèi))導(dǎo)入到IOC容器中
    2023-01-01
  • SpringBoot 使用hibernate validator校驗(yàn)

    SpringBoot 使用hibernate validator校驗(yàn)

    這篇文章主要介紹了SpringBoot 使用hibernate validator校驗(yàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • SpringBoot自動(dòng)配置深入探究實(shí)現(xiàn)原理

    SpringBoot自動(dòng)配置深入探究實(shí)現(xiàn)原理

    在springboot的啟動(dòng)類(lèi)中可以看到@SpringBootApplication注解,它是SpringBoot的核心注解,也是一個(gè)組合注解。其中@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan三個(gè)注解尤為重要。今天我們就來(lái)淺析這三個(gè)注解的含義
    2022-08-08
  • java 刪除數(shù)組元素與刪除重復(fù)數(shù)組元素的代碼

    java 刪除數(shù)組元素與刪除重復(fù)數(shù)組元素的代碼

    在java中刪除數(shù)組元素與過(guò)濾重復(fù)數(shù)組元素我們都會(huì)需要去遍歷數(shù)組然后根據(jù)我們?cè)O(shè)置的值或方法進(jìn)行去除數(shù)組
    2013-10-10
  • Java枚舉的使用與反射應(yīng)用方式

    Java枚舉的使用與反射應(yīng)用方式

    枚舉類(lèi)型是一種特殊的類(lèi),限定為固定實(shí)例集合,且是類(lèi)型安全和線程安全的,枚舉類(lèi)型不可繼承,但可以添加屬性和方法,支持單例模式,枚舉常量可以通過(guò)反射獲取和操作,提供了靈活性和擴(kuò)展性
    2024-09-09
  • 詳解Spring中bean的幾種注入方式

    詳解Spring中bean的幾種注入方式

    這篇文章主要介紹了詳解Spring中bean的幾種注入方式,主要介紹了4種注入,主要有屬性注入、構(gòu)造函數(shù)注入、工廠方法注入,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2018-06-06
  • 使用okhttp替換Feign默認(rèn)Client的操作

    使用okhttp替換Feign默認(rèn)Client的操作

    這篇文章主要介紹了使用okhttp替換Feign默認(rèn)Client的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • Java跳出多重嵌套循環(huán)過(guò)程解析

    Java跳出多重嵌套循環(huán)過(guò)程解析

    這篇文章主要介紹了Java跳出多重嵌套循環(huán)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12

最新評(píng)論