天天看點

MyBatis入門介紹(二)

1、根據使用者名查詢使用者資訊,查詢條件放到QueryVo的user屬性中,入參傳遞user對象

<!-- 使用包裝類型查詢使用者 
使用ognl從對象中取屬性值,如果是包裝對象可以使用.操作符來取内容部的屬性
-->
<select id="findUserByQueryVo" parameterType="queryvo" resultType="user">
  SELECT * FROM user where username like '%${user.username}%'
</select>      

2、擷取使用者清單總數

<select id="findUserCount" resultType="int">
  select count(1) from user
</select>      

resultType可以指定pojo将查詢結果映射為pojo,但需要pojo的屬性名和sql查詢的列名一緻方可映射成功

如果sql查詢字段名和pojo的屬性名不一緻,可以通過resultMap将字段名和屬性名作一個對應關系 ,resultMap實質上還需要将查詢結果映射到pojo對象中

resultMap可以實作将查詢結果映射為複雜類型的pojo,比如在查詢結果映射對象中包括pojo和list實作一對一查詢和一對多查詢

定義resultMap

由于上邊的mapper.xml中sql查詢列和Users.java類屬性不一緻,需要定義resultMap:userListResultMap将sql查詢列和Users.java類屬性對應起來

MyBatis入門介紹(二)

​​

​<id />:​

​​此屬性表示查詢結果集的唯一辨別,非常重要。如果是多個字段為複合唯一限制則定義多個​

​<id />​

​​。

Property:表示User類的屬性。

Column:表示sql查詢出來的字段名。

Column和property放在一塊兒表示将sql查詢出來的字段映射到指定的pojo類屬性上。

​​

​<result />:​

​普通結果,即pojo的屬性。

3、動态sql

3.1、if語句

<select id="findUserList" parameterType="user" resultType="user">
  select * from user 
  where 1=1 
  <if test="id!=null">
  and id=#{id}
  </if>
  <if test="username!=null and username!=''">
  and username like '%${username}%'
  </if>
</select>      

3.2、Where語句

<select id="findUserList" parameterType="user" resultType="user">
  select * from user 
  <where>
  <if test="id!=null and id!=''">
  and id=#{id}
  </if>
  <if test="username!=null and username!=''">
  and username like '%${username}%'
  </if>
  </where>
</select>      

where語句可以自動處理第一個and

3.3、foreach語句

向sql傳遞數組或List,mybatis使用foreach解析,如下:

傳入多個id查詢使用者資訊,用下邊兩個sql實作:

SELECT * FROM USERS WHERE username LIKE '%張%' AND (id =10 OR id =89 OR id=16)
SELECT * FROM USERS WHERE username LIKE '%張%'  id IN (10,89,16)      

在pojo中定義list屬性ids存儲多個使用者id,并添加getter/setter方法

<if test="ids!=null and ids.size>0">
    <foreach collection="ids" open=" and id in(" close=")" item="id" separator="," >
      #{id}
    </foreach>
</if>      

4、Sql重複片段

Sql中可将重複的sql提取出來,使用時用include引用即可,最終達到sql重用的目的,如下:

<select id="findUserList" parameterType="user" resultType="user">
  select * from user 
  <where>
  <include refid="query_user_where"/>
  </where>
</select>

<sql id="query_user_where">
  <if test="id!=null and id!=''">
    and id=#{id}
  </if>
  <if test="username!=null and username!=''">
    and username like '%${username}%'
  </if>
</sql>      

注意:如果引用其它mapper.xml的sql片段,則在引用時需要加上namespace,如下:

<include refid="namespace.sql片段”/>      

5、查詢所有訂單資訊,關聯查詢下單使用者資訊

使用resultType,定義訂單資訊po類,此po類中包括了訂單資訊和使用者資訊:

public class OrdersCustom extends Orders {

  private String username;// 使用者名稱
  private String address;// 使用者位址
get/set。。。。
}      

OrdersCustom類繼承Orders類後OrdersCustom類包括了Orders類的所有字段,隻需要定義使用者的資訊字段即可

<!-- 查詢所有訂單資訊 -->
<select id="findOrdersList" resultType="cn.itcast.mybatis.po.OrdersCustom">
SELECT
orders.*,
user.username,
user.address
FROM
orders, user
WHERE orders.user_id = user.id 
</select>      

小結:定義專門的po類作為輸出類型,其中定義了sql查詢結果集所有的字段。此方法較為簡單,企業中使用普遍

另外一種方法:使用resultMap,定義專門的resultMap用于映射一對一查詢結果。

在Orders類中加入User屬性,user屬性中用于存儲關聯查詢的使用者資訊,因為訂單關聯查詢使用者是一對一關系,是以這裡使用單個User對象存儲關聯查詢的使用者資訊。

xml檔案:

<!-- 查詢訂單關聯使用者資訊使用resultmap -->
<resultMap type="cn.itheima.po.Orders" id="orderUserResultMap">
  <id column="id" property="id"/>
  <result column="user_id" property="userId"/>
  <result column="number" property="number"/>
  <result column="createtime" property="createtime"/>
  <result column="note" property="note"/>
  <!-- 一對一關聯映射 -->
  <!-- 
  property:Orders對象的user屬性
  javaType:user屬性對應 的類型
   -->
  <association property="user" javaType="cn.itcast.po.User">
    <!-- column:user表的主鍵對應的列  property:user對象中id屬性-->
    <id column="user_id" property="id"/>
    <result column="username" property="username"/>
    <result column="address" property="address"/>
  </association>
</resultMap>
<select id="findOrdersWithUserResultMap" resultMap="orderUserResultMap">
  SELECT
    o.id,
    o.user_id,
    o.number,
    o.createtime,
    o.note,
    u.username,
    u.address
  FROM
    orders o
  JOIN `user` u ON u.id = o.user_id
</select>      

這裡resultMap指定orderUserResultMap。

association:表示進行關聯查詢單條記錄

property:表示關聯查詢的結果存儲在cn.itcast.mybatis.po.Orders的user屬性中

javaType:表示關聯查詢的結果類型

​​

​<id property="id" column="user_id"/>:​

​​查詢結果的user_id列對應關聯對象的id屬性,這裡是​

​<id />​

​​表示user_id是關聯查詢對象的唯一辨別。

​​

​<result property="username" column="username"/>:​

​查詢結果的username列對應關聯對象的username屬性

<resultMap type="cn.itheima.po.user" id="userOrderResultMap">
  <!-- 使用者資訊映射 -->
  <id property="id" column="id"/>
  <result property="username" column="username"/>
  <result property="birthday" column="birthday"/>
  <result property="sex" column="sex"/>
  <result property="address" column="address"/>
  <!-- 一對多關聯映射 -->
  <collection property="orders" ofType="cn.itheima.po.Orders">
    <id property="id" column="oid"/>  
        <!--使用者id已經在user對象中存在,此處可以不設定-->
    <!-- <result property="userId" column="id"/> -->
    <result property="number" column="number"/>
    <result property="createtime" column="createtime"/>
    <result property="note" column="note"/>
  </collection>
</resultMap>
<select id="getUserOrderList" resultMap="userOrderResultMap">
  SELECT
  u.*, o.id oid,
  o.number,
  o.createtime,
  o.note
  FROM
  `user` u
  LEFT JOIN orders o ON u.id = o.user_id
</select>