|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 一.缓存 作用:应用查询上,内存中的块区域。 缓存查询结果,减少与数据库的交互,从而提高运行效率。 1.SqlSession 缓存 |---------------------------------------------------------------------------------------| | 1. 又称为一级缓存,mybatis自动开启。 2. 作用范围:同一个session对象,调用同一个<select>, 第一次访问数据库,缓存到session缓存区。 | 示例 |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | // 获取学生的 Mapper 对象 StudentMapper studentMapper=session .getMapper(StudentMapper.class); // 调用方法 , 获取所有的学生信息 List<Student> list=studentMapper.selectAll (); for (Student stu:list) { System.out .println(stu); } System.out .println("----------------------------------------------"); // 调用方法 , 获取所有的学生信息 List<Student> list2=studentMapper.selectAll(); for (Student stu:list2) { System.out .println(stu); } 效果: | 2.SqlSessionFactory 缓存 |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1. 又称为二级缓存,mybatis 不自动开启。 2. 作用范围:同一个SqlSessionFactory对象中所有session都可以获取。 3. 打开mapper.xml配置开启二级缓存 <cache readOnly="true"></cache> 4.只有当session.close()或commit()时, 才会将session缓存的数据刷新到factory二级缓存中。 5. 适用于:频繁查询,很少被修改。 | 实例 |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | SqlSession session =sessionFactory.openSession(); // 获取学生的 Mapper 对象 StudentMapper studentMapper=session.getMapper(StudentMapper.class); // 调用方法 , 获取所有的学生信息 List<Student> list=studentMapper.selectAll(); for (Student stu:list) { System.out .println(stu); } // 只有当 session.close() 或 commit(), 才会将 session 缓存的数据刷新到 factory 二级缓存中 session.close(); System.out .println("----------------------------------------------"); SqlSession session2 =sessionFactory.openSession(); StudentMapper studentMapper2=session2.getMapper(StudentMapper.class); // 调用方法 , 获取所有的学生信息 List<Student> list2=studentMapper2.selectAll(); for (Student stu:list2) { System.out .println(stu); } 效果: | 二.Mybatis注解 作用:简化mapper.xml配置,mapper.xml配置和注解可以共存 一般动态sql或复杂的sql语句,推荐使用mapper.xml配置 1. 常用注解 |---------------------|-----------------| | @Delete("delete语句") | 等价于<delete>标签 | | @Insert("insert语句") | 等价于<insert>标签 | | @Update("update语句") | 等价于<update>标签 | | @Select("select语句") | 等价于<select>标签 | 示例: |-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | @Delete("delete from student where sid=#{id}") public int deleteById(int id); @Select("select * from student") public List<Student> selectAll(); @Insert("insert into student values(#{sid},#{stuName},#{age},#{tid})") public int add(Student stu); @Update("update student set stu_name=#{stuName},age=#{age},tid=#{tid} where sid=#{sid} ") public int update(Student stu); | 三.PageHelper 分页插件 PageHelper 是 MyBatis 中比较著名的分页插件,它提供了多种分页方式(例如 MySQL 和 Oracle 分页方式),支持多种数据库,并且使用非常简单。 下面就介绍一下 PageHelper 的使用方式。 1.pom.xml引入依赖 |-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | <!-- 分页 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.11</version> </dependency> | 2.mybatis.xml配置文件中添加 PageHelper 的插件 |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | <!-- 添加 PageHelper 分页插件 --> <!--com.github.pagehelper.PageInterceptor 是 PageHelper 插件的名称, dialect 属性用于指定数据库类型(支持多种数据库) --> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="helperDialect" value="mysql "/> </plugin> </plugins> | 3.查询所有的老师信息的mapper |---------------------------------------------------------------------------------------------------| | public interface TeacherMapper { @Select("select * from teacher") List<Teacher> selectAll(); } | 4.在查询方法中使用分页插件查询当前页的商品信息 |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | @Test public void testTeacherPage() { TeacherMapper teacherMapper = session.getMapper(TeacherMapper.class); //开启分页功能,在查询之前 , 设置分页的当前页以及每页的条数 PageHelper. startPage (2,2); // 执行查询操作,获取 查询 数据 List<Teacher> allTeachers = teacherMapper.selectAll(); // 通过 PageInfo 封装 查询的list 数据集合以及分页相关的数据 PageInfo<Teacher> pageInfo = new PageInfo<>(allTeachers); System.out .println("pageInfo = " + pageInfo); long total = pageInfo.getTotal(); // 获取总记录数 System.out .println("total = " + total); int pages = pageInfo.getPages(); // 获取总页数 System.out .println("pages = " + pages); int pageNum = pageInfo.getPageNum(); // 获取当前页码 System.out .println("pageNum = " + pageNum); int pageSize = pageInfo.getPageSize(); // 获取每页显示记录数 System.out .println("pageSize = " + pageSize); List<Teacher> list = pageInfo.getList(); // 获取查询页的数据集合 for (Teacher t:list) { System.out .println(t); } } | PageInfo 对象获取分页相关信息的属性 |------------------|-----------------------| | 方法名 | 描述 | | pageNum | 当前页的页号 | | pageSize | 每页显示的条数 | | size | 当前页的实际条数 | | total | 总条数 | | pages | 总页数 | | prePage | 上一页的页号 | | nextPage | 下一页的页号 | | isFirstPage | 是否为第一页 | | isLastPage | 是否为最后一页 | | hasPreviousPage | 是否存在上一页 | | hasNextPage | 是否存在下一页 | | navigatePages | 导航分页的页码数 | | navigatepageNums | 导航分页的页码,[1,2,3,4,5] | |
缓存、注解、分页
帅哥杨振海2024-11-06 8:32
相关推荐
格瑞@_@2 小时前
11.Three.js使用indexeddb前端缓存模型优化前端加载效率Lyqfor4 小时前
Redis学习:BitMap/HyperLogLog/GEO案例 、布隆过滤器BloomFilter、缓存预热+缓存雪崩+缓存击穿+缓存穿透新手小袁_J4 小时前
RabbitMQ的发布订阅模式码上一元5 小时前
缓存淘汰策略:Redis中的内存管理艺术wang090715 小时前
netty之实现一个redis的客户端LeonNo1115 小时前
【软考】Redis不同的数据类型和应用场景。Karoku06615 小时前
【缓存与加速技术实践】Web缓存代理与CDN内容分发网络w_t_y_y16 小时前
Mybatis中的缓存Karoku06620 小时前
【缓存与加速技术实践】Redis哨兵