分别有两个表customer和product,我查customer时也把相应的product查(连查)出来
customer有字段:id,productId,ame,sex,identityCard;
product有字段:productId,productName, amount
它们是以product为处键,建立关连的
package ibatis.pojo;public class Customer implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private int id;
private int productId;
private String name;
private String sex;
private String identityCard;
private Product product; public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getIdentityCard() {
return this.identityCard;
}
public void setIdentityCard(String identityCard) {
this.identityCard = identityCard;
}
public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
}
package ibatis.pojo;
public class Product implements java.io.Serializable {
private Integer productId;
private String productName;
private Integer amount;
public Integer getProductId() {
return this.productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public String getProductName() {
return this.productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public Integer getAmount() {
return this.amount;
}
public void setAmount(Integer amount) {
this.amount = amount;
}
}
请问我应该怎么映射两表之间的关系和相应的SQL语句?

解决方案 »

  1.   

     自定义的类的映射可以直接写product.productId
      

  2.   

    不用建两个javabean呀,一个就可以了。
    把你要取得两个表的字段写到一个javabean里面就可以了。然后写sql<select id="sqlId"
            resultClass="ibatis.pojo.Customer"
            parameterClass="java.lang.String">
       select T1.id,
              T1.productId,
              T1.name,
              T1.sex,
              T1.identityCard,
              T2.productName,
              T2.amount
         from customer T1,product T2
         where T1.productId = T2.productId
    </select>
      

  3.   

    不要parameterClass<select id="sqlId"
            resultClass="ibatis.pojo.Customer">
       select T1.id,
              T1.productId,
              T1.name,
              T1.sex,
              T1.identityCard,
              T2.productName,
              T2.amount
         from customer T1,product T2
         where T1.productId = T2.productId
    </select>
      

  4.   

    我是说不指定Bean与数据库的映射关系吗?
      

  5.   

    如果将二个表的属性写到一个Bean中不好吧,这样好混乱哦!有其它的方法吗?
      

  6.   

    可以这样吧!我先查询product表的值,把值填充Customer类中product的属性...
      

  7.   

    http://www.xn518.com/downIbatis/Ibatis.rar 这是我的代码,有兴趣的可以帮我看看
      

  8.   

    试下这个就OK 了!!!
    <resultMap id="productMap" class="java.util.HashMap">
    <result property="id" column="ID"/>
    <result property="productId" column="productId"/>
    <result property="ame" column="ame"/>
    <result property="sex" column="sex"/>
                    <result property="identityCard" column="identityCard"/>
    <result property="productName" column="productName"/>
    <result property="amount " column="amount "/>
    </resultMap><select id="listProductMap"
            resultClass="productMap"
            parameterClass="java.util.HashMap">
       select T1.id,
              T1.productId,
              T1.name,
              T1.sex,
              T1.identityCard,
              T2.productName,
              T2.amount
         from customer T1,product T2
         where T1.productId = T2.productId
    </select>
      

  9.   

    resultMap 结果映射可以把两个表的字段写在一个javabean中,或者直接放在Map中!!我一直这样用!!
    <resultMap   id= "productMap "   class= "java.util.HashMap "> 
     <result   property= "id "   column= "ID "/> 
     <result   property= "productId "   column= "productId "/> 
     <result   property= "ame "   column= "ame "/> 
     <result   property= "sex "   column= "sex "/> 
     <result   property= "identityCard "   column= "identityCard "/> 
     <result   property= "productName "   column= "productName "/> 
     <result   property= "amount   "   column= "amount   "/> 
    </resultMap> <select   id= "listProductMap " parameterClass="java.util.Map" resultMap="productMap "> 
          select   T1.id, 
                        T1.productId, 
                        T1.name, 
                        T1.sex, 
                        T1.identityCard, 
                        T2.productName, 
                        T2.amount 
              from   customer   T1,product   T2 
              where   T1.productId   =   T2.productId 
    </select> 
      

  10.   

    <resultMap  id= "productMap "  class= "Customer  ">
    <result  property= "id "  column= "ID "/>
    <result  property= "productId "  column= "productId "/>
    <result  property= "ame "  column= "ame "/>
    <result  property= "sex "  column= "sex "/>
    <result  property= "identityCard "  column= "identityCard "/>
    <result  property= "amount  "  column= "amount  "/>....................
    <result  property= "product"  column= "productId " select=“selectProductByProductID”/>
    </resultMap> 可以用这种方式啊。但可能会有lazyloading的问题。