0%

MyBatis语法简单说明

2018年2月26日 下午9:00

我这里只是简单的举个例子,具体的还是百度吧

2018/4/2文章更新:

  1. mybatis 表名做为参数 - 简书

第一部分:

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
  <select id="selectByNameAndProductId" resultMap="BaseResultMap" parameterType="map">
SELECT
<include refid="Base_Column_List"/>
from mmall_product
<where>
<if test="productName != null">
and name like #{productName}
</if>
<if test="productId != null">
and id = #{productId}
</if>
</where>
</select>

<select id="selectByNameAndCategoryIds" resultMap="BaseResultMap" parameterType="map">
SELECT
<include refid="Base_Column_List"></include>
from mmall_product
where status = 1
<if test="productName != null">
and name like #{productName}
</if>
<if test="categoryIdList != null" >
and category_id in
<foreach item="item" index="index" open="(" separator="," close=")" collection="categoryIdList">
#{item}
</foreach>
</if>
</select>

</mapper>

第二部分:批量插入


orderItemMapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13

<insert id="batchInsert" parameterType="list">
insert into mmall_order_item (id, order_no,user_id, product_id,
product_name, product_image, current_unit_price,
quantity, total_price, create_time,
update_time)
values
<foreach collection="orderItemList" index="index" item="item" separator=",">
(
#{item.id},#{item.orderNo},#{item.userId},#{item.productId},#{item.productName},#{item.productImage},#{item.currentUnitPrice},#{item.quantity},#{item.totalPrice},now(),now()
)
</foreach>
</insert>

表示xml解析器忽略解析