5.淘淘商城(非dubbo,IDEA)

商品列表查詢

1.在SqlMapConfig.xml中配置插件。

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

2.現在建一個測試類試一下分頁數據,在Java文件夾上右擊選擇Mark Directory As 在擴展的框中選擇Test Sources Root。


3.TestPageHelper類,內容如下:

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.mapper.TbItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemExample;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * Created by Administrator on 2018/5/2.
 */
public class TestPageHelper {

    @Test
    public void testPageHelper() {
        //創建一個spring容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
        //spring容器中獲得Mapper的代理對象
        TbItemMapper mapper = applicationContext.getBean(TbItemMapper.class);
        //執行查詢,並分頁
        TbItemExample example = new TbItemExample();
        //分頁處理
        PageHelper.startPage(2, 10);
        List<TbItem> list = mapper.selectByExample(example);
        //取商品列表
        for (TbItem tbItem : list) {
            System.out.println(tbItem.getTitle());
        }
        //取分頁信息
        PageInfo<TbItem> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        System.out.println("共有商品:"+ total);
    }
}

4.在taotao-common中創建EUDateGridResult

內容如下:

package com.taotao.pojo;

import java.util.List;

/**
 * Created by Administrator on 2018/5/2.
 */
public class EUDataGridResult {

    private long total;
    private List<?> rows;

    public long getTotal() {
        return total;
    }

    public List<?> getRows() {
        return rows;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public void setRows(List<?> rows) {
        this.rows = rows;
    }
} 

5.在接口中添加

EUDataGridResult getItemList(Integer page, Integer rows);

6.在實現類中實現

@Override
public EUDataGridResult getItemList(Integer page, Integer rows) {
    //查詢商品列表
    TbItemExample example = new TbItemExample();
    //分頁處理
    PageHelper.startPage(page, rows);
    List<TbItem> list = itemMapper.selectByExample(example);
    //創建一個返回值對象
    EUDataGridResult result = new EUDataGridResult();
    result.setRows(list);
    //取記錄總條數
    PageInfo<TbItem> pageInfo = new PageInfo<>(list);
    result.setTotal(pageInfo.getTotal());
    return result;
}

7.Controller

@RequestMapping("/item/list")
@ResponseBody
public EUDataGridResult getItemList(Integer page, Integer rows) {
    EUDataGridResult result = itemService.getItemList(page, rows);
    return result;
}

8.結果測試



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章