1. 程式人生 > >MyBatis入門(二)—— 輸入映射和輸出映射、動態sql、關聯查詢

MyBatis入門(二)—— 輸入映射和輸出映射、動態sql、關聯查詢

輸出類型 sql name屬性 一對一 test HA h標簽 自動 CI

一、輸入映射和輸出映射

1. parameterType(輸入類型)

1.1 傳遞簡單類型

1 <select id="getUserById" parameterType="int" resultType="com.cenobitor.pojo.User">
2   SELECT `id`,`username`,`birthday`,`sex`,`address` FROM `user` WHERE id = #{id}
3 </select>

1.2 傳遞pojo對象

1 <insert id="insertUser" parameterType="com.cenobitor.pojo.User"
> 2 INSERT INTO USER (`username`,`birthday`,`sex`,`address`) 3 VALUES (#{username},#{birthday},#{sex},#{address}) 4 </insert>

Mybatis使用ognl表達式解析對象字段的值,#{}或者${}括號中的值為pojo屬性名稱。

1.3 傳遞pojo包裝對象

1 <!-- 1、resultType:如果要返回數據集合,只需設定為每一個元素的數據類型
2     2、 包裝的pojo取值通過 "."來獲取,如取包裝的pojo中user屬性對象裏的username屬性的表達式為:user.username
3 --> 4 <select id="getUserByQueryVo" parameterType="queryvo" resultType="com.cenobitor.mybatis.pojo.User"> 5   SELECT * FROM USER WHERE username LIKE ‘%${user.username}%‘ 6 </select>

2. resultType(輸出類型)

2.1 輸出簡單類型

1 <!-- 查詢用戶總記錄數,演示返回簡單類型 -->
2 <select id="getUserCount" resultType="int">
3   SELECT COUNT(1) FROM USER
4 </select>

2.2 輸出pojo對象:

1 <select id="getUserById" parameterType="int" resultType="com.cenobitor.pojo.User">
2    SELECT `id`,`username`,`birthday`,`sex`,`address` FROM `user` WHERE id = #{id}
3 </select>

2.3輸出pojo列表

1 <select id="getUserById" parameterType="int" resultType="com.cenobitor.pojo.User">
2    SELECT `id`,`username`,`birthday`,`sex`,`address` FROM `user` WHERE sex = #{sex}
3 </select>

2.4 輸出resultMap

字段與pojo屬性不一致時引出的resultMap

 1 <!-- resultMap入門
 2   type:映射成的pojo類型
 3   id:resultMap唯一標識
 4 -->
 5 <resultMap type="order" id="orderMap">
 6 
 7   <!-- id標簽用於綁定主鍵 -->
 8   <id property="id" column="id"/>
 9 
10   <!-- 使用result綁定普通字段 -->
11   <result property="number" column="number"/>
12   <result property="createtime" column="createtime"/>
13   <result property="note" column="note"/>
14 </resultMap>
15 
16 <!-- 使用resultMap -->
17 <select id="getOrderListResultMap" resultMap="orderMap">
18   SELECT * FROM `order`
19 </select>
二、動態sql

2.1 If

由多查詢條件拼裝引出if標簽。

 1 <!-- 演示動態sql-if標簽的使用情景 -->
 2 <select id="getUserByWhere" parameterType="user" resultType="com.cenobitor.mybatis.pojo.User">
 3   SELECT * FROM USER where 1 = 1
 4   <!-- if標簽的使用 -->
 5   <if test="id != null">
 6     and id = #{id}
 7   </if>
 8   <if test="username != null and username != ‘‘">
 9     and username LIKE ‘%${username}%‘
10   </if>
11 </select>

2.2 Where

 1 <!-- 演示動態sql-where標簽的使用情景 -->
 2 <select id="getUserByWhere2" parameterType="user" resultType="com.cenobitor.mybatis.pojo.User">
 3   <!-- include:引入sql片段,refid引入片段id -->
 4   SELECT * FROM USER
 5   <!-- where會自動加上where同處理多余的and -->
 6   <where>
 7     <!-- if標簽的使用 -->
 8     <if test="id != null">
 9       and id = #{id}
10     </if>
11     <if test="username != null and username != ‘‘">
12       and username LIKE ‘%${username}%‘
13     </if>
14   </where>
15 </select>

2.3 Foreach

 1 <!-- 演示動態sql-foreach標簽的使用情景 -->
 2 <select id="getUserByIds" parameterType="queryvo" resultType="com.cenobitor.mybatis.pojo.User">
 3   SELECT * FROM USER
 4   <!-- where會自動加上where同處理多余的and -->
 5   <where>
 6     <!-- id IN(1,10,25,30,34) -->
 7     <!-- foreach循環標簽 
 8         collection:要遍歷的集合,來源入參 
 9         open:循環開始前的sql 
10         separator:分隔符 
11         close:循環結束拼接的sql
12     -->
13     <foreach item="uid" collection="ids" open="id IN(" separator="," close=")">
14       #{uid}
15     </foreach>
16   </where>
17 </select>

2.4 Sql片段

演示通過select * 不好引出查詢字段名,抽取共用sql片段。

定義

1 <!-- sql片段 定義,id:片段唯一標識 -->
2 <sql id="user_column">
3   `id`, `username`, `birthday`, `sex`, `address`, `uuid2`
4 </sql>

② 使用

1 SELECT
2 <!-- sql片段的使用:include:引入sql片段,refid引入片段id -->
3 <include refid="user_column" />
4 FROM USER
三、關聯查詢

3.1 一對一關聯

① 方法一,使用resultType

1 <!-- 一對一關聯查詢,使用resultType -->
2 <select id="getOrderUser" resultType="orderuser">
3   SELECT 
4     o.`id`, o.`user_id` userId, o.`number`, o.`createtime`, o.`note`, u.`username`,  u.`address`
5   FROM `order` o
6   LEFT JOIN `user` u
7   ON u.id = o.`user_id`
8 </select>

②方法二,使用resultMap

1 <!-- 一對一關聯查詢-resultMap --> 2 <resultMap type="order" id="order_user_map"> 3   <!-- id標簽用於綁定主鍵 --> 4   <id property="id" column="id"/> 5   <!-- 使用result綁定普通字段 --> 6   <result property="userId" column="user_id"/> 7   <result property="number" column="number"/> 8   <result property="createtime" column="createtime"/> 9   <result property="note" column="note"/> 10   <!-- association:配置一對一關聯 11     property:綁定的用戶屬性 12     javaType:屬性數據類型,支持別名 13    --> 14   <association property="user" javaType="com.cenobitor.mybatis.pojo.User"> 15     <id property="id" column="user_id"/> 16     <result property="username" column="username"/> 17     <result property="address" column="address"/> 18     <result property="sex" column="sex"/> 19   </association> 20 </resultMap> 21 22 <!-- 一對一關聯查詢-使用resultMap --> 23 <select id="getOrderUser2" resultMap="order_user_map"> 24   SELECT 25   o.`id`,o.`user_id`, o.`number`, o.`createtime`, o.`note`, u.`username`, u.`address`, u.`sex` 26   FROM `order` o 27   LEFT JOIN `user` u 28   ON u.id = o.`user_id` 29 </select>

3.2一對多關聯

 1 <!-- 一對多關聯查詢 -->
 2 <resultMap type="user" id="user_order_map">
 3   <id property="id" column="id" />
 4   <result property="username" column="username" />
 5   <result property="birthday" column="birthday" />
 6   <result property="address" column="address" />
 7   <result property="sex" column="sex" />
 8   <result property="uuid2" column="uuid2" />
 9   <!-- collection:配置一對多關系
10     property:用戶下的order屬性
11     ofType:property的數據類型,支持別名
12   -->
13   <collection property="orders" ofType="order">
14     <!-- id標簽用於綁定主鍵 -->
15     <id property="id" column="oid"/>
16     <!-- 使用result綁定普通字段 -->
17     <result property="userId" column="id"/>
18     <result property="number" column="number"/>
19     <result property="createtime" column="createtime"/>
20   </collection>
21 </resultMap>
22 <!-- 一對多關聯查詢 -->
23 <select id="getUserOrder" resultMap="user_order_map">
24   SELECT
25   u.`id`, u.`username`,u.`birthday`,u.`sex`,u.`address`,u.`uuid2`,o.`id` oid,o.`number`,o.`createtime`
26   FROM `user` u
27   LEFT JOIN `order` o
28   ON o.`user_id` = u.`id`
29 </select>

MyBatis入門(二)—— 輸入映射和輸出映射、動態sql、關聯查詢