mybatis分页插件pageHelper的使用示例
小编给大家分享一下mybatis分页插件pageHelper的使用示例,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
工作的框架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内容如下
<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTDConfig3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!--分页助手--><plugins><!--com.github.pagehelper为PageHelper类所在包名--><plugininterceptor="com.github.pagehelper.PageHelper"><!--数据库方言--><propertyname="dialect"value="MySQL"/><!--设置为true时,使用RowBounds分页会进行count查询会去查询出总数--><propertyname="rowBoundsWithCount"value="true"/></plugin></plugins></configuration>
在spring-mybatis.xml中添加一个bean属性
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><propertyname="dataSource"ref="dataSource"/>
加载全局的配置文件
<propertyname="configLocation"value="classpath:mybatis-config.xml"></property>
配置mapper的扫描,找到所有的mapper.xml映射文件。
<propertyname="mapperLocations"value="classpath:com/lyitong/mapping/*.xml"></property>
备注:如果你的mybatis-config.xml配置文件开启了如下别名配置:
<typeAliases><!--javabean的首字母小写的非限定类名来作为它的别名(其实别名是不去分大小写的)。也可在javabean加上注解@Alias来自定义别名,例如:@Alias(student)--><packagename="com.lyt.usermanage.mapper"/></typeAliases>
那么你的spring和mybatis整合文件就得加上相应的属性,否则会造成mybatis配置文件加载不成功报异常,如下:
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><propertyname="dataSource"ref="dataSource"/><!--加载全局的配置文件--><propertyname="configLocation"value="classpath:mybatis/mybatis-config.xml"></property><!--配置mapper的扫描,找到所有的mapper.xml映射文件。--><propertyname="mapperLocations"value="classpath:com/lyt/usermanage/mapper/*.xml"></property><!--配置类型别名--><propertyname="typeAliasesPackage"value="classpath:com/lyt/usermanage/pojo/*"></property></bean>
相比于上面的配置我们这里多了一步
<propertyname="typeAliasesPackage"value="classpath:com/lyt/usermanage/pojo/*"></property>
配置的时候要注意mybatis配置文件和spring-mybatis整合文件的属性要统一。
2.如上操作配置完成,下面第二种方法
直接在spring-mybatis.xml中配置如下属性
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><propertyname="dataSource"ref="dataSource"/><propertyname="mapperLocations"value="classpath:com/lyitong/mapping/*.xml"></property><!--pageHelper分页插件--><propertyname="plugins"><array><beanclass="com.github.pagehelper.PageHelper"><propertyname="properties"><value>dialect=mysqlrowBoundsWithCount=true</value></property></bean></array></property></bean>
配置文件加载好之后,就可以直接使用,具体使用代码如下:
PageHelper.startPage(Integer.parseInt(currentPage),Integer.parseInt(pageSize));List<LytBbsTz>publishTz=bbsTzDao.getPublishTz(userId);PageInfo<LytBbsTz>info=newPageInfo<LytBbsTz>(publishTz);map.put("status",1);map.put("tzList",info.getList());returnmap;
前台需要传入的参数是当前页和页面显示数目,当然页面显示数目也可以后台规定,一般在接收参数时最好加上默认配置如下:
@RequestParam(defaultValue="1",value="currentPage")StringcurrentPage,@RequestParam(defaultValue="10",value="pageSize")StringpageSize
这是如果接收参数为空字符串时它自身默认显示的页面和条数,这个可以自己规定
看完了这篇文章,相信你对“mybatis分页插件pageHelper的使用示例”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。