在系统开发中,经常会遇到多个表关联查询。关联查询有返回的结果有多种形式,一对一形式,一对多形式。接下来我们看看怎样使用mybatis进行关联表查询,在这一节中,我们会接触到以下几个关键字association,collection,resultMap,result等,请注意用法。
首先我们创建两个表,skus表和category分类表,通过分类ID进行关联
CREATE TABLE `category` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`CategoryName` varchar(50) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8
CREATE TABLE `skus` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`SkuName` varchar(50) DEFAULT NULL,
`SkuCode` varchar(50) DEFAULT NULL,
`CategoryId` int(11) DEFAULT '0',
PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8
1.一对一查询
首先我们定义一个实体类,查询商品信息,并获得商品所在的分类,每个商品只能属于一个分类。 一对一查询主要用到association关键字package dto;mapper.xml配置
public class skus {
private int Id ;
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
private String SkuName ;
public String getSkuCode() {
return SkuCode;
}
public void setSkuCode(String skuCode) {
SkuCode = skuCode;
}
public String getSkuName() {
return SkuName;
}
public void setSkuName(String skuName) {
SkuName = skuName;
}
private String SkuCode;
/**
* 定义分类属性
*/
private Category category;
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
<!-- 关联分类表查询商品和分类信息 -->resultMap表示返回的结果类型. Id表示主键,javaType表示对象返回类型
<select id="getSkuList" resultMap="skusResultMap">
SELECT
s.Id,
s.SkuName,
s.SkuCode,
c.Id,
c.CategoryName
FROM skus s INNER JOIN category c ON s.categoryid=c.id
</select>
<resultMap type="dto.skus" id="skusResultMap">
<id property="Id" column="Id"/>
<result property="SkuName" column="SkuName"/>
<result property="SkuCode" column="SkuCode"/>
<association property="category" javaType="dto.Category">
<id property="Id" column="Id"/>
<result property="CategoryName" column="CategoryName"/>
</association>
</resultMap>
查询结果
@RequestMapping(value="GetSkuList")
public String GetSkuList()
{
String resource = "/conf.xml";
//加载mybatis的配置文件
InputStream inputstream =this.getClass().getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputstream);
SqlSession session = sessionFactory.openSession();
String statesql= "mapping.skusMapper.getSkuList";//在skusMapper.xml中有命名空间+方法名
java.util.List<skus> listResult= session.selectList(statesql);
session.close();
for(dto.skus item:listResult)
{
System.out.println(item.getId()+"--"+item.getSkuCode()+"--"+item.getSkuName()+"--"+item.getCategory().Id+"--"+item.getCategory().CategoryName);
}
return "index";
}
2.一对多查询
下面这个查询表示查询分类下的所有商品信息。使用collection来实现一对多的结果集查询package dto;配置SQL
import java.util.List;
public class Category {
public int Id;
public String CategoryName;
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getCategoryName() {
return CategoryName;
}
public void setCategoryName(String categoryName) {
CategoryName = categoryName;
}
//分类下的商品集合
public List<skus> skuList;
public List<skus> getSkuList() {
return skuList;
}
public void setSkuList(List<skus> skuList) {
this.skuList = skuList;
}
}
<!-- 查询分类信息 -->查询结果
<select id="getCateogy" resultMap="categoryResultMap" parameterType="int">
SELECT
c.Id,
c.CategoryName from
category c
WHERE c.Id=#{Id}
</select>
<resultMap type="dto.Category" id="categoryResultMap">
<id property="Id" column="Id"/>
<result property="CategoryName" column="CategoryName"/>
<collection property="skuList" ofType="dto.skus" column="Id" select="getsinglesku" >
</collection >
</resultMap>
<!-- ofType指定集合中的对象类型-->
<select id="getsinglesku" parameterType="int" resultType="dto.skus">
SELECT s.Id,
s.SkuName,
s.SkuCode
FROM skus s WHERE s.`CategoryId`=#{Id}
</select>
@RequestMapping(value="GetCategory")
public String GetCategory()
{
String resource = "/conf.xml";
//加载mybatis的配置文件
InputStream inputstream =this.getClass().getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputstream);
SqlSession session = sessionFactory.openSession();
String statesql= "mapping.skusMapper.getCateogy";//在skusMapper.xml中有命名空间+方法名
//根据分类ID查询
Category dto= session.selectOne(statesql,6);
System.out.println(dto.getId()+"--"+dto.getCategoryName());
for(dto.skus item:dto.getSkuList())
{
System.out.println(item.getId()+"--"+item.getSkuCode()+"--"+item.getSkuName());
}
return "index";
}