mybatis多表查询

 

1
2
3
4
5
6
7
8
9
10
11
12
/*
*作者:呆萌老师
*☑csdn认证讲师
*☑51cto高级讲师
*☑腾讯课堂认证讲师
*☑网易云课堂认证讲师
*☑华为开发者学堂认证讲师
*☑爱奇艺千人名师计划成员
*在这里给大家分享技术、知识和生活
*各种干货,记得关注哦!
*vx:it_daimeng
*/

  

一个学生对应一个班级的情况

方法1:

 <!-- 多表查询  -->

   <!-- 用户表和班级表进行内连接查询 -->

   <!-- 不能用resultType 要用resultMap 里面的类型需要自己定义-->  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<select id="findUserinfoAndGrade" resultMap="userinfoMap" >
 
      select * from userinfo ,grade where userinfo.cid=grade.cid
 
   </select>
 
   <resultMap  type="Userinfo" id="userinfoMap">
 
       <id property="uid" column="uid" />
 
       <result property="name" column="name" />
 
       <result property="age" column="age" />
 
       <!-- association 使用在一对一或多对一时 代表一个学生有一个班级 -->
 
       <!-- association中 属性一定要设置javaType -->
 
       <association property="grade" javaType="Grade">
 
          <id property="cid" column="cid" />
 
          <result property="cname" column="cname" />
 
       </association>
 
   </resultMap>

  

 

 

方法2:

  <!-- 将内连接拆分成两个查询语句 -->

   <select id="selectUserinfo" resultMap="userinfoMap2" >

        select * from userinfo

   </select>

   <select id="selectGradeByCid" parameterType="int" resultType="Grade" >

        select * from grade where cid=#{cid}

   </select>

  

   <resultMap  type="Userinfo" id="userinfoMap2">

       <id property="uid" column="uid" />

       <result property="name" column="name" />

       <result property="age" column="age" />

       <association property="grade" column="cid" select="selectGradeByCid">

      

       </association>

   </resultMap>

 

一个班级对应多个学生的情况

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<select id="findGradeAndUserinfo" resultMap="gradeMap">
 
     select * from grade,userinfo where grade.cid = userinfo.cid
 
  </select>
 
  <resultMap type="Grade" id="gradeMap">
 
     <id property="cid" column="cid"/>
 
     <result property="cname" column="cname"/>
 
             association都有property属性,
 
             association里面javaType属性 ;collection里面有ofType属性
 
     <collection property="userlist" ofType="Userinfo">
 
        <id property="uid" column="uid"/>
 
        <result property="name" column="name"/>
 
        <result property="age" column="age"/>
 
     </collection>
 
  </resultMap>

  

  

   <!-- 分开查询 -->  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<select id="selectGrade" resultMap="gradeMap1">
 
     select * from grade
 
  </select>
 
  <select id="selectUserinfo" parameterType="int" resultType="Userinfo">
 
     select * from userinfo where cid=#{cid}
 
  </select>
 
  
 
  <resultMap type="Grade" id="gradeMap1">
 
     <id property="cid" column="cid"/>
 
     <result property="cname" column="cname"/>
 
     <collection property="userlist" column="cid" select="selectUserinfo">
 
     <id property="uid" column="uid"/>
 
        <result property="name" column="name"/>
 
        <result property="age" column="age"/>
 
     </collection>
 
  </resultMap>

  

 

 

Dao:

 //1.读取配置文件

        InputStream is=this.getClass().getClassLoader().getResourceAsStream("mybatis.xml");

       

        //2.获取会话工厂创建者

       SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();

       

       //3.获取会话工厂

       SqlSessionFactory sqlSessionFactory=sqlSessionFactoryBuilder.build(is);

       

       //4.获取会话

       SqlSession sqlSession=sqlSessionFactory.openSession();

       

       //5.查询

       List<Userinfo>  userlist=sqlSession.selectList("com.test.bean.UserinfoMapper.selectUserinfo",1);

             

       //关闭会话

       sqlSession.close();

       for(int i=0;i<userlist.size();i++)

         System.out.println(userlist.get(i).toString());

 

posted @   呆萌老师  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示