商品列表查询

1.展示后台首页

1.1功能分析

请求的url:/

参数:无

返回值:逻辑视图String

1.2Controller

@Controller

public class PageController {

 

      @RequestMapping("/")

      public String showIndex() {

            return "index";

      }

     

      @RequestMapping("/{page}")

      public String showPage(@PathVariable String page) {

            return page;

      }

}

2.功能分析

2.1整合静态页面

静态页面位置:02.第二天(三大框架整合,后台系统搭建)\01.参考资料\后台管理系统静态页面

使用方法:

把静态页面添加到e3-manager-web工程中的WEB-INF下:

由于在web.xml中定义的url拦截形式为“/”表示拦截所有的url请求,包括静态资源例如css、js等。所以需要在springmvc.xml中添加资源映射标签:

      <mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>

      <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>

2.2商品列表页面

对应的jsp

item-list.jsp

 

请求的url

/item/list

请求的参数:

page=1&rows=30

响应的json数据格式:

Easyui中datagrid控件要求的数据格式为:

{total:”2”,rows:[{“id”:”1”,”name”:”张三”},{“id”:”2”,”name”:”李四”}]}

2.3响应的json数据格式EasyUIResult

public class EasyUIDataGridResult {

 

    private Integer total;

 

    private List<?> rows;

 

    public EasyUIResult(Integer total, List<?> rows) {

        this.total = total;

        this.rows = rows;

    }

 

    public EasyUIResult(Long total, List<?> rows) {

        this.total = total.intValue();

        this.rows = rows;

    }

 

    public Integer getTotal() {

        return total;

    }

 

    public void setTotal(Integer total) {

        this.total = total;

    }

 

    public List<?> getRows() {

        return rows;

    }

 

    public void setRows(List<?> rows) {

        this.rows = rows;

    }

 

}

2.4分页处理

逆向工程生成的代码是不支持分页处理的,如果想进行分页需要自己编写mapper,这样就失去逆向工程的意义了。为了提高开发效率可以使用mybatis的分页插件PageHelper。

3.分页插件PageHelper

3.1Mybatis分页插件 - PageHelper说明

如果你也在用Mybatis,建议尝试该分页插件,这个一定是最方便使用的分页插件。

该插件目前支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库分页。

3.2使用方法

第一步:把PageHelper依赖的jar包添加到工程中。官方提供的代码对逆向工程支持的不好,使用参考资料中的pagehelper-fix。

第二步:在Mybatis配置xml中配置拦截器插件:

<plugins>

    <!-- com.github.pagehelperPageHelper类所在包名 -->

    <plugin interceptor="com.github.pagehelper.PageHelper">

        <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->       

        <property name="dialect" value="mysql"/>

    </plugin>

</plugins>

第二步:在代码中使用

1、设置分页信息:

//获取第1页,10条内容,默认查询总数count

    PageHelper.startPage(1, 10);

 

    //紧跟着的第一个select方法会被分页

List<Country> list = countryMapper.selectIf(1);

2、取分页信息

//分页后,实际返回的结果list类型是Page<E>,如果想取出分页信息,需要强制转换为Page<E>

Page<Country> listCountry = (Page<Country>)list;

listCountry.getTotal();

3、取分页信息的第二种方法

//获取第1页,10条内容,默认查询总数count

PageHelper.startPage(1, 10);

List<Country> list = countryMapper.selectAll();

//PageInfo对结果进行包装

PageInfo page = new PageInfo(list);

//测试PageInfo全部属性

//PageInfo包含了非常全面的分页属性

assertEquals(1, page.getPageNum());

assertEquals(10, page.getPageSize());

assertEquals(1, page.getStartRow());

assertEquals(10, page.getEndRow());

assertEquals(183, page.getTotal());

assertEquals(19, page.getPages());

assertEquals(1, page.getFirstPage());

assertEquals(8, page.getLastPage());

assertEquals(true, page.isFirstPage());

assertEquals(false, page.isLastPage());

assertEquals(false, page.isHasPreviousPage());

assertEquals(true, page.isHasNextPage());

3.3分页测试

@Test

      public void testPageHelper() throws Exception {

            //初始化spring容器

            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");

            //获得Mapper的代理对象

            TbItemMapper itemMapper = applicationContext.getBean(TbItemMapper.class);

            //设置分页信息

            PageHelper.startPage(1, 30);

            //执行查询

            TbItemExample example = new TbItemExample();

            List<TbItem> list = itemMapper.selectByExample(example);

            //取分页信息

            PageInfo<TbItem> pageInfo = new PageInfo<>(list);

            System.out.println(pageInfo.getTotal());

            System.out.println(pageInfo.getPages());

            System.out.println(pageInfo.getPageNum());

            System.out.println(pageInfo.getPageSize());

      }

3.4Service层

参数:int page ,int rows

业务逻辑:查询所有商品列表,要进行分页处理。

返回值:EasyUIDataGridResult

@Override

      public EasyUIDataGridResult getItemList(int page, int rows) {

           

            //设置分页信息

            PageHelper.startPage(page, rows);

            //执行查询

            TbItemExample example = new TbItemExample();

            List<TbItem> list = itemMapper.selectByExample(example);

            //取分页信息

            PageInfo<TbItem> pageInfo = new PageInfo<>(list);

           

            //创建返回结果对象

            EasyUIDataGridResult result = new EasyUIDataGridResult();

            result.setTotal(pageInfo.getTotal());

            result.setRows(list);

           

            return result;

      }

3.4.1发布服务

3.5表现层

引用服务:

1、初始化表格请求的url:/item/list

2、Datagrid默认请求参数:

  1. page:当前的页码,从1开始。
  2. rows:每页显示的记录数。
  3. 响应的数据:json数据。EasyUIDataGridResult

@RequestMapping("/item/list")

      @ResponseBody

      public EasyUIDataGridResult getItemList(Integer page, Integer rows) {

            EasyUIDataGridResult result = itemService.getItemList(page, rows);

            return result;

      }

 

 

 

可以设置服务超时时间:

服务调用超时时间默认1秒,

3.6安装maven工程跳过测试

clean install -DskipTests

 

猜你喜欢

转载自blog.csdn.net/etna_hh/article/details/82155870
今日推荐