mybatis一对多,多对一

假设两张表

person对order为一对多

实体类 

person

package com.kerwin.mybatis.pojo;
import java.util.List;
public class Person {
    private int id;
    private String name;
    private List<Orders> orderList;
    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 List<Orders> getOrderList() {
        return orderList;
    }
    public void setOrderList(List<Orders> orderList) {
        this.orderList = orderList;
    }
    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + "]";
    }
    public Person() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Person(int id, String name, List<Orders> orderList) {
        super();
        this.id = id;
        this.name = name;
        this.orderList = orderList;
    }
}

order

package com.kerwin.mybatis.pojo;

public class Orders {
private int id;
private double price;
private Person person;
public Person getPerson() {
return person;
}

public void setPerson(Person person) {
this.person = person;
}

public int getId() {
return id;
}

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

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

@Override
public String toString() {
return "Orders [id=" + id + ", price=" + price + "]";
}

public Orders() {
super();
// TODO Auto-generated constructor stub
}

}

 PersonMapper.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.kerwin.mybatis.pojo.Person">
    <resultMap type="com.kerwin.mybatis.pojo.Person" id="personreSultMap">
        <id column="p_id" property="id"/>
        <result column="name" property="name"/>
        <collection property="orderList" ofType="com.kerwin.mybatis.pojo.Orders" column="pid">
            <id column="o_id" property="id"/>
            <result column="price" property="price"/>
        </collection>
    </resultMap>
    <select id="selectPersonFetchOrder" parameterType="int" resultMap="personreSultMap" >
        select p.*,o.* from person p,orders o where o.pid=p.p_id and p.p_id=#{id}
    </select> 
</mapper>

OrderMpper.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.kerwin.mybatis.pojo.Orders">
    <resultMap type="com.kerwin.mybatis.pojo.Orders" id="OrdersResultMap">
        <id column="o_id" property="id"/>
        <result column="price" property="price"/>
        <association property="person" javaType="com.kerwin.mybatis.pojo.Person">
            <id column="p_id" property="id"/>
            <result column="name" property="name"/>
        </association>
    </resultMap>
    
    <select id="selectOrdersFetchPerson" resultMap="OrdersResultMap">
        select p.*,o.* from person p,orders o where o.pid=p.p_id and o.o_id=#{id} 
    </select>

</mapper>

 

sqlmapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

<typeAliases>
    <typeAlias type="com.kerwin.mybatis.pojo.Author" alias="Author"/>
</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:3306/mybatis"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="com/kerwin/mybatis/pojo/AuthorMapper.xml"/>
    <mapper resource="com/kerwin/mybatis/pojo/PostMapper.xml"/>
    <mapper resource="com/kerwin/mybatis/pojo/PersonMapper.xml"/>
    <mapper resource="com/kerwin/mybatis/pojo/OrdersMapper.xml"/>
  </mappers>
</configuration>

 

posted @ 2016-03-17 10:12  ordinary01  阅读(369)  评论(0编辑  收藏  举报