一、表之間的關聯關系:
- 關聯關系是有方向的,
1、四種關聯關系:
1)一對多關聯:一個老師可以教多個學生,多個學生只有一個老師來教,站在老師方,就是一對多關聯,
2)多對一關聯:一個老師可以教多個學生,多個學生只有一個老師來教,站在學生方,就是多對一關聯,
3)一對一關聯:一個老師輔導一個學生,一個學生只請教一個老師,學生和老師是一對一,
4)多對多關聯:園區劃線的車位和園區的每一輛車,任意一個車位可以停任意一輛車,任意一車輛車可以停在任意一個車位上,
2、一對多關聯關系:
客戶和訂單就是典型的一對多關聯關系;
一個客戶名下可以有多個訂單;
客戶表是一方,訂單表是多方.客戶一中持有訂單的集合;
使用一對多的關聯關系,可以滿足查詢客戶的同時查詢該客戶名下的所有訂單.;
需求:根據客戶的id查詢客戶所有資訊并同時查詢該客戶名下的所有訂單
<!--
//customer表中的三個列
private Integer id;
private String name;
private Integer age;
//該客戶名下的所有訂單的集合
private List<Orders> ordersList;
-->
<resultMap id="customermap" type="customer">
<!--主鍵系結-->
<id property="id" column="cid"></id>
<!--非主鍵系結-->
<result property="name" column="name"></result>
<result property="age" column="age"></result>
<!--多出來的一咕嚕系結ordersList
Orders物體類:
private Integer id;
private String orderNumber;
private Double orderPrice;
-->
<collection property="ordersList" ofType="orders">
<!--主鍵系結-->
<id property="id" column="oid"></id>
<!--非主鍵系結-->
<result property="orderNumber" column="orderNumber"></result>
<result property="orderPrice" column="orderPrice"></result>
</collection>
</resultMap>
<select id="getById" parameterType="int" resultMap="customermap">
select c.id cid,name,age,o.id oid,orderNumber,orderPrice,customer_id
from customer c
left ,join orders o on c.id = o.customer_id
where c.id=#{id}
</select>
3、多對一關聯關系:
訂單和客戶就是多對一關聯;
站在訂單的方向查詢訂單的同時將客戶資訊查出;
訂單是多方,會持有一方的物件,客戶是一方;
需求:根據訂單的id查詢訂單所有資訊并同時查詢該訂單對應的用戶資訊
<mapper namespace="com.bjpowernode.mapper.OrdersMapper">
<!--
物體類
private Integer id;
private String orderNumber;
private Double orderPrice;
//關聯下此訂單的客戶資訊,多方持有一方的物件
private Customer customer;
-->
<resultMap id="ordersmap" type="orders">
<!--主鍵系結-->
<id property="id" column="oid"></id>
<!--非主鍵系結-->
<result property="orderNumber" column="orderNumber"></result>
<result property="orderPrice" column="orderPrice"></result>
<!--多出來的一咕嚕系結
private Integer id;
private String name;
private Integer age;
//該客戶名下的所有訂單的集合,一方持有多方的集合
private List<Orders> ordersList; //不用管
-->
<association property="customer" javaType="customer">
<id property="id" column="cid"></id>
<result property="name" column="name"></result>
<result property="age" column="age"></result>
</association>
</resultMap>
<select id="getById" parameterType="int" resultMap="ordersmap">
select o.id oid,orderNumber,orderPrice,customer_id,c.id cid,name,age
from orders o inner join customer c on o.customer_id = c.id
where o.id=#{id}
</select>
</mapper>
總結:無論是什么關聯關系,如果某方持有另一方的集合,則使用<collection>標簽完成映射,如果某方持有另一方的物件,則使用<association>標簽完成映射,
二、事務:
- 多個操作同時完成,或同時失敗稱為事務處理,事務有四個特性:一致性,持久性,原子性,隔離性.
在MyBatis框架中設定事務:
<!--程式員自己控制處理的提交和回滾-->
<transactionManager type="JDBC"></transactionManager>
//默認是手工提交事務,設定為false也是手工提交事務,如果設定為true,則為自動提交.
sqlSession = factory.openSession();
//設定為自動提交,在增刪改后不需要commit();
sqlSession = factory.openSession(true);
三、快取:
- MyBatis框架提供兩級快取,一級快取和二級快取,默認開啟一級快取,快取就是為了提交查詢效率,
1、使用快取后,查詢的流程:
查詢時先到快取里查,如果沒有則查詢資料庫,放快取一份,再回傳客戶端,下次再查詢的時候直接從快取回傳,不再訪問資料庫;
如果資料庫中發生commit()操作,則清空快取,
2、一級快取使用的是SqlSession的作用域,同一個sqlSession共享一級快取的資料,
二級快取使用的是mapper的作用域,不同的sqlSession只要訪問的同一個mapper.xml檔案,則共享二級快取作用域,
四、ORM:
- ORM(Object Relational Mapping):物件關系映射
1、MyBatis框架是ORM非常優秀的框架,
java語言中以物件的方式操作資料,存到資料庫中是以表的方式進行存盤,物件中的成員變數與表中的列之間的資料互換稱為映射,整個這套操作就是ORM,
2、持久化的操作:將物件保存到關系型資料庫中 ,將關系型資料庫中的資料讀取出來以物件的形式封裝
MyBatis是持久化層優秀的框架,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/472256.html
標籤:其他
上一篇:salesforce是什么
