Mybatis_基礎學習_03

Mybatis 的連接池技術

<dataSource type="POOLED">
    <!-- 配置連接數據庫的4個基本信息 -->
    <property name="driver" value="${jdbc.driver}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</dataSource>
  • type屬性值

UNPOOLED : 不使用連接池的數據源,直接使用傳統技術新建
POOLED : 使用連接池的數據源
JNDI : 使用 JNDI 實現的數據源

POOLED 使用連接池的數據源: 在使用時會引用一個UNPOOLED對象, 在添加鏈接到連接池時調用

  • 有關POOLED 底層代碼邏輯:
獲取連接時,首先在空閒池尋找是否有空閒的鏈接
-- 有,直接獲取返回
-- 沒有,判斷活動池的鏈接數量是否已經達到最大鏈接數量
   -- 沒有達到,使用UNPOOLED的方法新建一個鏈接並返回
   -- 已經達到,查詢最先開始使用的鏈接,判斷使用時間是否超過20S
	-- 沒有超過,進入等待
	-- 已經超過,強制終止連接,此時鏈接空閒,將連接返回
  • 活動池的鏈接在使用完畢後會返回到空閒池 ,有被調用則存在於活動池

動態sql的應用

更新語句的範例:

<select id="updateUser" parameterType="User">
    UPDATE USER
    <trim prefix="set" suffixOverrides=",">
        <if test="_username!=null and _username!=''">
            username =#{_username} ,
        </if>
        <if test="_birthday!=null">
            birthday =#{_birthday} ,
        </if>
        <if test="_sex!=null and _sex!=''">
            sex =#{_sex} ,
        </if>
        <if test="_address!=null and _address!=''">
            address =#{_address}
        </if>
    </trim>
    where id =#{_id};
</select>

查詢語句範例
foreach標籤導入集合參數的使用

<!--查詢多個用戶-->
<select id="findOneByIds"  resultMap="userMap">
    SELECT * FROM USER
    <where>a
    <foreach collection="list" open="id in(" close=")" item="id" separator=",">
        #{id}
    </foreach>
    </where>
</select>

模糊查詢

<!--模糊查詢-->
<select id="findOneByCondition" resultMap="userMap">
    SELECT * FROM USER WHERE username like '%${value}%';
    <where>
        <if test="username!=null and username!=''">
            and username = #{_username}
        </if>
        <if test="address!=null and address!=''">
            and address = '${_address}'
        </if>
    </where>
</select>

Mybatis 表關係
表之間的關係有幾種:
一對多
多對一
一對一
多對多
一對一關係的數據庫查詢

<resultMap id="accountMap" type="account">
    <id property="id" column="id"></id>
    <result property="uid" column="uid"></result>
    <result property="money" column="money"></result>
    <association property="user" javaType="User">
        <!--主鍵字段的對應使用id標籤-->
        <id property="_id" column="uuida"></id>
        <!--a非主鍵字段的對應使用result標籤-->
        <result property="_username" column="username"></result>
        <result property="_birthday" column="birthday"></result>
        <result property="_sex" column="sex"></result>
        <result property="_address" column="address"></result>
    </association>
</resultMap>

一對多關係的數據庫查詢

<resultMap id="userMap" type="lorihen.domain.User">
    <!--主鍵字段的對應使用id標籤-->
    <id property="_id" column="id"></id>
    <!--非主鍵字段的對應使用result標籤-->
    <result property="_username" column="username"></result>
    <result property="_birthday" column="birthday"></result>
    <result property="_sex" column="sex"></result>
    <result property="_address" column="address"></result>
    <collection property="accounts" ofType="Account">
        <id property="id" column="aid"></id>
        <result property="uid" column="uid"></result>
        <result property="money" column="money"></result>
    </collection>
</resultMap>

多對多關係的數據庫查詢
在resultMap標籤的編寫上和一對多比較並沒有什麼不同,不同的地方在於sql的編寫上,多對多的數據庫查詢需要引用一箇中間表.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章