首页 > 其他分享 >MyBatis - 基础学习7 - 多表查询

MyBatis - 基础学习7 - 多表查询

时间:2022-11-20 20:47:12浏览次数:37  
标签:mapper 实体类 多表 查询 sqlSession student getstudent MyBatis teacher

一.按照查询嵌套处理

1.写接口

 List<Student> getstudent();

 

2.在mapper.xml中写相关的sql语句

<!--
思路:
1.查询所有的学生信息
2.根据查询出来的学生的tid,寻找对应的老师
-->
<select id="getstudent" resultMap="StudentTeacher">
    SELECT * FROM student
</select>
    <resultMap id="StudentTeacher" type="Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <!--复杂的属性单独处理:
        对象:association
        集合:collection
        -->
        <association property="teacher" column="tid" javaType="Teacher" select="getteacher"/>
    </resultMap>
    <select id="getteacher" resultType="Teacher">
        SELECT * from teacher where id = #{tid}
    </select>

 

多对一:

多个学生对应一个老师,所以查询的思路是要把学生全部查出来去找对应的老师

由于这是不同的表查询,所以在返回值上:就不能简单的返回实体类(student,teacher是两个实体类,不能一下返回),我们就需要用到结果集映射resultMap

由于是多对一;返回值类型依旧是学生(student),普通的字段就是一一对应映射,但是对于要找的老师来说,在不同表中,这是一个对象,所以需要另外的关键字:association,专门负责对象映射

property:实体类中的属性      column:数据库中的字段     javaType:实体类中的属性在那个表中,就写它对饮的实体类    select:(子查询)这个查询的结果要有与column(数据库字段)对应的字段,他们才可以联系起来,这里是(student中的tid  =   teacher中的id)

子查询负责和column进行对应就好了,其中:id=#{ tid } 这个id(teacher表中的)会自动去匹配上面的column:tid(student表中的)

3.测试:

    @Test
    public void test1(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> getstudent = mapper.getstudent();
        for (Student student : getstudent) {
            System.out.println(student);
        }
        sqlSession.close();
    }

 

二.按照结果嵌套处理

1.先写对应的接口

 List<Student> getstudent();

 

2.在mapper.xml中写对应的方法

<!--按照结果嵌套处理-->
    <select id="getstudent" resultMap="studentteacher">
SELECT s.id sid,s.name sam,t.name tam
from student s,teacher t
WHERE s.tid=t.id
    </select>
    <resultMap id="studentteacher" type="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sam"/>
        <association property="teacher" javaType="Teacher">
            <result property="name" column="tam"/>
        </association>

    </resultMap>

 

这种写法的解题思路就是sql语句的查询方式

语句其实没什么大问题,因为就是淳朴的sql语句,直接写里面就行了,问题在于mybayis的返回值又是两个表中的字段;所以又要用到结果集映射

前面的步骤都是一样的;property对应student实体类中的属性  column对应数据库中的字段

在使用association   property:student实体类中的字段   javaType:就是property的返回值是那个表里面的,然后还要在里面写:javaType对应的实体类:中的属性要和数据库的字段对应(由于取了别名,所以用:tam)

3.测试

    @Test
    public void test1(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> getstudent = mapper.getstudent();
        for (Student student : getstudent) {
            System.out.println(student);
        }
        sqlSession.close();
    }

 

 

====一对多(集合)=====

一.按照结果嵌套处理

1.编写接口

 List<Teacher> getstudent();

 

2.在对应的mapper.xml中写sql语句

<select id="getstudent" resultMap="Teacher1">
SELECT s.id sid,s.name sam,t.name tam FROM student s,teacher t where s.tid=t.id
</select>
    <!--
    javaType=“”  指定属性的类型
    集合中的泛型信息用ofType
    -->
<resultMap id="Teacher1" type="Teacher">
    <collection property="student" ofType="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sam"/>
    </collection>
</resultMap>

 

这里的一对多和多对一都是一个概念,只不过是结果集映射不同

对象:association

集合:collection

3.测试

@Test
    public void test(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
    List<Teacher> getstudent = mapper.getstudent();
    for (Teacher teacher : getstudent) {

        System.out.println(teacher);
    }
    sqlSession.close();
}

 

二.按照查询嵌套处理

1.编写接口

 List<Teacher> getstudent();

 

2.在mapper中写sql

<select id="getstudent" resultMap="Teacher1">
SELECT * FROM teacher
</select>
    <!--
    javaType=“”  指定属性的类型
    集合中的泛型信息用ofType
    -->
<resultMap id="Teacher1" type="Teacher">
    <collection property="student" javaType="ArrayList" ofType="Student" column="id" select="sell">
    </collection>
</resultMap>
    <select id="sell" resultType="Student">
        SELECT  * from student where tid =#{id}
    </select>

 

子查询相对来说没有直接将其它表的字段和java类中的属性对应,我们就需要加上javaType,来告诉编译器,也就是property的类型,在Teacher这个实体类定义了是List,也就是数组(最准确是一个集合),

column就是子查询语句要和主查询产生关联的一个字段(它们都是Student类中的)

3.测试

@Test
    public void test(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
    List<Teacher> getstudent = mapper.getstudent();
    for (Teacher teacher : getstudent) {

        System.out.println(teacher);
    }
    sqlSession.close();
}

 

标签:mapper,实体类,多表,查询,sqlSession,student,getstudent,MyBatis,teacher
From: https://www.cnblogs.com/5ran2yl/p/16909442.html

相关文章

  • 常见的数据查询
    常见的数据查询mysql中常见的数据查询1)groupby子句2)回溯统计3)分组排序4)having子句5)orderby子句1、groupby子句groupby子句:分组统计,根据某个字段将所有......
  • MySQL子查询
    MySQL子查询什么是子查询subquery,通过select查询结果当作另外一条select查询的条件或者数据源子查询的分类根据子查询出现的位置分类from子查询:子查询出现在from后......
  • 异步查询工具 axios
    异步查询数据,自然是通过ajax查询,大家首先想起的肯定是jQuery。但jQuery与MVVM的思想不吻合,而且ajax只是jQuery的一小部分。因此不可能为了发起ajax请求而......
  • Django ORM 多表操作:一对一、一对多、多对多的增删改,基于对象/双下划线的跨表查询
    DjangomodelORM数据表相关操作分析思路,创建数据表对于表操作,表之间的关联关系,必须理解他们之间的关系,对于编程很重要。可以看看映射关系、外键和relationship查询,至少明......
  • Laravel:whereIn子查询
    文档​​https://learnku.com/docs/laravel/9.x/queries/12246#08034f​​示例如下$users=User::whereNotIn('id',function($query)use($user){$query->se......
  • mybatisplus和java8一些常用方法总结
    mybatis-plus:1、Stream.of();用于为给定元素创建顺序流。我们可以传递单个元素或多个元素。 2、Optional+Assert优雅判空  3、Optional+ MapUtils组合使用如果要使......
  • 027.1Mybatis注解开发方式
    1.常用注解  2.使用流程2.1  com.imooc.mybatis.dao包下创建接口packagecom.imooc.mybatis.dao;importcom.imooc.mybatis.dto.GoodsDTO;importcom.imooc.......
  • 026.MyBatis批处理
    1.goods.xml<!--INSERTINTOtable--><!--VALUES("a","a1","a2"),("b","b1","b2"),(....)--><insertid="batchInsert"parameterType="java.util.List">......
  • 025.MyBatis整合C3P0连接池
    1.pom.xml<dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.4</version>......
  • mybatis-plus与springboot整合
    一、mybatis开发问题需要自己写实体需要自己写xml文件和对应的xml中的sql那是不是存在一种对于通用的功能做很好支持的插件功能:mybatis-plus二、解决的问题:代码生......