国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > MySQL > 正文

mybatis分頁插件pageHelper詳解及簡單實(shí)例

2024-07-24 13:13:18
字體:
供稿:網(wǎng)友

mybatis分頁插件pageHelper詳解及簡單實(shí)例

工作的框架spring springmvc mybatis3

首先使用分頁插件必須先引入maven依賴,在pom.xml中添加如下

<!-- 分頁助手 --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>3.7.5</version></dependency>

其次需要在配置文件中添加配置,有兩種方式

1,新建mybatis-config.xml內(nèi)容如下

<?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時(shí),使用RowBounds分頁會(huì)進(jìn)行count查詢 會(huì)去查詢出總數(shù) -->    <property name="rowBoundsWithCount" value="true"/>  </plugin></plugins> </configuration>

在spring-mybatis.xml中添加一個(gè)bean屬性

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" />

加載全局的配置文件

<property name="configLocation" value="classpath:mybatis-config.xml"></property>

配置mapper的掃描,找到所有的mapper.xml映射文件。

<property name="mapperLocations" value="classpath:com/lyitong/mapping/*.xml"></property>

備注:如果你的mybatis-config.xml配置文件開啟了如下別名配置:

<typeAliases>    <!-- javabean 的首字母小寫的非限定類名來作為它的別名(其實(shí)別名是不去分大小寫的)。也可在javabean 加上注解@Alias 來自定義別名, 例如: @Alias(student) -->    <package name="com.lyt.usermanage.mapper"/>  </typeAliases>

那么你的spring和mybatis整合文件就得加上相應(yīng)的屬性,否則會(huì)造成mybatis配置文件加載不成功報(bào)異常,如下:

 <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>

相比于上面的配置我們這里多了一步

    <property name="typeAliasesPackage" value="classpath:com/lyt/usermanage/pojo/*"></property>

配置的時(shí)候要注意mybatis配置文件和spring-mybatis整合文件的屬性要統(tǒng)一。

2.如上操作配置完成,下面第二種方法

直接在spring-mybatis.xml中配置如下屬性

<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>

配置文件加載好之后,就可以直接使用,具體使用代碼如下:

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;

前臺(tái)需要傳入的參數(shù)是當(dāng)前頁和頁面顯示數(shù)目,當(dāng)然頁面顯示數(shù)目也可以后臺(tái)規(guī)定,一般在接收參數(shù)時(shí)最好加上默認(rèn)配置如下:

@RequestParam(defaultValue="1",value="currentPage")String currentPage, @RequestParam(defaultValue="10",value="pageSize")String pageSize

這是如果接收參數(shù)為空字符串時(shí)它自身默認(rèn)顯示的頁面和條數(shù),這個(gè)可以自己規(guī)定

以上就是pageHelper的簡單應(yīng)用

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關(guān)教程知識閱讀請移步到MYSQL教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 虞城县| 梁平县| 鹰潭市| 无为县| 淮阳县| 灌云县| 济南市| 察隅县| 柞水县| 义乌市| 邳州市| 武夷山市| 五家渠市| 叶城县| 永昌县| 金沙县| 武隆县| 泰宁县| 屏山县| 南陵县| 凯里市| 房山区| 鱼台县| 四平市| 乌鲁木齐县| 尉氏县| 来宾市| 八宿县| 陵川县| 抚宁县| 南通市| 阿尔山市| 湘乡市| 慈溪市| 桃园县| 信阳市| 郁南县| 阿坝县| 永平县| 高尔夫| 察隅县|