首页 > 代码库 > Mybatis collection 使用的注意点

Mybatis collection 使用的注意点

mybatis  的一对多

<?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.ly.fw.dao.OrderDAO">	<resultMap type="com.ly.fw.entity.Order" id="order">		<id column="id" property="id" />          <result column="orderId" property="orderId" />         <result column="user" property="user" />          <result column="address" property="address" />            <result column="tel" property="tel" />        <result column="code" property="code" />           <result column="orderPrice" property="orderPrice" />         <result column="transNum" property="transNum" />    		<result column="status" property="status" />    		<result column="time" property="time" />    		<collection property="productList"  column="orderId"  resultMap="opro"/>  	</resultMap>	<resultMap type="com.ly.fw.entity.OrderProduct" id="opro">		<id column="opid" property="opId" />          <result column="orderId" property="orderId" />         <result column="productId" property="productId" />          <result column="count" property="count" />            <result column="price" property="price" />       	<result column="size" property="size" />	</resultMap>  <select id="getAll" resultMap="order">        select o.*,p.* from `order` as o, order_product as p where o.orderId=p.orderId order by o.`time` desc limit #{start},#{end}    </select></mapper>

  实体类中:

 

    private long id ;    private String orderId ;//订单id    private String user ;//用户    private String address ;//地址    private String tel ;//电话    private String code ;//邮编    private String orderPrice ;//订单金额    private String transNum;    private int status;//状态。    private Date time;//订单时间        private List<OrderProduct> productList;// 一对多

  正常情况这样是直接能返会集合中的数据的。但是今天遇到个问题是返还的集合中始终只有一条数据。数据库中返回应该不只一条.

查了半天资料发现:两张表的主键名称不能一样,我就是由于两张表的主键 都是 “id”  所以返回只有一条数据

Mybatis collection 使用的注意点