?一.緩存 ? ? ? ? 作用:應(yīng)用查詢上,內(nèi)存中的塊區(qū)域。 ? ? ? ? ? ? ? 緩存查詢結(jié)果,減少與數(shù)據(jù)庫的交互,從而提高運(yùn)行效率。 ??? ? ?1.SqlSession 緩存 1. 又稱為一級緩存,mybatis自動開啟。 2. 作用范圍:同一個session對象,調(diào)用同一個<select>, ????????????第一次訪問數(shù)據(jù)庫,緩存到session緩存區(qū)。 |
???示例 //獲取學(xué)生的Mapper對象 StudentMapper studentMapper=session.getMapper(StudentMapper.class); //調(diào)用方法,獲取所有的學(xué)生信息 List<Student> list=studentMapper.selectAll(); for (Student stu:list) { ????System.out.println(stu); } System.out.println("----------------------------------------------"); //調(diào)用方法,獲取所有的學(xué)生信息 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.只有當(dāng)session.close()或commit()時, ???才會將session緩存的數(shù)據(jù)刷新到factory二級緩存中。 5. 適用于:頻繁查詢,很少被修改。 |
?????實例 SqlSession session=sessionFactory.openSession(); //獲取學(xué)生的Mapper對象 StudentMapper studentMapper=session.getMapper(StudentMapper.class); //調(diào)用方法,獲取所有的學(xué)生信息 List<Student> list=studentMapper.selectAll(); for (Student stu:list) { ????System.out.println(stu); } //只有當(dāng)session.close()或commit(),才會將session緩存的數(shù)據(jù)刷新到factory二級緩存中 session.close(); System.out.println("----------------------------------------------"); SqlSession session2=sessionFactory.openSession(); StudentMapper studentMapper2=session2.getMapper(StudentMapper.class); //調(diào)用方法,獲取所有的學(xué)生信息 List<Student> list2=studentMapper2.selectAll(); for (Student stu:list2) { ????System.out.println(stu); } 效果:  | 二.Mybatis注解 ? ? ??作用:簡化mapper.xml配置,mapper.xml配置和注解可以共存 ? ? ? ? ? ? ? ? ?一般動態(tài)sql或復(fù)雜的sql語句,推薦使用mapper.xml配置 ? ? ? ?1. 常用注解 @Delete(“delete語句”) | 等價于<delete>標(biāo)簽 | @Insert(“insert語句”) | 等價于<insert>標(biāo)簽 | @Update(“update語句”) | 等價于<update>標(biāo)簽 | @Select(“select語句”) | 等價于<select>標(biāo)簽 |
? ? ? ? 示例: @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 分頁方式),支持多種數(shù)據(jù)庫,并且使用非常簡單。 ???下面就介紹一下 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 屬性用于指定數(shù)據(jù)庫類型(支持多種數(shù)據(jù)庫)--> <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.在查詢方法中使用分頁插件查詢當(dāng)前頁的商品信息 @Test public void testTeacherPage() { ????TeacherMapper teacherMapper = session.getMapper(TeacherMapper.class); ????//開啟分頁功能,在查詢之前,設(shè)置分頁的當(dāng)前頁以及每頁的條數(shù) ????PageHelper.startPage(2,2); ????//執(zhí)行查詢操作,獲取查詢數(shù)據(jù) ????List<Teacher> allTeachers = teacherMapper.selectAll(); ????//通過PageInfo封裝查詢的list數(shù)據(jù)集合以及分頁相關(guān)的數(shù)據(jù) ????PageInfo<Teacher> pageInfo = new PageInfo<>(allTeachers); ????System.out.println("pageInfo = " + pageInfo); ????long total = pageInfo.getTotal(); // 獲取總記錄數(shù) ????System.out.println("total = " + total); ????int pages = pageInfo.getPages(); ?// 獲取總頁數(shù) ????System.out.println("pages = " + pages); ????int pageNum = pageInfo.getPageNum(); // 獲取當(dāng)前頁碼 ????System.out.println("pageNum = " + pageNum); ????int pageSize = pageInfo.getPageSize(); // 獲取每頁顯示記錄數(shù) ????System.out.println("pageSize = " + pageSize); ????List<Teacher> list = pageInfo.getList(); //獲取查詢頁的數(shù)據(jù)集合 ????for (Teacher t:list) { ????????System.out.println(t); ????} } |
PageInfo對象獲取分頁相關(guān)信息的屬性 方法名 | 描述 | pageNum | 當(dāng)前頁的頁號 | pageSize | 每頁顯示的條數(shù) | size | 當(dāng)前頁的實際條數(shù) | total | 總條數(shù) | pages | 總頁數(shù) | prePage | 上一頁的頁號 | nextPage | 下一頁的頁號 | isFirstPage | 是否為第一頁 | isLastPage | 是否為最后一頁 | hasPreviousPage | 是否存在上一頁 | hasNextPage | 是否存在下一頁 | navigatePages | 導(dǎo)航分頁的頁碼數(shù) | navigatepageNums | 導(dǎo)航分頁的頁碼,[1,2,3,4,5] | |