淘淘商城——day2(所有商品的展示:查询所有商品)
一 上节回顾
- 项目介绍
- 项目搭建
- 前端页面显示
二 今日计划
2.1、完成商品的展示
2.2、类目查询
2.3、搭建图片服务器
2.4、添加商品
三、商品展示
3.0、访问路径
我们发现访问路径是带项目名,而easyui访问后台controller的时候是不带项目名的,我们可以重新设置项目的访问路径
在taotao-manager项目pom文件添加以下配置
<build>
<!-- 配置插件 -->
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<!-- 设置tomcat的映射路径 -->
<path>/</path>
<!-- 端口号 -->
<port>8080</port>
</configuration>
</plugin>
</plugins>
</build>
然后报错,重新安装一下taotao-manager即可
3.1、分析
我们需要查询商品表tb_item,并且需要使用分页。所以需要配置mybatis的分页插件
3.2、JSP分析
我们点击查询商品,会将itm-list.jsp页面 加载到首页中
2.请求的参数:http://localhost:8080/item/list?page=1&rows=30 分页信息。(需要看官方的手册)
Page:当前第几页
Rows:每页的数据量
3.返回值: Json数据,数据格式:
Easyui中datagrid控件要求的数据格式为:
total:总数据量,rows:对象数据
{total:”2”,rows:[{“id”:”1”,”name”,”张三”},{“id”:”2”,”name”,”李四”}]}
我们查询出来的结果都要按照easyui的规范来传递数据,所以我们需要将查询出来的数据封装到EasyUI要求的格式中,我们这可以创建一个工具类来封装数据。 该实体类中包含total,rows
3.3、创建实体类
该实体类需要创建到taotao-common工程下。(因为是作为工具类)
package com.taotao.pojo;
import java.util.List;
public class EUDataGridResult {
private Long total;//存储当前查询,出来的总数据量
private List<?> rows; //存储查询出来的所有对象
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public List<?> getRows() {
return rows;
}
public void setRows(List<?> rows) {
this.rows = rows;
}
}
3.4、配置mybatis的分页插件
<?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>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库 -->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
3.5、错误
找不到dao层
解决方法:
修改taotao-mapper的pom文件
在pom文件中添加如下内容:
<build>
<!-- 不拦截properties和xml文件 -->
<resources>
<resource>
<!-- 文件路径 -->
<directory>src/main/java</directory>
<!-- 文件类型 -->
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<!-- 改为false -->
<filtering>false</filtering>
</resource>
</resources>
</build>
3.6、Dao层
单表操作,直接用****即可
3.7、Service层
接收controller传过来的page和rows,调用mapper查询数据并进行分页,返回EUDataGirdResult对象
取查询结果的总数量:
创建一个PageInfo类的对象,从对象中取分页信息。
package com.taotao.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.mapper.TbItemMapper;
import com.taotao.pojo.EUDataGridResult;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemExample;
import com.taotao.pojo.TbItemExample.Criteria;
import com.taotao.service.ItemService;
@Service
public class ItemServiceImpl implements ItemService {
//注入TbItemMapper
@Resource
private TbItemMapper TbItemMapper;
@Override
public EUDataGridResult selectItem(Integer page, Integer rows) {
//先设置分页策略
PageHelper.startPage(page, rows);
//查询,注意不能传null值
TbItemExample tbItemExample = new TbItemExample();
//Criteria criteria = tbItemExample.createCriteria();
List<TbItem> list = TbItemMapper.selectByExample(tbItemExample);
//设置分页
//存储数据
PageInfo<TbItem> pageInfo = new PageInfo<TbItem>(list);
EUDataGridResult result = new EUDataGridResult();
//list:所有的数据
//pageInfo:装的本页的数据
result.setRows(pageInfo.getList());
result.setTotal(pageInfo.getTotal());
return result;
}
}
3.8、Controller
接收前端传过来的page和rows,访问Service层,接收EUDataGirdResult,并以Json格式返回给前端页面
package com.taotao.controller;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.taotao.pojo.EUDataGridResult;
import com.taotao.service.ItemService;
@Controller
@RequestMapping("/item")
public class ItemContoller {
@Resource
private ItemService itemService;
/**
* 返回值:EUDataGridResult,封装数据
*调用Service查询
*/
@RequestMapping("/list")
@ResponseBody
public EUDataGridResult selectItem(Integer page,Integer rows){
return itemService.selectItem(page,rows);
}
}
3.9、展示结果
要想访问到页面,首先需要去controller写个接口去访问
package com.taotao.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class PageController {
//展示首页
@RequestMapping("/")
public String topage(){
return "index";
}
//展示其他页面
@RequestMapping("/{page}")
public String showpage(@PathVariable String page){
return page;
}
}
四、项目开发小技巧
4.1、前端页面
URL:
参数:
返回值:该创建pojo就创建pojo
4.2、Controller
接收参数:根据前端页面确定
返回值:根据前端页面确定
我们可能写接口提供出去给别的程序调用(如:上边那个controller写的就是展示页面),这个时候返回值和requestmapping就是自己决定了。你爱调不调
4.3、Service
调用Mapper,接收controller传递的参数,返回数据即可,这要写一些业务。
4.4、Mapper
单表操作,不用写,直接用****生成的代码。如果是多表操作,创建视图即可
4.5、关于报错
1)看报错信息,分析是前端报错还是后端报错
2)尝试自己思考报错原因,分析可能出现错误的地方,可能是代码,可能是配置
3)上网搜索