MyBatis-13 结果映射


创建两张表

客户表和订单表

create table customer_tbl(
  id integer primary key auto_increment,
  name varchar(20)
);

create table order_tbl(
  id integer primary key auto_increment,
  name varchar(20),
  cid integer
);

创建映射文件

CustomerMapper.xml

<?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.cd.CustomerMapper">

    <resultMap type="Order" id="myOrder">
        <id property="id" column="id" />
        <result property="name" column="o_name" />
        <association property="customer" javaType="Customer" column="cid">
            <id property="id" column="id" />
            <result property="name" column="c_name" />
        </association>
    </resultMap>

    <select id="selectOrders" resultMap="myOrder">
        select o.id, o.name as o_name,
         c.name as c_name from
        customer_tbl c right join order_tbl as o on c.id = o.cid;
    </select>

</mapper>

客户和订单类

package com.cd;

public class Order {
    private int id;
    String name;
    Customer customer;

    public Order() {
        super();
    }

    public Order(int id, String name, Customer customer) {
        super();
        this.id = id;
        this.name = name;
        this.customer = customer;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    @Override
    public String toString() {
        return this.id+","+this.name+","+this.customer.name;
    }
}

package com.cd;

public class Customer {
    private int id;
    String name;

    public Customer() {
        super();
    }

    public Customer(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

接口

package com.cd;

import java.util.List;

public interface CustomerMapper {
    public List<Order> selectOrders();
}

测试

package com.cd;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class Test {

    public static void main(String[] args) {
        String resource = "com/cd/mybatis-config.xml";
        InputStream inputStream;
        try {
            inputStream = (InputStream) Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            CustomerMapper mapper = session.getMapper(CustomerMapper.class);

            List<Order> order_list = mapper.selectOrders();
            System.out.println(order_list);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


原文链接:http://codingdict.com/