mybatis的一对多(转)
一 建表,提供初始数据。
表说明:t_customer为客户信息表;t_orders为订单表。一个客户可以有多个订单,一个订单只属于一个客户。多方使用外键(t_orders表的customer_id)来约束。这里并没有建立强制的外键约束,做更新,删除操作时会很麻烦。
package com.alex.app.entity; import java.util.List; /** * 客户信息 * @author leileiyuan * */ public class Customer { private Integer id; private String name; private String cellPhone; //一对多 private List<Orders> orders; public List<Orders> getOrders() { return orders; } public void setOrders(List<Orders> orders) { this.orders = orders; } // 略 getter setter方法 @Override public String toString() { return "Customer [id=" + id + ", name=" + name + ", cellPhone=" + cellPhone + "]"; } }
Orders
package com.alex.app.entity; /** * 客户订单信息 * @author leileiyuan * */ public class Orders { private Integer id; private String number; private String address; // 多对一 private Customer customer; public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } // 略 getter setter方法 @Override public String toString() { return "Orders [id=" + id + ", number=" + number + ", address=" + address + ", customer=" + customer + "]"; } }
三 多对一映射
1)问题简单分析
从orders 到 customer 多对一的关联关联。
就是说 我们要查询orders的信息,然后经该orders可以导航到它对应的customer
考虑这个sql
@Test public void test() { SqlSession session = null; try { session = MyBatisUtil.openSession(); OrdersDao ordersDao = session.getMapper(OrdersDao.class); Orders orders = ordersDao.selectById(1); System.out.println(orders); } catch (Exception e) { e.printStackTrace(); session.rollback(); }finally{ MyBatisUtil.coloseSession(session); } }
结果如下
//一对多 private List<Orders> orders;
ofType,是集合中的放置的内容的类型,这里集合放的东西是Orders对象
4)测试下
@Test public void testSelectCustomer() { SqlSession session = null; try { session = MyBatisUtil.openSession(); CustomerDao customerDao = session.getMapper(CustomerDao.class); Customer customer = customerDao.selectById(1); System.out.println(customer); List<Orders> orders = customer.getOrders(); System.out.println(orders); } catch (Exception e) { e.printStackTrace(); }finally{ MyBatisUtil.coloseSession(session); } }
结果
- DEBUG 2015-05-10 08:59:28,640 org.apache.ibatis.logging.jdbc.BaseJdbcLogger: ==> Preparing: select c.id cid, c.name name, c.cell_phone cellPhone, o.id oid, o.number number, o.address address from t_customer c, t_orders o where c.id = o.customer_id and c.id = ?
- DEBUG 2015-05-10 08:59:28,687 org.apache.ibatis.logging.jdbc.BaseJdbcLogger: ==> Parameters: 1(Integer)
- DEBUG 2015-05-10 08:59:28,718 org.apache.ibatis.logging.jdbc.BaseJdbcLogger: <== Total: 2
- Customer [id=1, name=bing, cellPhone=null, orders=[Orders [id=1, number=GASDF235, address=null, customer=null], Orders [id=2, number=JHGFVSD34, address=null, customer=null]]]
- [Orders [id=1, number=GASDF235, address=null, customer=null], Orders [id=2, number=JHGFVSD34, address=null, customer=null]]
5)Customer.xml完整内容。t_orders和t_customer的id列,要使用别名来区
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.alex.app.dao.CustomerDao">
- <resultMap type="Customer" id="CustomerResultMap">
- <id property="id" column="cid" />
- <result property="name" column="name" />
- <collection property="orders" javaType="ArrayList" ofType="Orders">
- <id property="id" column="oid" />
- <result property="number" column="number" />
- </collection>
- </resultMap>
- <select id="selectById" parameterType="int" resultMap="CustomerResultMap">
- select
- c.id cid,
- c.name name,
- c.cell_phone cellPhone,
- o.id oid,
- o.number number,
- o.address address
- from t_customer c, t_orders o
- where c.id = o.customer_id
- and c.id = #{id}
- </select>
- </mapper>
相关推荐
houmenghu 2020-11-17
我心似明月 2020-11-09
oraclemch 2020-11-06
ltd00 2020-09-12
康慧欣 2020-09-10
waveclouds 2020-09-04
蓝色深海 2020-09-15
jincheng 2020-09-01
思君夜未眠 2020-08-25
取个好名字真难 2020-08-06
歆萌 2020-08-03
阳光之吻 2020-08-03
婷婷小屋 2020-07-28
solarspot 2020-07-28
MLXY 2020-07-26
dxbjfu0 2020-07-26
songbinxu 2020-07-19
83520298 2020-07-06