那么有时候我们会有需求,联合两个表去进行查询。本次讲解Mybatis如何实现多表查询。
在讲多表查询的时候,我们要先引入一个知识点 resultMap标签,它可以解决:
- java属性名称与数据库中的字段不一致
- 多表查询
具体用法结合结合下边的例子进行理解。
当直接引入一个对象的时候:
- property:表示java对象的属性,也就是我们的类中定义的属性
- column: 和前边属性对应映射的 数据库中的字段。
- id一列表示这个字段属性为主键
- association一列表示引入其他的对象,其中property中是引入的对象的名称,就是在类中定义的对象。 resultMap里边放的是要引入的对象的resultMap:引入的类的路径+引入类的resultMap路径。
<!-- id是标记这个resultMap身份的id 后边是他返回数据类型的路径-->
<resultMap id="art1" type="com.example.springmybatisdemo.model.Article">
<!-- 这里的id是主键 -->
<id property="uid" column="id"></id>
<result property="createtime" column="createtime"></result>
<result property="updatetime" column="updatetime"></result>
<result property="content" column="content"></result>
<result property="title" column="title"></result>
<result property="rcount" column="rcount"></result>
<!-- 引入其他对象的时候,用association,且要引入其他xml的resultMap,路径是 xml文件的namespace + resultMap的id -->
<association property="user2" resultMap="com.example.springmybatisdemo.mapper.UserMapper2.map1"></association>
举个实例,直接引入一个对象:
类:
import lombok.Data;
import java.util.Date;
@Data
public class Article {
private Integer uid;
private String title;
private String content;
private Date createtime;
private Date updatetime;
private Integer rcount;
private Integer state;
// 一般不会这样用,耦合度太高了
// 作者类相关信息
private User2 user2;
}
mapper 接口:
import com.example.springmybatisdemo.model.Article;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface ArticleMapper {
List<Article> showAll();
}
测试类代码:
import com.example.springmybatisdemo.model.Article;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@Slf4j
@SpringBootTest
class ArticleMapperTest {
//依赖注入
@Autowired
private ArticleMapper articleMapper;
@Test
void showAll() {
List<Article> all=articleMapper.showAll();
log.info(all.toString());
}
}
此时,我们要进行articleinfo表和userinfo表的联合查询,我们查询当userinfo中的id和articleinfo表中的uid相等的时候 的结果。
此时的xml文件,就涉及了另一个表,那么我们要把这个表引入,就要用到了resultMap,在resultMap中引入要联合查询的表,然后进行查询。
整个xml文件如下:
<!-- id是标记这个resultMap身份的id 后边是他返回数据类型的路径-->
<resultMap id="art1" type="com.example.springmybatisdemo.model.Article">
<!-- 这里的id是主键 -->
<id property="uid" column="id"></id>
<result property="createtime" column="createtime"></result>
<result property="updatetime" column="updatetime"></result>
<result property="content" column="content"></result>
<result property="title" column="title"></result>
<result property="rcount" column="rcount"></result>
<!-- 引入其他对象的时候,用association,且要引入其他xml的resultMap,路径是 xml文件的namespace + resultMap的id -->
<association property="user2" resultMap="com.example.springmybatisdemo.mapper.UserMapper2.map1"></association>
<!--本次的多表查询语句-->
<!-- 这里的id是实现的方法的名称
这里的resultMap是使用的resultMap的id,因为同一个xml文件中,可以有多个resultMap-->
<select id="showAll" resultMap="art1">
select *
from articleinfo ta
left join userinfo tb on ta.uid=tb.id;
</select>
引入的是另一个对象的属性
上边直接引入整个对象,耦合度太高了,一般不建议使用,因此一般使用方法为,引入另一个对象的属性,什么属性呢?自然是多表查询时相关的属性了,当然想知道别的,也可以引入其他属性。
此时代码:
类:
import lombok.Data;
import java.util.Date;
@Data
public class Article {
private Integer uid;
private String title;
private String content;
private Date createtime;
private Date updatetime;
private Integer rcount;
private Integer state;
private Integer userId11;
private String username;
}
mapper接口:
@Mapper
public interface ArticleMapper {
List<Article> showAll2();
}
测试类:
import com.example.springmybatisdemo.model.Article;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@Slf4j
@SpringBootTest
class ArticleMapperTest {
//注入接口中要测试的方法的实现类对象 接口对象指向他的实现类对象
@Autowired
private ArticleMapper articleMapper;
@Test
void showAll2() {
List<Article> all=articleMapper.showAll2();
log.info(all.toString());
}
}
xml文件:(注意这里的resultMap)
<resultMap id="art2" type="com.example.springmybatisdemo.model.Article">
<id property="uid" column="id"></id>
<result property="rcount" column="rcount"></result>
<result property="title" column="title"></result>
<result property="content" column="content"></result>
<result property="updatetime" column="updatetime"></result>
<result property="createtime" column="createtime"></result>
<result property="userId11" column="id"></result>
<result property="username" column="username"></result>
</resultMap>
<select id="showAll2" resultMap="art2">
select
ta.id,
tb.id as userid,
tb.username as username
from articleinfo ta
left join userinfo tb on ta.id=tb.id;
</select>
多表查询内容结束,下次再见!