MyBatis(3)-关系映射

2019-04-28  本文已影响0人  炖土豆块

1.关系映射1:N
1)新建数据库表customer及orders表


customer表结构.png
orders表结构.png

2)创建实体类

package com.capgemini.entity;

import java.util.List;

/*
实体类,Customer
*/
public class Customer {

   public List<Orders> orders;
   public Integer id;
   public String name;

   public List<Orders> getOrders() {
       return orders;
   }

   public void setOrders(List<Orders> orders) {
       this.orders = orders;
   }

   public Integer getId() {
       return id;
   }

   public void setId(Integer id) {
       this.id = id;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   @Override
   public String toString() {
       return "Customer{" +
               "orders=" + orders +
               ", id=" + id +
               ", name='" + name + '\'' +
               '}';
   }
}
package com.capgemini.entity;

/*
实体类Order
 */
public class Orders {
    private Integer id;
    private String sn;
    private String remark;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getSn() {
        return sn;
    }

    public void setSn(String sn) {
        this.sn = sn;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }
}

3)增加映射文件


sqlMapperConfig中增加.png
<?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.capgemini.mapper.CustomerMapper">

    <resultMap id="customerRM" type="com.capgemini.entity.Customer">
        <id property="id" column="id"></id>
        <result property="name" column="name"></result>
    </resultMap>

    <!-- 配置关联关系 1:N-->
    <!-- id代表resultMap这个1:N关联关系的名称,type则是1:N中的1那个表示对应的集合元素,
          extends表示1:N中1那个表对应的集合元素-->
    <resultMap id="customerOrderRM" type="com.capgemini.entity.Customer" extends="customerRM">
        <!-- collection是用于1:N的,collection中代表的是N的那个表-->
        <!-- property代表实体中的 List 对象属性名称,ofType是集合元素类型-->
        <collection property="orders" ofType="com.capgemini.entity.Orders">
            <id property="id" column="id"></id>
            <result property="sn" column="sn"></result>
            <result property="remark" column="remark"></result>
        </collection>
    </resultMap>

    <select id="find" parameterType="Map" resultMap="customerOrderRM">
        SELECT c.name,o.sn,o.remark
        FROM (SELECT id,name FROM customer) c
        LEFT JOIN (SELECT id,sn,remark,customer_id FROM orders) o
        ON c.id = o.customer_id
        WHERE c.name = #{customerName}
    </select>
</mapper>

4)测试类

package com.capgemini.test;

import com.capgemini.entity.Customer;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;

public class MyBatisTest {
    @Test
    public void test06() throws IOException {
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("sqlMapperConfig.xml"));
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Map map = new HashMap();
        map.put("customerName","charies");
        List<Customer> customers = sqlSession.selectList("com.capgemini.mapper.CustomerMapper.find",map);
        for (Customer c:customers
             ) {
            System.out.println(c);
        }
    }
}

测试结果:


image.png

5)文件结构:


image.png
6)总结
对于1:N关系,在resultMap中增加<collection>属性进行配置;其中<collection>配置的是n所对应的表(实体类);其中extends对应的是1对应的表对应的集合元素;
上一篇下一篇

猜你喜欢

热点阅读