Maven下,ssm框架Mybatis-pagehelper的使用

Mybatis-pagehelper一个非常好用的分页组件

  1. 在pom.xml文件下导入需要的包(在https://mvnrepository.com/ 搜索更多版本,个人使用4.2.0有报错,更改的4.1.6)
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
			<version>4.1.6</version>
		</dependency>

2.在spring配置文件下配置sqlsessionfactory

<!--创建sqlSession工厂对象 -->
	<!--在此之前要保证mybatis的包和mybatis给spring提供的插件包都要导入到项目中 -->
	<bean id="sessionFactory"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<!--配置数据层查询文件(XxxMapper.xml文件)的地址 -->
		<property name="mapperLocations"
			value="classpath:com/lifei/shop/dao/*Mapper.xml" />
		<!--将Spring创建的c3p0连接池对象 注入给工厂对象 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 分页插件 -->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageHelper">
                    <property name="properties">
                        <value>
                            dialect=mysql
                        </value>
                    </property>
                </bean>
            </array>
        </property>
	</bean>

这种方法,可以在mybatis全局配置文件下,添加监听器。

3.控制层使用

@RequestMapping("product-list.do")
	@ResponseBody
	public Result productList(@RequestParam(value="pageNum",defaultValue="1")int pageNum,
			@RequestParam(value="pageSize",defaultValue="5")int pageSize){
		PageHelper.startPage(pageNum,pageSize);
		List<Product> proList = productService.list();
		PageInfo<Product> pageInfo = new PageInfo<Product>(proList);
		pageInfo.setList(proList);
		Result result = new Result(pageInfo);
		return result;
	}

Result result = new Result(pageInfo);下封装了pageInfo
4.显示层

$.ajax({
		url : "product-list.do",
		type : "post",
		data : {
			"pageNum" : pageIndex(1),
			"pageSize" : 5
		},
		dataType : "json",
		success : function(result) {
			/* alert(result.pageInfo.list[1].name); */
			$(".datatr").remove();
			for(var i=0;i<result.pageInfo.list.length;i++){
				//组装html
				var html='<tr class="datatr">';
				html+='<td>'+result.pageInfo.list[i].pno+'</td>';
				html+='<td>'+result.pageInfo.list[i].name+'</td>';
				html+='<td><img src="'+result.pageInfo.list[i].image+'"></td>';
				html+='<td>'+result.pageInfo.list[i].price+'</td>';
				html+='<td>'+result.pageInfo.list[i].num+'</td>';
				html+='<td><a class="addShopping" href="javascropt:" onclick="shopping('+result.pageInfo.list[i].pno+')">加入购物车</a></td>';
				html+='</tr>';
				//将html作为新的标签添加到最后一行之前
				$("#last").before(html);
			}
			});

有疑问QQ:550611343

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值