mybatis (高級映射 緩存 延遲加載)
1 一對一查詢映射的pojo
創建pojo包括 訂單信息和用戶信息,resultType才可以完成映射。
一個訂單對應一個用戶 因此在訂單實體類中 我們應該加入一個用戶屬性 實體類如下:
public class Order { private Integer orid; private Integer userid; private Double ordermoney; private User user; }
接下來 我們該在mapper.xml中利用(resultMap 和association 標簽配置User)
<resultMap type="com.login.entity.Order" id="userAndOrder"> <result property="orid" column="order_id" /> <result property="userid" column="user_id"/> <result property="ordermoney" column="order_money"/> <association property="user" javaType="com.login.entity.User"> <result property="username" column="user_name"/> <result property="userpwd" column="user_pwd"/> <result property="userphone" column="user_phone"/> </association> </resultMap> <select id="findOrderAndUserById" resultMap="userAndOrder"> SELECT * FROM qxglxt.order orde JOIN qxglxt.user u ON orde.user_id=u.user_id where u.user_id=2 </select>
測試:
public static void main(String[] args) { try { Reader reader=Resources.getResourceAsReader("SqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader); SqlSession sqlSession=sqlSessionFactory.openSession(); //訂單 OrderMapper orderMapper=sqlSession.getMapper(OrderMapper.class); Order ord=orderMapper.findOrderById(1);
2 一對多查詢映射的pojo
創建pojo包括 訂單信息和用戶信息,resultType才可以完成映射。
一個用戶應該有多個訂單 因此在用戶實體類中我們應該加入List<Order> list 屬性 實體類如下:
public class User {
private String username;
private String userpwd;
private String userphone;
private List<Order> list;
}
接下來 我們該在mapper.xml中利用(resultMap 和connection 標簽配置list)
<resultMap type="com.login.entity.User" id="uid"> <result property="username" column="user_name"/> <result property="userpwd" column="user_pwd"/> <result property="userphone" column="user_phone"/> <collection property="list" ofType="com.login.entity.Order"> <result property="orid" column="order_id" /> <result property="userid" column="user_id"/> <result property="ordermoney" column="order_money"/> </collection> <!-- 名字不一樣必須配置 名字一樣 可以不配的 --> </resultMap> <select id="findUserById" resultMap="uid"> select * from user where user_id=#{id} </select>
一對一的xml配置詳細介紹:
private User user;
一對多的xml配置詳細介紹:
private List<Order> list;
3 延遲加載
·在進行數據查詢時,為了提高數據庫查詢性能,盡量使用單表查詢,因為單表查詢比多表關聯查詢速度要快。如果查詢單表就可以滿足需求,一開始先查詢單表,當需要關聯信息時,再關聯查詢,當需要關聯信息再查詢這個叫延遲加載。
mybatis中resultMap提供延遲加載功能,通過resultMap配置延遲加載。
例子 : 當查詢一個訂單時 如果沒有點擊訂單詳情的東西 就只需要將訂單表中的信息顯示出來 而訂單詳情的東西則等到調用的時候再顯示
首先應該配置核心xml SqlMapConfig.xml 開啟延遲加載
<!-- 全局配置參數 --> <settings> <!-- 延遲加載總開關 --> <setting name="lazyLoadingEnabled" value="true" /> <!-- 設置按需加載 如果存在多個懶加載 就是說 訂單中有用戶 用戶中有地址 那麽如果設置為true 就會在加載用戶的時候吧用戶的地址也給加載了 如果為false 就是使用到才加載 --> <setting name="aggressiveLazyLoading" value="false" /> </settings>
需求:
查詢訂單及用戶的信息,一對一查詢。剛開始只查詢訂單信息,當需要用戶時調用 Orders類中的getUser()方法執行延遲加載 ,向數據庫發出sql。
mapper.xml
resultMap
findUserByid 方法: user_id (關聯字段)
<select id="findOrderAndUserById" resultMap="userAndOrder">
SELECT * FROM qxglxt.order orde JOIN qxglxt.user u ON orde.user_id=u.user_id where u.user_id=2
</select>
mapper.java
測試代碼
4 緩存
正如大多數持久層框架一樣,MyBatis 同樣提供了一級緩存和二級緩存的支持
- mybatis的一級緩存是指SqlSession。一級緩存的作用域是一個SqlSession。Mybatis默認開啟一級緩存。
2.Mybatis的二級緩存是指mapper映射文件。二級緩存的作用域是同一個namespace下的mapper映射文件內容,多個SqlSession共享。Mybatis需要手動設置啟動二級緩存。
對於兩級緩存來說 使用close方法後則往緩存區存入數據 使用commit 後 就會清空緩存區的數據,在執行增加,刪除,修改操作後必須要使用commit提交事務 。
3. 對於緩存數據更新機制,當某一個作用域(一級緩存Session/二級緩存Namespaces)的進行了 C/U/D 操作後,默認該作用域下所有 select 中的緩存將被clear。
mybatis (高級映射 緩存 延遲加載)