您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Mybatis的多對(duì)多舉例分析”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Mybatis的多對(duì)多舉例分析”吧!
多對(duì)多
一張訂單里 可以包含多種產(chǎn)品,一種產(chǎn)品 可以出現(xiàn)在多張訂單里
為了維系多對(duì)多關(guān)系,必須要一個(gè)中間表。 在這里我們使用訂單項(xiàng)(OrderItem)表來作為中間表
(1)建立order_表,增添數(shù)據(jù)
(2)創(chuàng)建實(shí)體類Order和OrderItem
Order.java中
private int id; private String code; List<OrderItem> orderItems; setter,getter
OrderItem.java中
private int id; private int number; private Order order; private Product product; setter,getter
(3)創(chuàng)建映射文件Order.xml,Product.xml和OrderItem.xml
Order.xml中
<mapper namespace="com.how2java.pojo"> <resultMap type="Order" id="orderBean"> <id column="oid" property="id" /> <result column="code" property="code" /> <collection property="orderItems" ofType="OrderItem"> <id column="oiid" property="id" /> <result column="number" property="number" /> <association property="product" javaType="Product"> <id column="pid" property="id"/> <result column="pname" property="name"/> <result column="price" property="price"/> </association> </collection> </resultMap> <select id="listOrder" resultMap="orderBean"> select o.*,p.*,oi.*, o.id 'oid', p.id 'pid', oi.id 'oiid', p.name 'pname' from order_ o left join order_item_ oi on o.id =oi.oid left join product_ p on p.id = oi.pid </select> <select id="getOrder" resultMap="orderBean"> select o.*,p.*,oi.*, o.id 'oid', p.id 'pid', oi.id 'oiid', p.name 'pname' from order_ o left join order_item_ oi on o.id =oi.oid left join product_ p on p.id = oi.pid where o.id = #{id} </select> </mapper>
Product.xml中
<mapper namespace="com.how2java.pojo"> <resultMap type="Product" id="productBean"> <id column="pid" property="id" /> <result column="pname" property="name" /> <result column="price" property="price" /> <!-- 多對(duì)一的關(guān)系 --> <!-- property: 指的是屬性名稱, javaType:指的是屬性的類型 --> <association property="category" javaType="Category"> <id column="cid" property="id"/> <result column="cname" property="name"/> </association> </resultMap> <select id="listProduct" resultMap="productBean"> select c.*, p.*, c.id 'cid', p.id 'pid', c.name 'cname', p.name 'pname' from category_ c left join product_ p on c.id = p.cid </select> <select id="getProduct" resultMap="productBean"> select c.*, p.*, c.id 'cid', p.id 'pid', c.name 'cname', p.name 'pname' from category_ c left join product_ p on c.id = p.cid where p.id = #{id} </select> </mapper>
OrderItem.xml中
<mapper namespace="com.how2java.pojo"> <insert id="addOrderItem" parameterType="OrderItem"> insert into order_item_ values(null,#{order.id},#{product.id},#{number}) </insert> <insert id="deleteOrderItem" parameterType="OrderItem"> delete from order_item_ where oid = #{order.id} and pid = #{product.id} </insert> </mapper>
(3)修改mybatis-config.xml,添加對(duì)于Order.xml和OrderItem的映射
<mappers> <mapper resource="com/how2java/pojo/Category.xml"/> <mapper resource="com/how2java/pojo/Product.xml"/> <mapper resource="com/how2java/pojo/Order.xml"/> <mapper resource="com/how2java/pojo/OrderItem.xml"/> </mappers>
(4)聯(lián)合查詢
private static void listOrder(SqlSession session) { List<Order> os = session.selectList("listOrder"); for (Order o : os) { System.out.println(o.getCode()); List<OrderItem> ois = o.getOrderItems(); for (OrderItem oi : ois) { System.out.format("\t%s\t%f\t%d%n", oi.getProduct().getName(), oi.getProduct().getPrice(), oi.getNumber()); } } }
(5)建立關(guān)系,首先通過id分別獲取Order對(duì)象和Product對(duì)象,然后創(chuàng)建一個(gè)新的OrderItem對(duì)象,
接著設(shè)置Order,設(shè)置Product,設(shè)置數(shù)量,最后調(diào)用"addOrderItem" 對(duì)應(yīng)的sql語句插入數(shù)據(jù)。
private static void addOrderItem(SqlSession session) { Order o1 = session.selectOne("getOrder", 1); Product p6 = session.selectOne("getProduct", 6); OrderItem oi = new OrderItem(); oi.setProduct(p6); oi.setOrder(o1); oi.setNumber(200); session.insert("addOrderItem", oi); }
(6)刪除關(guān)系,通過訂單id(1)和產(chǎn)品id(6)進(jìn)行刪除,其實(shí)所謂的刪除關(guān)系,就是刪除掉OrderItem記錄。
private static void deleteOrderItem(SqlSession session) { Order o1 = session.selectOne("getOrder",1); Product p6 = session.selectOne("getProduct",6); OrderItem oi = new OrderItem(); oi.setProduct(p6); oi.setOrder(o1); session.delete("deleteOrderItem", oi); }
(7)修改關(guān)系,多對(duì)多不存在修改關(guān)系的做法,就是刪除舊的,然后新增一條即達(dá)到修改的效果。
注:提示,通過Mybatis執(zhí)行多條sql語句需要增加一個(gè)參數(shù):allowMultiQueries
<property name="url" value="jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8&allowMultiQueries=true"/>
到此,相信大家對(duì)“Mybatis的多對(duì)多舉例分析”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。