Mybatis系列之-一对多表关联查询

转自:http://blog.csdn.net/jefry_xdz/article/details/8767358

创建两张表:一张是用户,一张是用户所对应的移动手机,一户用户可以有部移动手机。
      这是用户t_user表

             
        这是移动电话t_mobile表
              
           在Java实体对象对中,一对多可以根据List和Set来实现,两者在mybitis中都是通过collection标签来配合使用,稍后会做详细配置介绍

          创建表对应的JavaBean对象

Mobile  Bean

[java] view plain copy
  1. public class Mobile {  
  2.     private int id;  
  3.     private String telnumber;  
  4.   
  5.     public int getId() {  
  6.         return id;  
  7.     }  
  8.   
  9.     public void setId(int id) {  
  10.         this.id = id;  
  11.     }  
  12.   
  13.     public String getTelnumber() {  
  14.         return telnumber;  
  15.     }  
  16.   
  17.     public void setTelnumber(String telnumber) {  
  18.         this.telnumber = telnumber;  
  19.     }  
  20.   
  21. }  


User Bean

[java] view plain copy
  1. package com.jefry;  
  2.   
  3. import java.util.List;  
  4.   
  5. public class User {  
  6.     private int id;  
  7.     private String userName;  
  8.     private String password;  
  9.           private List<Mobile> mobiles; //这里也可以是Set集合  
  10.   
  11.     public List<Mobile> getMobiles() {  
  12.         return mobiles;  
  13.     }  
  14.   
  15.     public void setMobiles(List<Mobile> mobiles) {  
  16.         this.mobiles = mobiles;  
  17.     }  
  18.   
  19.     public String getUserName() {  
  20.         return userName;  
  21.     }  
  22.   
  23.     public void setUserName(String userName) {  
  24.         this.userName = userName;  
  25.     }  
  26.   
  27.     public String getPassword() {  
  28.         return password;  
  29.     }  
  30.   
  31.     public void setPassword(String password) {  
  32.         this.password = password;  
  33.     }  
  34.   
  35.     public int getId() {  
  36.         return id;  
  37.     }  
  38.   
  39.     public void setId(int id) {  
  40.         this.id = id;  
  41.     }  
  42.   
  43. }  

 

在上一篇的基础上改写映射文件:

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE mapper  
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  5. <mapper namespace="user">  
  6.      <resultMap id="userResultMap" type="User">   
  7.         <id property="id" column="id" javaType="int" jdbcType="INTEGER" />  
  8.         <result property="userName" column="name" javaType="string" jdbcType="VARCHAR"/>  
  9.         <result property="password" column="pass" javaType="string" jdbcType="VARCHAR"/>  
  10.         <collection property="mobiles" column="userid" ofType="Mobile">    
  11.             <id property="id" column="id" javaType="int" jdbcType="INTEGER"/>    
  12.             <result property="telnumber" column="telnumber" javaType="string" jdbcType="VARCHAR"/>    
  13.         </collection>    
  14.      </resultMap>  
  15.       
  16.     <!--多表查询操作-->  
  17.     <select id="selectUser" parameterType="int"  resultMap="userResultMap" >  
  18.         <!--分别为mobile的主键id与user的主键id赋值别名,避免因为两个表字段名称相同而注入到对应对象名称冲突-->  
  19.         select m.id m_id,m.telnumber,u.id u_id,u.name,u.pass from t_mobile m,t_user u where m.userid = u.id and u.id = #{id}   
  20.     </select>  
  21. </mapper>  

 

最后,通过测试OK

 

[java] view plain copy
    1. public class Test {  
    2.     static String resource = "mybatis-config.xml";  
    3.   
    4.     public static void main(String[] args) throws IOException {  
    5.         InputStream inputStream = Resources.getResourceAsStream(resource);  
    6.         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);  
    7.         SqlSession session = sqlSessionFactory.openSession();  
    8.         try {  
    9.             User user = session.selectOne("user.selectUser", 1);  
    10.             List<Mobile> mobiles = user.getMobiles();  
    11.             for(Mobile mobile : mobiles) {  
    12.                 System.out.println("user:" + user.getUserName() + ",tel:" + mobile.getTelnumber());  
    13.             }  
    14.               
    15.               
    16.         } finally {  
    17.             session.close();  
    18.         }  
    19.     }  
    20.   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis-Plus是一款MyBatis的增强工具,它提供了很多实用的功能,比如一对多关联查询。在MyBatis-Plus中,一对多关联查询可以通过使用@TableName注解和@TableField注解来实现。 假设我们有两张,一张是学生,另一张是课程,一个学生可以选多门课程,那么我们就可以用一对多关联查询来查询某个学生选的所有课程。 首先,在学生中定义一个属性List<Course> courses,并使用@TableField注解将该属性与课程的外键联起来: ``` public class Student { @TableId private Long id; private String name; @TableField(exist = false) private List<Course> courses; } ``` 然后,在课程中定义一个属性Long studentId,并使用@TableField注解将该属性与学生的主键联起来: ``` public class Course { @TableId private Long id; private String name; @TableField("student_id") private Long studentId; } ``` 最后,我们使用MyBatis-Plus提供的wrapper类进行关联查询: ``` QueryWrapper<Student> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("id", studentId); List<Student> students = studentMapper.selectList(queryWrapper); for (Student student : students) { QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>(); courseQueryWrapper.eq("student_id", student.getId()); List<Course> courses = courseMapper.selectList(courseQueryWrapper); student.setCourses(courses); } ``` 以上就是MyBatis-Plus实现一对多关联查询的方法。如果您还有其他问题或需要进一步的帮助,请随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值