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

你可能感兴趣的文章
Mysql中文乱码问题完美解决方案
查看>>
mysql中的 +号 和 CONCAT(str1,str2,...)
查看>>
Mysql中的 IFNULL 函数的详解
查看>>
mysql中的collate关键字是什么意思?
查看>>
MySql中的concat()相关函数
查看>>
mysql中的concat函数,concat_ws函数,concat_group函数之间的区别
查看>>
MySQL中的count函数
查看>>
MySQL中的DB、DBMS、SQL
查看>>
MySQL中的DECIMAL类型:MYSQL_TYPE_DECIMAL与MYSQL_TYPE_NEWDECIMAL详解
查看>>
MySQL中的GROUP_CONCAT()函数详解与实战应用
查看>>
MySQL中的IO问题分析与优化
查看>>
MySQL中的ON DUPLICATE KEY UPDATE详解与应用
查看>>
mysql中的rbs,SharePoint RBS:即使启用了RBS,内容数据库也在不断增长
查看>>
mysql中的undo log、redo log 、binlog大致概要
查看>>
Mysql中的using
查看>>
MySQL中的关键字深入比较:UNION vs UNION ALL
查看>>
mysql中的四大运算符种类汇总20多项,用了三天三夜来整理的,还不赶快收藏
查看>>
mysql中的字段如何选择合适的数据类型呢?
查看>>
MySQL中的字符集陷阱:为何避免使用UTF-8
查看>>
mysql中的数据导入与导出
查看>>