一、MyBatis的Collections用法
MyBatis是一款非常流行的ORM框架,擁有著簡單易用,靈活性強等優點,Collection是MyBatis中非常重要的一個概念。集合元素可以是簡單類型,也可以是複雜類型,MyBatis中提供的Collection標籤,可以在映射文件和SQL中方便地操作集合。下面我們就來分別看看它的用法吧!
二、MyBatis的Collection用法
Collection標籤包含了兩個屬性property和ofType。其中property是指要操作的集合元素屬性,而ofType則指的是集合元素的類型。用法如下:
// XML映射文件 <select id="getOrderById" resultMap="orderMap"> SELECT o.id, o.order_no, o.total_price, u.user_name FROM orders o, users u WHERE o.user_id = u.id and o.id = #{id}; <collection property="orderItems" ofType="Item"> SELECT i.id, i.product_name, i.price, i.quantity FROM order_items oi INNER JOIN items i ON oi.item_id = i.id WHERE oi.order_id = #{id} </collection> </select> // Java代碼 Order order = sqlSession.selectOne("getOrderById", 1);
在上面的代碼中,我們可以看到<collection>標籤中的property和ofType屬性分別指的是「orderItems」和「Item」,這意味著我們可以方便地操作order對象中的orderItems集合,而ofType指定的是集合元素的類型是Item。
三、MyBatis的Collection的column
在mybatis的collection標籤中,還有一項非常重要的屬性就是column。column屬性的作用是用於指定集合元素在資料庫中對應的列名,用法如下:
// XML映射文件 <select id="getOrderById" resultMap="orderMap"> SELECT o.id, o.order_no, o.total_price, u.user_name FROM orders o, users u WHERE o.user_id = u.id and o.id = #{id}; <collection property="orderItems" ofType="Item"> SELECT oi.order_id, i.id, i.product_name, i.price, i.quantity FROM order_items oi INNER JOIN items i ON oi.item_id = i.id WHERE oi.order_id = #{id} </collection> </select> // Java代碼 Order order = sqlSession.selectOne("getOrderById", 1);
在上面的代碼中,我們可以注意到在<collection>標籤中的column屬性指定了order_id,表示將order_items表中的order_id列作為集合元素中orderId屬性的值進行賦值。
四、MyBatis的Collection的屬性
在使用MyBatis的Collection標籤時,還有一些其他的屬性需要我們了解。比如說,我們可以通過select屬性來指定一個查詢語句,這個查詢語句將返回一個集合元素列表。此外,我們還可以通過fetchSize來設置每頁數據的數量,防止一次載入大量數據導致內存溢出等問題。
五、MyBatis的Collection處理父子關係
在處理資料庫的時候,往往存在一些父子關係。MyBatis提供了collection標籤處理這種情況。比如說,我們可以在Order對象中包含多個OrderItem對象,而每個OrderItem對象中還可以包含多個其他對象,如下所示:
// Java代碼 public class Order { private int id; private String orderNo; private List<OrderItem> orderItems; // 省略了其它的get/set方法 } public class OrderItem { private int id; private String productName; private BigDecimal price; private int quantity; private List<Item> items; // 省略了其它的get/set方法 } public class Item { private int id; private String name; private BigDecimal price; // 省略了其它的get/set方法 }
在使用MyBatis進行操作的時候,我們可以通過collection標籤來傳遞參數,如下所示:
// XML映射文件 <select id="getOrderById" resultMap="orderMap"> SELECT o.id, o.order_no, o.total_price, u.user_name FROM orders o, users u WHERE o.user_id = u.id and o.id = #{id}; <collection property="orderItems" ofType="OrderItem"> SELECT oi.id, oi.product_name, oi.price, oi.quantity FROM order_items oi WHERE oi.order_id = #{id}; <collection property="items" ofType="Item"> SELECT i.id, i.product_name, i.price FROM item i WHERE i.order_item_id=#{id} </collection> </collection> </select>
在上面的代碼中,我們可以看到MyBatis中使用了兩個<collection>標籤,第一個標籤用於獲取Order對象中的所有OrderItem對象,而第二個標籤則用於獲取每個OrderItem對象中的所有Item對象。我們可以很方便地構造出類似於以上的父子關係,達到較為良好的代碼結構。
六、MyBatis的Collection資料庫連接
在使用MyBatis進行操作的時候,往往需要連接資料庫。MyBatis提供了多種不同的連接池來管理資料庫連接,比如說Druid、C3P0、HikariCP、DBCP等等,我們可以根據需要來選擇不同的連接池。
// 配置連接池 <dataSource type="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=false"/> <property name="username" value="root"/> <property name="password" value="root"/> <!-- 配置初始化連接數量及最大連接數量 --> <property name="initialSize" value="5"/> <property name="maxActive" value="50"/> </dataSource>
七、MyBatis的Collection標籤的屬性
在MyBatis中,我們還需要注意<collection>標籤中的orderBy屬性。orderBy屬性是用來指定SQL查詢結果的排序規則的,用法如下:
// XML映射文件 <select id="getOrderById" resultMap="orderMap"> SELECT o.id, o.order_no, o.total_price, u.user_name FROM orders o, users u WHERE o.user_id = u.id and o.id = #{id}; <collection property="orderItems" ofType="OrderItem" orderBy="id desc"> SELECT oi.id, oi.product_name, oi.price, oi.quantity FROM order_items oi WHERE oi.order_id = #{id}; </collection> </select>
在上面的代碼中,我們指定了排序規則為按照id降序排列。這個屬性很實用,尤其在我們需要對結果集排序的時候。
八、MyBatis的Collection怎麼用
最後,我們可以來總結一下MyBatis的Collection怎麼用。首先,我們需要在XML映射文件中使用<collection>標籤操作集合,指定要操作的集合元素的屬性和類型。其次,我們還可以為集合元素指定對應的資料庫列名,用orderBy屬性來設置結果集排序規則,使用select和fetchSize屬性來進行分頁等操作。總的來說,MyBatis的Collection功能十分強大,毫無疑問,它是非常有幫助的一個功能。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/157792.html