05-MyBatis_关联映射之一对多

来源:互联网 发布:java国家精品课程 编辑:程序博客网 时间:2024/05/18 02:37
一对多: 一个人 对应 多个订单

0. 源码下载

    MyBatis_关系映射之一对多-src.zip

1. 表


 1.1 表名和字段

 
  person(personId,personName,personAddress,personTel)
  orders(orderId,ordreNumber,orderPrice,pid)
 

 1.2 SQL脚本

USE mybatis;CREATE TABLE person(  personId VARCHAR(36) PRIMARY KEY,  personName VARCHAR(64),  personAddress VARCHAR(128),  personTel VARCHAR(11));-- order 在MySQL中是关键字CREATE TABLE orders(  orderId VARCHAR(36) PRIMARY KEY,  orderNumber VARCHAR(20),  orderPrice INT,  pid VARCHAR(36));INSERT INTO person VALUES('001', 'Jack', 'Wuhan', '1234567');INSERT INTO orders VALUES('O_00001', '00001', 100, '001');INSERT INTO orders VALUES('O_00002', '00002', 200, '001');SELECT p.*, o.*FROM person p   JOIN orders o ON (p.personId=o.pid)WHERE p.personId = '001'

2. 实体

public class Person {    private String id;    private String name;    private String address;    private String tel;        List<Order> orders;    // get / set 已省略}    public class Order {        private String id;    private String number;    private Integer price;    // get / set 已省略}    


3. 总配置 

<configuration><typeAliases><typeAlias type="com.hehe.mybatis.domain.Person" alias="Person"/><typeAlias type="com.hehe.mybatis.domain.Order" alias="Order"/></typeAliases><environments default="development"><environment id="development"><transactionManager type="JDBC" /><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost/mybatis" /><property name="username" value="root" /><property name="password" value="root" /></dataSource></environment></environments><mappers><!-- 映射文件的位置  --><mapper resource="com/hehe/mybatis/domain/Person.xml" /><mapper resource="com/hehe/mybatis/domain/Order.xml" /></mappers></configuration>



4. 映射文件

<mapper namespace="com.hehe.mybatis.domain.Person"><resultMap type="Person" id="personMap"><id column="personId" property="id"/><result column="personName" property="name"/><result column="personAddress" property="address"/><result column="personTel" property="tel"/><!-- 一对多 --><!-- List<Order> orders;property : 实体属性名(集合名)ofType : 集合 中元素的类型 --><collection property="orders" ofType="Order"><id column="orderId" property="id"/><result column="orderNumber" property="number"/><result column="orderPrice" property="price"/></collection></resultMap><select id="selectPersonById" parameterType="string" resultMap="personMap">SELECT p.*, o.*FROM person p   JOIN orders o ON (p.personId=o.pid)WHERE p.personId = #{id}</select></mapper>


<mapper namespace="com.hehe.mybatis.domain.Order"><resultMap type="Order" id="orderMap"><id column="orderId" property="id"/><result column="orderNumber" property="number"/><result column="orderPrice" property="price"/></resultMap></mapper>


5. 测试

/*<select id="selectPersonById" parameterType="string" resultMap="orderMap">SELECT p.*, o.* FROM person p   JOIN orders o ON (p.personId=o.pid);where p.id = #{id}</select> */@Test public void testSelectPersonById() throws Exception {SqlSession session = sqlSessionFactory.openSession();List<Person> persons = session.selectList("com.hehe.mybatis.domain.Person.selectPersonById", "001");// 在此处打断点查看for (Person p : persons) {System.out.println(p.getId() + "\t" +p.getName() + "\t" +p.getAddress() + "\t" +p.getTel() );for (Order o : p.getOrders()) {System.out.println(o.getId() + "\t" +o.getNumber() + "\t" +o.getPrice() );}}session.close();}




原创粉丝点击