惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
WordPress大学
WordPress大学
IT之家
IT之家
G
Google Developers Blog
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
博客园 - 三生石上(FineUI控件)
Engineering at Meta
Engineering at Meta
Martin Fowler
Martin Fowler
V
V2EX
爱范儿
爱范儿
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog
V
Visual Studio Blog
有赞技术团队
有赞技术团队
I
InfoQ
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
J
Java Code Geeks
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
雷峰网
雷峰网

博客园 - superstar

我的抽奖系统 CRM 后台管理 计件系统 -.netcore-审批人 计件系统 win10家庭版怎么打开组策略编辑器 触发值更新 求类似这个Excel的甘特国科项目模板 SQL账表实现超始日期为最近7天 简单帐表双击打开直接SQL账表 安装补丁的时候提示“无法连接,需要安装K3CloudManager服务”,该服务怎么安装? sql 开窗函数 需要基本单位实发数量!= 主库存基本单位实发数量 就不报这个错 邮件服务器设置 结合实例来分析SQL的窗口函数 sql 逐行累加 如何设置而是采购员默认值 BOSS语句整理!!!花了两个小时!!! 金蝶版本升级例如 6.2 升级成8.0 建议先升级个7.0过滤版本再升级8.0 金蝶云星空MRP运算测试 oracle使用exp/imp导入导出(用户) 1.Spring Boot创建SSM(IDEA+ORACLE) .NET 6 使用JWT Bearer认证和授权的步骤 一年级拼音练习题
springboot整合mybatis实现多表查询的实战记录
superstar · 2022-08-17 · via 博客园 - superstar

SpringBoot对数据库操作有多种方式,下面这篇文章主要给大家介绍了关于springboot整合mybatis实现多表查询的相关资料,文中通过示例代码以及图文介绍的非常详细,需要的朋友可以参考下

什么是mybatis

(1)mybatis 是一个半 orm(对象关系映射)框架,它内部封装了 jdbc,开发时只需要关注 sql 语句本身,不需要花费精力去处理加载驱动、创建连接、创建statement 等繁杂的过程。程序员直接编写原生态 sql,可以严格控制 sql 执行性能,灵活度高。

(2)mybatis 可以使用 xml 或注解来配置和映射原生信息,将 pojo 映射成数据库中的记录,避免了几乎所有的 jdbc 代码和手动设置参数以及获取结果集。 @insert @repository

(3)通过 xml 文件或注解的方式将要执行的各种 statement 配置起来,并通过java 对象和 statement 中 sql 的动态参数进行映射生成最终执行的 sql 语句,最后由 mybatis 框架执行 sql 并将结果映射为 java 对象并返回。(从执行 sql 到返回 result 的过程)。

mybaits 的优点:

(1)基 于 sql 语句编程,相当灵活,不会对应用程序或者数据库的现有设计造成任何影响,sql 写在 xml 里,解除 sql 与程序代码的耦合,便于统一管理;提供 xml标签,支持编写动态 sql 语句,并可重用。

(2)与 jdbc 相比,减少了 50%以上的代码量,消除了 jdbc 大量冗余的代码,不需要手动开关连接;

(3)很好的与各种数据库兼容(因为 mybatis 使用 jdbc 来连接数据库,所以只要jdbc 支持的数据库 mybatis 都支持)。

(4)能够与 spring 很好的集成;

mybatis是如何进行分页的?分页插件的原理是什么?

mybatis使用rowbounds对象进行分页,它是针对resultset结果集执行的内存分页,而非物理分页。可以在sql内直接书写带有物理分页的参数来完成物理分页功能,也可以使用分页插件来完成物理分页。

下面将详细springboot整合mybatis多表查询的方法,一起来看看吧

1、一对一查询(例一个用户一个账户)

1.1、实体类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

@data

public class userinfo {

    private int u_id;

    private string name;

    private account account;

}   

@data

public class account {

    private int a_id;

    private string  aname;

    private double money;

}

1.2、数据库表

用户表

springboot整合mybatis实现多表查询的实战记录

账户表

springboot整合mybatis实现多表查询的实战记录

1.3、持久层接口

?

1

2

3

4

5

6

7

8

@select("select * from userinfo where name=#{name} ")

  @results({

          @result(property ="account",column = "a_id",javatype = account.class,

                  one = @one(select="com.bbz.dao.accountdao.findbyid",fetchtype = fetchtype.lazy))

  })

  public userinfo finduserlnfo(string name);

?

1

2

@select("select * from account where a_id=#{a_id}")

 public account findbyid (int a_id);

2、一对多查询(例一个用户对应多个账户)

2.1、实体类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

@data

public class userinfo {

    private int u_id;

    private string name;

    private list<account>accountlist;

}

@data

public class account {

    private  int id;

    private int a_id;

    private string  aname;

    private double money; 

}

2.2、数据库表

用户表

springboot整合mybatis实现多表查询的实战记录

账户表

springboot整合mybatis实现多表查询的实战记录

2.3、持久层接口

?

1

2

3

4

5

6

7

8

9

@select("select * from userinfo where name=#{name}")

@results({

        @result(property ="accountlist",column ="a_id",javatype = list.class,

                many = @many(select = "com.bbz.dao.accountdao.findbyid",fetchtype = fetchtype.lazy)

        )

})

public userinfo finduser(string name);

?

1

2

3

@select("select * from account where a_id=#{a_id}")

   public   account   findbyid (int a_id);

3、总结

共同点:

无论是一对一还是一对多,都是通过附属查询来实现的,我们需要定义这个附属查询方法。

在主查询方法中通过@one、@many指定附属查询方法的全路径。

都通过column来传递参数给附属方法。

不同点:

一对一,那么附属方法返回的是一个单独的对象

一对多,那么附属方法返回的是一个对象集合

4、多对多的查询(例一个用户多个角色)

4.1、实体类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

@data

public class userinfo {

    private int u_id;

    private string name;

    private  list<role>rolelist;

}

@data

public class role {

    private int r_id;

    private string name;

}

4.2、数据库表

用户表

springboot整合mybatis实现多表查询的实战记录

角色表

springboot整合mybatis实现多表查询的实战记录

中间表

springboot整合mybatis实现多表查询的实战记录

4.3、持久层接口

?

1

2

3

4

5

6

7

8

9

10

@select("select * from userinfo where u_id=#{u_id}")

   @results({

           @result(property = "u_id",column = "u_id"),

           @result(property ="rolelist",column ="u_id",javatype = list.class,

                   many = @many(select = "com.bbz.dao.roledao.findbyid",fetchtype = fetchtype.lazy)

           )

   })

   public userinfo finduser(int u_id);

?

1

2

@select("select * from role r,user_role ur where r.r_id=ur.r_id and ur.u_id=#{u_id}")

   public list<role> findbyid(int u_id);

5、多对一(一个用户对应多个老师)

5.1 实体类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

@data

public class userinfo {

    private int u_id;

    private string name;

    private teacher teacher;

}

public class teacher {

    public int t_id;

    public string name;

}

5.2、数据库表

用户表

springboot整合mybatis实现多表查询的实战记录

老师表

springboot整合mybatis实现多表查询的实战记录

5.3、持久层接口

?

1

2

3

4

5

6

@select("select * from  userinfo where u_id=#{u_id}")

    @results({

            @result(property ="teacher",column ="t_id",javatype = teacher.class,

                    one= @one(select = "com.bbz.dao.teacherdao.findbyid",fetchtype = fetchtype.lazy)

            )

    })

?

1

2

@select("select * from teacher where t_id=#{t_id}")

public teacher findbyid(int t_id);

总结

到此这篇关于springboot整和mybatis实现多表查询的文章就介绍到这了,更多相关springboot整和mybatis多表查询内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!