一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - myBatis的mapper映射文件之批量處理方式

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

2022-01-24 12:54曾衛 Java教程

這篇文章主要介紹了myBatis的mapper映射文件之批量處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

#mybatis常見批量處理

在開發當中,可能經常會遇到批量處理這種情況,一般都再在java層面進行,

其本質是節省數據庫連接打開關閉的的次數,占用更少的運行內存。

mybatis批量插入

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<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批量刪除

?
1
2
3
4
5
6
7
8
<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值對應,屬于自定義變量。

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

mybatis批量修改

?
1
2
3
4
5
6
7
8
9
<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文件詳解

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

表結構:

?
1
2
3
4
5
6
7
8
9
10
11
12
CREATE TABLE customer (
id int(11) NOT NULL COMMENT ‘企業用戶ID',
name varchar(45) DEFAULT NULL COMMENT ‘名稱',
logo varchar(80) DEFAULT ' COMMENT ‘企業標識',
describe varchar(500) DEFAULT ' COMMENT ‘企業班車說明',
is_enable tinyint(1) DEFAULT ‘0' COMMENT ‘是否啟用 1=啟用 0=不啟用',
phone varchar(20) DEFAULT NULL COMMENT ‘客服電話',
admin varchar(50) DEFAULT NULL COMMENT ‘管理員賬號',
password varchar(80) DEFAULT NULL COMMENT ‘管理員密碼',
uuid varchar(80) DEFAULT NULL COMMENT ‘企業唯一ID',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ;

Mapper映射文件是在實際開發過程中使用最多的。

Mapper文件中包含的元素有

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?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綁定了與之對應的接口,值是該接口的全限定名;這個參數有且只有一個
-->
<mapper namespace="cn.rainbowbus.dao.CustomerMapper">
 
  <!--
    用來描述select語句返回字段與java屬性的映射關系。
    可以有多個resultMap標簽,用不同id區分不同標簽。
    可以實現一對多,多對多關系
  -->
  <resultMap id="BaseResultMap" type="cn.rainbowbus.entity.Customer">
    <!--
     column是表中的字段名。
     property是對應的java屬性。
     jdbcTyep: 數據庫中字段類型,它與Java中屬性類型有對應關系,詳情看下表。
     id:數據庫主鍵字段。
     result:普通字段。
     一對多標簽 :
     collection> property:對應的java屬性名  ofType:對應的java屬類型
        <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 塊,也可以被其他語句引用。
 
  -->
  <sql id="Example_Where_Clause">
    <where>/* where 可以自動去除sql語句where關鍵字后的and關鍵字*/
    /*
      向sql傳遞數組或List,  mybatis使用foreach解析,可以做批量處理。
      collection:傳入的集合的變量名稱(要遍歷的值)。
      item:每次循環將循環出的數據放入這個變量中。
      open:循環開始拼接的字符串。
      close:循環結束拼接的字符串。
      separator:循環中拼接的分隔符。
    */
      <foreach collection="oredCriteria" item="criteria" separator="or">
        /*
        判斷語句,test值等于true執行等于false跳過
        test可以是一個值為Boolean型的計算語句
        */
        <if test="criteria.valid">
        /*
          前綴'and' 被'('  替換
          prefix:前綴覆蓋并增加其內容 不寫的話默認替換為空
          suffix:后綴覆蓋并增加其內容 不寫的話默認替換為空
          prefixOverrides:前綴判斷的條件
          suffixOverrides:后綴判斷的條件
        */
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              /*
                choose 是或(or)的關系。
                choose標簽是按順序判斷其內部when標簽中的test條件出否成立,如果有一個成立,則 choose 結束。
                當 choose 中所有 when 的條件都不滿則時,則執行 otherwise 中的sql。
                類似于Java 的 switch 語句,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查詢語句標簽
  id: 與namespace接口中的方法名對應
  parameterType: 參數類型
  resultMap : 返回值類型
  自增IDset到對象中: useGeneratedKeys="true" keyProperty="id" keyColumn="id"
  支持類型簡寫,詳情看下表
  -->
  <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" />/*引入一個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刪除語句標簽
  id: 與namespace接口中的方法名對應
  parameterType: 參數類型
  -->
  <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>
 
  <!--插入語句-->
  <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>

注意

#{}占位符: 占位

如果傳入的是基本類型,那么#{}中的變量名稱可以隨意寫

如果傳入的參數是pojo類型,那么#{}中的變量名稱必須是pojo中的屬性.屬性名(user.username)

$ {}拼接符: 字符串原樣拼接(有sql注入的風險)

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

注意:使用拼接符有可能造成sql注入,在頁面輸入的時候可以加入校驗,不可輸入sql關鍵字,不可輸入空格

注意:如果是取簡單數量類型的參數,括號中的值必須為value

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

可以這樣寫來解決sql注入:select * from user where username like ‘%' #{name}'%'(開發經常使用)

動態SQL

Mybatis提供了9種動態sql標簽:trim | where | set | foreach | if | choose | when | otherwise | bind。

1.判斷語句if

判斷語句,test值等于true執行等于false跳過,test可以是一個值為Boolean型的計算語句

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

2.修剪語句:trim

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

3.循環語句:foreach

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

4.選擇語句:choose

類似于Java 的 switch 語句,choose 為 switch,when 為 case,otherwise 則為 default。

choose 是或(or)的關系。choose標簽是按順序判斷其內部when標簽中的test條件出否成立,如果有一個成立,則 choose 結束。當 choose 中所有 when 的條件都不滿則時,則執行 otherwise 中的sql。

?
1
2
3
4
5
6
7
8
9
10
11
<choose>
            <when test="false">
              ...
            </when>
            <when test="true">
 ...
            </when>
            <otherwise>
 ...同樣這不是必須的
            </otherwise>              
          </choose>

mapper對應的Java接口文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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支持別名:

別名 映射類型
_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的映射關系

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]

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://zengwei.blog.csdn.net/article/details/72958460

延伸 · 閱讀

精彩推薦
  • Java教程Java使用SAX解析xml的示例

    Java使用SAX解析xml的示例

    這篇文章主要介紹了Java使用SAX解析xml的示例,幫助大家更好的理解和學習使用Java,感興趣的朋友可以了解下...

    大行者10067412021-08-30
  • Java教程Java實現搶紅包功能

    Java實現搶紅包功能

    這篇文章主要為大家詳細介紹了Java實現搶紅包功能,采用多線程模擬多人同時搶紅包,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙...

    littleschemer13532021-05-16
  • Java教程Java BufferWriter寫文件寫不進去或缺失數據的解決

    Java BufferWriter寫文件寫不進去或缺失數據的解決

    這篇文章主要介紹了Java BufferWriter寫文件寫不進去或缺失數據的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望...

    spcoder14552021-10-18
  • Java教程升級IDEA后Lombok不能使用的解決方法

    升級IDEA后Lombok不能使用的解決方法

    最近看到提示IDEA提示升級,尋思已經有好久沒有升過級了。升級完畢重啟之后,突然發現好多錯誤,本文就來介紹一下如何解決,感興趣的可以了解一下...

    程序猿DD9332021-10-08
  • Java教程xml與Java對象的轉換詳解

    xml與Java對象的轉換詳解

    這篇文章主要介紹了xml與Java對象的轉換詳解的相關資料,需要的朋友可以參考下...

    Java教程網2942020-09-17
  • Java教程Java8中Stream使用的一個注意事項

    Java8中Stream使用的一個注意事項

    最近在工作中發現了對于集合操作轉換的神器,java8新特性 stream,但在使用中遇到了一個非常重要的注意點,所以這篇文章主要給大家介紹了關于Java8中S...

    阿杜7482021-02-04
  • Java教程小米推送Java代碼

    小米推送Java代碼

    今天小編就為大家分享一篇關于小米推送Java代碼,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧...

    富貴穩中求8032021-07-12
  • Java教程20個非常實用的Java程序代碼片段

    20個非常實用的Java程序代碼片段

    這篇文章主要為大家分享了20個非常實用的Java程序片段,對java開發項目有所幫助,感興趣的小伙伴們可以參考一下 ...

    lijiao5352020-04-06
主站蜘蛛池模板: 日本精品www色| 国产免费看黄的私人影院 | 亚洲激情婷婷 | 国产区综合另类亚洲欧美 | 久久成人永久免费播放 | 男生操女生的漫画 | 99久久一区二区精品 | 深夜在线网址 | 国产成人亚洲精品乱码在线观看 | 日韩操比视频 | 国产成人综合网 | 肉色欧美久久久久久久蜜桃 | 国产一区二区三区久久精品小说 | 偷偷狠狠的日日高清完整视频 | 男gay男gay男gay野外 | 男男gaygays18中国 | 国产美女做爰免费视频网址 | 美女操穴视频 | 日韩一区二区在线视频 | 日本啊v在线观看 | 色香视频在线 | 爽好紧别夹宝贝叫大声点护士 | 女人爽到喷水的视频免费 | 天美传媒果冻传媒星空传媒 | 欧美日韩精品一区二区三区视频 | 色婷婷在线视频 | 蜜桃麻豆| 91天堂视频 | 国产欧美一区视频在线观看 | 男同gay玩奴男同玩奴 | 天天中文 | 国产高清好大好夹受不了了 | 欧美色图日韩色图 | 91久久国产青草亚洲 | 五月天久久久 | 饭冈加奈子黑人解禁在线播放 | 日本一区二区视频免费播放 | 好姑娘完整版在线观看中文 | 亚洲午夜精品久久久久久成年 | 久久re视频精品538在线 | 四虎最新免费观看网址 |