《Mysql應(yīng)用mybatis分頁插件pageHelper詳解及簡單實例》要點:
本文介紹了Mysql應(yīng)用mybatis分頁插件pageHelper詳解及簡單實例,希望對您有用。如果有疑問,可以聯(lián)系我們。
MYSQL數(shù)據(jù)庫mybatis分頁插件pageHelper詳解及簡單實例
MYSQL數(shù)據(jù)庫工作的框架spring springmvc mybatis3
MYSQL數(shù)據(jù)庫首先使用分頁插件必須先引入maven依賴,在pom.xml中添加如下
MYSQL數(shù)據(jù)庫
<!-- 分頁助手 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.7.5</version>
</dependency>
MYSQL數(shù)據(jù)庫其次需要在配置文件中添加配置,有兩種方式
MYSQL數(shù)據(jù)庫1,新建mybatis-config.xml內(nèi)容如下
MYSQL數(shù)據(jù)庫
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 分頁助手 -->
<plugins>
<!-- com.github.pagehelper為PageHelper類所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 數(shù)據(jù)庫方言 -->
<property name="dialect" value="MySQL"/>
<!-- 設(shè)置為true時,使用RowBounds分頁會進行count查詢 會去查詢出總數(shù) -->
<property name="rowBoundsWithCount" value="true"/>
</plugin>
</plugins>
</configuration>
MYSQL數(shù)據(jù)庫在spring-mybatis.xml中添加一個bean屬性
MYSQL數(shù)據(jù)庫
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
MYSQL數(shù)據(jù)庫加載全局的配置文件
MYSQL數(shù)據(jù)庫
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
MYSQL數(shù)據(jù)庫配置mapper的掃描,找到所有的mapper.xml映射文件.
MYSQL數(shù)據(jù)庫
<property name="mapperLocations" value="classpath:com/lyitong/mapping/*.xml"></property>
MYSQL數(shù)據(jù)庫備注:如果你的mybatis-config.xml配置文件開啟了如下別名配置:
MYSQL數(shù)據(jù)庫
<typeAliases>
<!-- javabean 的首字母小寫的非限定類名來作為它的別名(其實別名是不去分大小寫的).也可在javabean 加上注解@Alias 來自定義別名, 例如: @Alias(student) -->
<package name="com.lyt.usermanage.mapper"/>
</typeAliases>
MYSQL數(shù)據(jù)庫那么你的spring和mybatis整合文件就得加上相應(yīng)的屬性,否則會造成mybatis配置文件加載不成功報異常,如下:
MYSQL數(shù)據(jù)庫
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 加載全局的配置文件 -->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
<!-- 配置mapper的掃描,找到所有的mapper.xml映射文件. -->
<property name="mapperLocations" value="classpath:com/lyt/usermanage/mapper/*.xml"></property>
<!-- 配置類型別名 -->
<property name="typeAliasesPackage" value="classpath:com/lyt/usermanage/pojo/*"></property>
</bean>
MYSQL數(shù)據(jù)庫相比于上面的配置我們這里多了一步
MYSQL數(shù)據(jù)庫
<property name="typeAliasesPackage" value="classpath:com/lyt/usermanage/pojo/*"></property>
MYSQL數(shù)據(jù)庫配置的時候要注意mybatis配置文件和spring-mybatis整合文件的屬性要統(tǒng)一.
MYSQL數(shù)據(jù)庫2.如上操作配置完成,下面第二種方法
MYSQL數(shù)據(jù)庫直接在spring-mybatis.xml中配置如下屬性
MYSQL數(shù)據(jù)庫
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:com/lyitong/mapping/*.xml"></property>
<!-- pageHelper 分頁插件 -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=mysql
rowBoundsWithCount=true
</value>
</property>
</bean>
</array>
</property>
</bean>
MYSQL數(shù)據(jù)庫配置文件加載好之后,就可以直接使用,具體使用代碼如下:
MYSQL數(shù)據(jù)庫
PageHelper.startPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
List<LytBbsTz> publishTz = bbsTzDao.getPublishTz(userId);
PageInfo<LytBbsTz> info = new PageInfo<LytBbsTz>(publishTz);
map.put("status", 1);
map.put("tzList", info.getList());
return map;
MYSQL數(shù)據(jù)庫前臺需要傳入的參數(shù)是當(dāng)前頁和頁面顯示數(shù)目,當(dāng)然頁面顯示數(shù)目也可以后臺規(guī)定,一般在接收參數(shù)時最好加上默認配置如下:
MYSQL數(shù)據(jù)庫
@RequestParam(defaultValue="1",value="currentPage")String currentPage, @RequestParam(defaultValue="10",value="pageSize")String pageSize
MYSQL數(shù)據(jù)庫這是如果接收參數(shù)為空字符串時它自身默認顯示的頁面和條數(shù),這個可以自己規(guī)定
MYSQL數(shù)據(jù)庫以上就是pageHelper的簡單應(yīng)用
MYSQL數(shù)據(jù)庫感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/1400.html