分别有两个表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语句?
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语句?
把你要取得两个表的字段写到一个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>
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>
<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>
<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>
<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的问题。