博客
关于我
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/

你可能感兴趣的文章
Netty相关
查看>>
Network Dissection:Quantifying Interpretability of Deep Visual Representations(深层视觉表征的量化解释)
查看>>
Network Sniffer and Connection Analyzer
查看>>
NFS共享文件系统搭建
查看>>
ng 指令的自定义、使用
查看>>
nginx + etcd 动态负载均衡实践(二)—— 组件安装
查看>>
Nginx + uWSGI + Flask + Vhost
查看>>
Nginx Location配置总结
查看>>
Nginx 动静分离与负载均衡的实现
查看>>
Nginx 反向代理解决跨域问题
查看>>
Nginx 反向代理配置去除前缀
查看>>
nginx 后端获取真实ip
查看>>
Nginx 学习总结(17)—— 8 个免费开源 Nginx 管理系统,轻松管理 Nginx 站点配置
查看>>
nginx 常用配置记录
查看>>
Nginx 我们必须知道的那些事
查看>>
Nginx 的 proxy_pass 使用简介
查看>>
Nginx 的配置文件中的 keepalive 介绍
查看>>
nginx 配置 单页面应用的解决方案
查看>>
nginx 配置~~~本身就是一个静态资源的服务器
查看>>
Nginx下配置codeigniter框架方法
查看>>