框架:spring+springmvc+mybatis
使用maven來管理項目
1:引入依賴jar包
2:添加pageHelper插件
3:Dao層單元測試,書寫代碼
4:服務層單元測試:書寫代碼
5:Web層使用:書寫代碼
1:引入依賴jar包 pom.xml
<!-- 引入mybatis的 pagehelper 分頁插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency>
2:添加pageHelper插件
idea編輯器ctrl+N 查找類路徑,輸入pageIntercepter
spring 中叫做攔截器,在mybatis配置中叫做插件
此類路徑是配置插件的值如下com.github.pagehelper.PageInterceptor
<plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!--reasonable:分頁合理化參數,默認值為false,直接根據參數進行查詢。 當該參數設置為 true 時,pageNum<=0 時會查詢第一頁, pageNum>pages(超過總數時),會查詢最后一頁。--> <!--<property name="reasonable" value="true"/>--> </plugin> </plugins>
哪么mybatis插件要配置到mybatis全局配置變中,即mybatis-config.xml
特別強調, mybatis配置是有順序的,查看如下
ctrl+點擊<configuration>
可以查看配置文件在environments前邊一行,復制如上<plugins>……</plugins>
<!ELEMENT configuration (properties?, settings?, typeAliases?, typeHandlers?, objectFactory?, objectWrapperFactory?, reflectorFactory?, plugins?, environments?, databaseIdProvider?, mappers?)>
3:書寫代碼
a)在調用dao層書寫,采用junit單元測試
//測試分頁后,可刪除 @Test public void listRecruitsDaoPerPage(){
//連接數據庫,且數據配置如上步驟2:添加pageHelper插件 SqlSession sqlSession = getSqlSession(); //獲取代理對象 RecruitDao mapper = sqlSession.getMapper(RecruitDao.class);
//此包一定要放在調用數據庫之前,即mapper.listRecruitsDaoPerpage PageHelper.startPage(2,3); //2表示是起始頁,3表示每頁顯示行數 List<Recruit> recruits = mapper.listRecruitsDaoPerPage("南通"); //PageInfo封裝分頁信息,把上一行,記錄集recruits,通過構造函數傳遞給PageInfo計算
PageInfo<Recruit> pageInfo= new PageInfo<Recruit>(recruits,3); //3表示有很多分頁中,中列出三個頁碼 System.out.println(pageInfo); //打印能看到PageInfo對象提供所有頁碼屬性,相當多,提供周全
System.out.println("打印頁碼"+ Arrays.toString(pageInfo.getNavigatepageNums())); Integer i=0; for (Recruit recruit:recruits){ i+=1; System.out.println(i+"、"+recruit.getRid()+recruit.getJobTitle()+recruit.getCityCounty()); } }
4:服務層單元測試書寫代碼
因為服務層,單元測試,管理bean是通過applicationContent.xml 為了加載plugins,需在數據庫工廠中,加載插件如下配置,具體見如下代碼 <!--添加pageHelper 分頁過慮器, --> <property name="plugins"> </property>
<!--引入Mybatis文件--> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 添加pageHelper 分頁過慮器, begin--> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value> helperDialect=mysql </value> </property> </bean> </array> </property> <!-- 添加pageHelper 分頁過慮器, end -->
<property name="dataSource" ref="dataSource"/> <!-- 加載mybatis的配置文件 --> <!-- 當mybatis的xml文件和mapper接口不在相同包下時,需要用mapperLocations屬性指定xml文件的路徑。 *是個通配符,代表所有的文件,**代表所有目錄下 --> <!--強調:xxxMapper.xml配置在resources文件夾下,classpath*:沒加*報錯--> <property name="mapperLocations" value="classpath*:com/ibaiqi/spider/dao/mapper/*.xml" /> </bean>
5:Web層使用:書寫代碼
要在web.xml指向的spring-mvc.xml 添加插件配置
<!--引入Mybatis文件--> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <!--添加pageHelper插件 分頁過慮器, --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value> helperDialect=mysql </value> </property> </bean> </array> </property> <property name="dataSource" ref="dataSource"/> <!-- 加載mybatis的配置文件 --> <!-- 當mybatis的xml文件和mapper接口不在相同包下時,需要用mapperLocations屬性指定xml文件的路徑。 *是個通配符,代表所有的文件,**代表所有目錄下 --> <!--強調:xxxMapper.xml配置在resources文件夾下,classpath*:沒加*報錯--> <property name="mapperLocations" value="classpath*:com/ibaiqi/spider/dao/mapper/*.xml" /> </bean>