当前位置:   article > 正文

Mybatis的多表查询_java mybatis 多数据库表实现动态列(可能存在多个不同数据库表的列)查询功能

java mybatis 多数据库表实现动态列(可能存在多个不同数据库表的列)查询功能

那么有时候我们会有需求,联合两个表去进行查询。本次讲解Mybatis如何实现多表查询。

在讲多表查询的时候,我们要先引入一个知识点 resultMap标签,它可以解决:

  1. java属性名称与数据库中的字段不一致
  2. 多表查询

具体用法结合结合下边的例子进行理解。

当直接引入一个对象的时候:

  • property:表示java对象的属性,也就是我们的类中定义的属性
  • column: 和前边属性对应映射的 数据库中的字段。
  • id一列表示这个字段属性为主键
  • association一列表示引入其他的对象,其中property中是引入的对象的名称,就是在类中定义的对象。 resultMap里边放的是要引入的对象的resultMap:引入的类的路径+引入类的resultMap路径。
  1. <!-- id是标记这个resultMap身份的id 后边是他返回数据类型的路径-->
  2. <resultMap id="art1" type="com.example.springmybatisdemo.model.Article">
  3. <!-- 这里的id是主键 -->
  4. <id property="uid" column="id"></id>
  5. <result property="createtime" column="createtime"></result>
  6. <result property="updatetime" column="updatetime"></result>
  7. <result property="content" column="content"></result>
  8. <result property="title" column="title"></result>
  9. <result property="rcount" column="rcount"></result>
  10. <!-- 引入其他对象的时候,用association,且要引入其他xml的resultMap,路径是 xml文件的namespace + resultMap的id -->
  11. <association property="user2" resultMap="com.example.springmybatisdemo.mapper.UserMapper2.map1"></association>

举个实例,直接引入一个对象:

类:

  1. import lombok.Data;
  2. import java.util.Date;
  3. @Data
  4. public class Article {
  5. private Integer uid;
  6. private String title;
  7. private String content;
  8. private Date createtime;
  9. private Date updatetime;
  10. private Integer rcount;
  11. private Integer state;
  12. // 一般不会这样用,耦合度太高了
  13. // 作者类相关信息
  14. private User2 user2;
  15. }

mapper 接口:

  1. import com.example.springmybatisdemo.model.Article;
  2. import org.apache.ibatis.annotations.Insert;
  3. import org.apache.ibatis.annotations.Mapper;
  4. import org.apache.ibatis.annotations.Param;
  5. import org.apache.ibatis.annotations.Select;
  6. import java.util.List;
  7. @Mapper
  8. public interface ArticleMapper {
  9. List<Article> showAll();
  10. }

测试类代码:

  1. import com.example.springmybatisdemo.model.Article;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.junit.jupiter.api.Test;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. import static org.junit.jupiter.api.Assertions.*;
  9. @Slf4j
  10. @SpringBootTest
  11. class ArticleMapperTest {
  12. //依赖注入
  13. @Autowired
  14. private ArticleMapper articleMapper;
  15. @Test
  16. void showAll() {
  17. List<Article> all=articleMapper.showAll();
  18. log.info(all.toString());
  19. }
  20. }

此时,我们要进行articleinfo表和userinfo表的联合查询,我们查询当userinfo中的id和articleinfo表中的uid相等的时候 的结果。

此时的xml文件,就涉及了另一个表,那么我们要把这个表引入,就要用到了resultMap,在resultMap中引入要联合查询的表,然后进行查询。

整个xml文件如下:

  1. <!-- id是标记这个resultMap身份的id 后边是他返回数据类型的路径-->
  2. <resultMap id="art1" type="com.example.springmybatisdemo.model.Article">
  3. <!-- 这里的id是主键 -->
  4. <id property="uid" column="id"></id>
  5. <result property="createtime" column="createtime"></result>
  6. <result property="updatetime" column="updatetime"></result>
  7. <result property="content" column="content"></result>
  8. <result property="title" column="title"></result>
  9. <result property="rcount" column="rcount"></result>
  10. <!-- 引入其他对象的时候,用association,且要引入其他xml的resultMap,路径是 xml文件的namespace + resultMap的id -->
  11. <association property="user2" resultMap="com.example.springmybatisdemo.mapper.UserMapper2.map1"></association>
  12. <!--本次的多表查询语句-->
  13. <!-- 这里的id是实现的方法的名称
  14. 这里的resultMap是使用的resultMap的id,因为同一个xml文件中,可以有多个resultMap-->
  15. <select id="showAll" resultMap="art1">
  16. select *
  17. from articleinfo ta
  18. left join userinfo tb on ta.uid=tb.id;
  19. </select>

引入的是另一个对象的属性

上边直接引入整个对象,耦合度太高了,一般不建议使用,因此一般使用方法为,引入另一个对象的属性,什么属性呢?自然是多表查询时相关的属性了,当然想知道别的,也可以引入其他属性。

此时代码:

类:

  1. import lombok.Data;
  2. import java.util.Date;
  3. @Data
  4. public class Article {
  5. private Integer uid;
  6. private String title;
  7. private String content;
  8. private Date createtime;
  9. private Date updatetime;
  10. private Integer rcount;
  11. private Integer state;
  12. private Integer userId11;
  13. private String username;
  14. }

mapper接口:

  1. @Mapper
  2. public interface ArticleMapper {
  3. List<Article> showAll2();
  4. }

测试类:

  1. import com.example.springmybatisdemo.model.Article;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.junit.jupiter.api.Test;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. import static org.junit.jupiter.api.Assertions.*;
  9. @Slf4j
  10. @SpringBootTest
  11. class ArticleMapperTest {
  12. //注入接口中要测试的方法的实现类对象 接口对象指向他的实现类对象
  13. @Autowired
  14. private ArticleMapper articleMapper;
  15. @Test
  16. void showAll2() {
  17. List<Article> all=articleMapper.showAll2();
  18. log.info(all.toString());
  19. }
  20. }

xml文件:(注意这里的resultMap)

  1. <resultMap id="art2" type="com.example.springmybatisdemo.model.Article">
  2. <id property="uid" column="id"></id>
  3. <result property="rcount" column="rcount"></result>
  4. <result property="title" column="title"></result>
  5. <result property="content" column="content"></result>
  6. <result property="updatetime" column="updatetime"></result>
  7. <result property="createtime" column="createtime"></result>
  8. <result property="userId11" column="id"></result>
  9. <result property="username" column="username"></result>
  10. </resultMap>
  11. <select id="showAll2" resultMap="art2">
  12. select
  13. ta.id,
  14. tb.id as userid,
  15. tb.username as username
  16. from articleinfo ta
  17. left join userinfo tb on ta.id=tb.id;
  18. </select>

多表查询内容结束,下次再见!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/830569
推荐阅读
相关标签
  

闽ICP备14008679号