MyBatis分页插件、关联映射

添加pageHelper到pom

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.2.0</version>
</dependency>

 在mybatis-conf.xml配置

<plugins>
    <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>

分页查询测试@Test

 //开启分页
    PageHelper.startPage(1,4);
    //执行查询
    //获取分页信息
    PageInfo<Book> pageInfo = new PageInfo<Book>(  bookDAO.selectBooks() );

    //PageInfo对象中封装了分页所需的所有数据:存储查询结果的集合、总记录数、总页数、当前页、每页条数

 PageHelper.startPage(1,3);
 PageInfo<ShangPing> pageInfo = new PageInfo<ShangPing>(spDao.queryAll());
 System.out.println(pageInfo);
 System.out.println("总记录数"+pageInfo.getTotal());
 System.out.println("总页数"+pageInfo.getPages());
List<ShangPing> list = pageInfo.getList();
 for (int i = 0; i < list.size() ; i++) {
     System.out.println(list.get(i));
 }

 测试

 PageInfo{pageNum=1, pageSize=3, size=3, startRow=1, endRow=3, total=8, pages=3, list=Page{count=true, pageNum=1, pageSize=3, startRow=0, endRow=3, total=8, pages=3, reasonable=false, pageSizeZero=false}[ShangPing(id=4, name=测试, price=23.3, type=测试, datas=2000-01-01 00:00:00.0), ShangPing(id=5, name=测试1, price=23.33, type=测试1, datas=2000-02-11 00:00:00.0), ShangPing(id=6, name=测试9, price=12.22, type=测试9, datas=2022-02-01 17:51:00.0)], prePage=0, nextPage=2, isFirstPage=true, isLastPage=false, hasPreviousPage=false, hasNextPage=true, navigatePages=8, navigateFirstPage=1, navigateLastPage=3, navigatepageNums=[1, 2, 3]}
总记录数8
总页数3
ShangPing(id=4, name=测试, price=23.3, type=测试, datas=2000-01-01 00:00:00.0)
ShangPing(id=5, name=测试1, price=23.33, type=测试1, datas=2000-02-11 00:00:00.0)
ShangPing(id=6, name=测试9, price=12.22, type=测试9, datas=2022-02-01 17:51:00.0)

MyBatis的关联映射配置

Test2_14

 多对一: 学生—班级

  • 在多的一端添加外键与一的一端的主键进行关联

  • 在Student类私有一个 private Classes getClazz;
    //多对一
  • StudentDao
    • public List<Students> queryAllStudent(int sId);
  • <mapper namespace="Dao.StudentDao">
        <!--查询-->
        <resultMap id="queryStudent" type="Utility.Students">
            <id column="s_id" property="sId"></id>
            <result column="s_name" property="stuName"></result>
            <result column="s_gender" property="stuGender"></result>
            <result column="s_age" property="stuAge"></result>
            <result column="s_class" property="sClass"></result>
            <association property="getClazz" column="s_class" select="Dao.ClassesDao.queryClassAndStudent"></association>
    //这是配置private Classes getClazz 私有属性的resultMap的方式;把ClassesDao.queryClassAndStudent 查询到的结果设置进来。
    //
        </resultMap>
        <select id="queryAllStudent" parameterType="int" resultMap="queryStudent">
            SELECT * from test_student where s_id= #{sId}
        </select>
    </mapper>

一对多: 班级—学生

  • 在多的一端添加外键与一的一端的主键进行关联

  • ClassesDao:在Classes类私有一个 private List<Student> getAllStudent; //一对多

  • ClassesMapper: <resultMap id="classMap" type="com.qfedu.entity.Clazz">
            <id column="class_id" property="classId"/>
            <result column="class_name" property="className"/>
            <result column="class_desc" property="classDesc"/>
            <collection property="students" column="class_id" select="com.qfedu.dao.ClassDAO.selectStudentsByCid"/>//查询到所有学生

  • //collection:用来接收集合;
        </resultMap>

        <select id="selectClassById" resultMap="classMap">
            select class_id,class_name,class_desc
            from classes
            where class_id=#{classId}
        </select>

 连接查询:collection:用oftype

 子查询:collection:用当前查询的值作为子查询的查询条件:select

 连接查询:一次连接,查询时间长;子查询:多次连接,差寻时间短

 多对多:学生—课程 用户—角色

可以看成是多对一:只是在一那一端也是多

  • 建立第三张关系表分别与两张数据表建立外键关联

要求:通过Student查询到其所选的所有课程 

连接查询和子查询

连接查询

连接查询指定 ofType

 子查询 指定property :子查询的结果courseList by stu_num

<mapper namespace="studentDao">
    <!--查询-->
    <resultMap id="studentMap3" type="student">
        <id column="stu_num" property="stuNum"/>
        <result column="stu_name" property="stuName"/>
        <result column="stu_age" property="stuAge"/>
        <collection property="courseList" column="stu_num" select="CourseDao.selectCoursesByStunum"></collection>//子查询的结果集List<Course>
<!--        子查询-->
    </resultMap>
    <select id="selectStudentByStunum" resultMap="studentMap3">
        select stu_num,stu_name,stu_age
        from students
        where stu_num=#{stuNum}
    </select>
</mapper>

子查询:对于courseList

 <mapper namespace="CourseDao"> <!--查询-->

<resultMap id="courseMap" type="Course">

<id column="course_id" property="courseId"/>

<result column="course_name" property="courseName"/>

</resultMap>

<select id="selectCoursesByStunum" resultMap="courseMap">

select course_id,course_name from courses where course_id in

(select cid from tb_student_course where snum=#{stuNum})//返回List<Course>:

查询到一个学生选的所有课

</select>

</mapper>

 子查询结果:

student(stuNum=1003, stuName=zdq, stuAge=20, courseList=

[Course(courseId=1, courseName=Java, studentList=null),//studentList为null ;不能出现死循环

Course(courseId=2, courseName=C++, studentList=null),

Course(courseId=3, courseName=MySQL, studentList=null)])
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值