博客
关于我
Mybatis-多表查询(注解)
阅读量:99 次
发布时间:2019-02-26

本文共 3732 字,大约阅读时间需要 12 分钟。

项目目录

一对一查询

一对一查询语句

一对一查询通常涉及通过外键关联两个表。例如,订单表和用户表的关系,可以通过订单的外键oid来查询对应的用户信息。

代码示例

OrderMapper接口

@Select("select * from orders")@Results({    @Result(id = true, column = "id", property = "id"),    @Result(column = "ordertime", property = "ordertime"),    @Result(column = "total", property = "total"),    @Result(column = "oid", property = "oid"),    @Result(property = "user", javaType = User.class, column = "oid",            one = @One(select = "cn.guardwhy.dao.UserMapper.findById", fetchType = FetchType.EAGER))})public interface OrderMapper {    void findAllWithUser();}

UserMapper接口

@Select("select * from user where id = #{id}")@Results({    @Result(column = "id", property = "id", id = true),    @Result(column = "user_name", property = "username"),    @Result(column = "birthday", property = "birthday"),    @Result(column = "sex", property = "sex"),    @Result(column = "address", property = "address")})public interface UserMapper {    User findById(Integer id);}

测试代码

// 查询一个订单,并同时查询该订单所属的用户@Testpublic void testOrderWithUser() {    SqlSession sqlSession = MybatisUtils.getSession();    OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);    List
list = mapper.findAllWithUser(); for (Orders orders : list) { System.out.println(orders); } sqlSession.close();}

一对多查询

一对多查询语句

一对多查询涉及通过一张表的主键到多张表的关联。例如,用户表和订单表的关系,可以通过用户表的id查询所有关联的订单信息。

代码示例

UserMapper接口

@Select("select * from user")@Results({    @Result(column = "id", property = "id", id = true),    @Result(column = "user_name", property = "username"),    @Result(column = "birthday", property = "birthday"),    @Result(column = "sex", property = "sex"),    @Result(column = "address", property = "address"),    @Result(property = "ordersList", javaType = List.class, column = "id",            many = @Many(select = "cn.guardwhy.dao.OrderMapper.findByOid"))})public interface UserMapper {    void findAllWithOrders();}

OrderMapper接口

@Select("select * from orders where id = #{oid}")public interface OrderMapper {    List
findByOid(Integer oid);}

测试代码

// 查询一个用户,并同时查询该用户具有的订单@Testpublic void testUserWithOrders() {    SqlSession sqlSession = MybatisUtils.getSession();    UserMapper mapper = sqlSession.getMapper(UserMapper.class);    List
userList = mapper.findAllWithOrders(); for (User user : userList) { System.out.println(user); } sqlSession.close();}

多对多查询

多对多查询语句

多对多查询涉及通过中间表(关联表)查询两个表之间的关系。例如,用户表和角色表的关系,可以通过中间表user_role查询用户的角色信息。

代码示例

UserMapper接口

@Select("select * from user")@Results({    @Result(column = "id", property = "id", id = true),    @Result(column = "user_name", property = "username"),    @Result(column = "birthday", property = "birthday"),    @Result(column = "sex", property = "sex"),    @Result(column = "address", property = "address"),    @Result(property = "roleList", javaType = List.class, column = "id",            many = @Many(select = "cn.guardwhy.dao.RoleMapper.findByOid"))})public interface UserMapper {    void findAllWithRole();}

RoleMapper接口

@Select("select * from role r inner join user_role ur on r.id = ur.user_id where ur.role_id = #{id}")@Results({    @Result(id = true, column = "id", property = "id"),    @Result(column = "role_name", property = "roleName"),    @Result(column = "role_detail", property = "roleDetail")})public interface RoleMapper {    List
findByOid(Integer id);}

测试代码

// 多对多查询测试@Testpublic void testUserWithRole() {    SqlSession sqlSession = MybatisUtils.getSession();    UserMapper mapper = sqlSession.getMapper(UserMapper.class);    List
list = mapper.findAllWithRole(); for (User user : list) { System.out.println(user); } sqlSession.close();}

执行结果

(以下为示例执行结果图)

转载地址:http://dscz.baihongyu.com/

你可能感兴趣的文章
python商品数据分析可视化系统(带爬虫)京东销售数据分析 计算机毕业设计 源码下载
查看>>
python商品库存管理系统 django框架 商品网站 MySQL数据库 源码下载 计算机毕业设计
查看>>
Python哪个版本最稳定好用2023.10.19
查看>>
Python和黑客技术的渊源
查看>>
Python和RF编写接口自动化
查看>>
python和js哪个难_【Python】记一次学习,Python 与 js 在实现闭包时的差异
查看>>
python和java哪个更值得学——来自知乎高赞回答
查看>>
python和c混合编程 github_在pycharm中使用git版本管理以及同步github的方法
查看>>
python命名空间更改_Python模块xml.etree.ElementTree自动修改xml命名空间键
查看>>
Python命名中尾随下划线有什么好处?
查看>>
Python启动脚本
查看>>
Python听写汇总结果
查看>>
python吊打Excel?屁!那是你不会用!
查看>>
Python合并两张图片 | 先叠透明度再合并 (附Demo)
查看>>
python各进制的表述与转换
查看>>
python各种库安装
查看>>
python可视化matplotlib_如何使用Python中最强大的可视化工具Matplotlib?
查看>>
python可维护性_使用这7大神器,让你的Python 代码更容易于维护
查看>>
Python只运行一次while循环
查看>>
python变量的详细教程_Python零基础入门教程之语法入门[变量](第二期)
查看>>