Maven下SSM項目整合筆記05:查詢並分頁展示

程序跳轉描述: 訪問index.jsp界面,頁面發送查詢員工列表請求,完成查詢並顯示到listemp.jsp界面。

  • 編寫index界面,直接跳轉到前端控制器:
<jsp:forward page="/emps"></jsp:forward>
  • 編寫控制器對應的代碼:
package com.zr.crud.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zr.crud.bean.Employee;
import com.zr.crud.service.EmployeeService;

/**
 * 處理員工請求
 * @author asus
 *
 */
@Controller
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;
    /**
     * 查詢員工的數據(分頁查詢)
     * @return
     */
    @RequestMapping("/emps")
    public String getEmps(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model) {
        System.out.println("開始分頁查詢");
        //引入PageHelper分頁查詢插件,參數爲頁碼與每頁數量
        PageHelper.startPage(pn, 3);
        //startPage後面緊跟的查詢就是一個分頁查詢
        List< Employee> emps = employeeService.getAll();
        //使用pageInfo包裝查詢出來的數據,第二個參數爲連續顯示的頁數
        PageInfo<Employee> page = new PageInfo<Employee>(emps,2);
        //將pageInfo交給界面
        model.addAttribute("pageInfo",page);

        System.out.println("獲取數據");
        return "listemp";
    }
}

返回的頁面對應listemp.jsp,這裏之前在springmvc的配置文件dispatcherServlet-servlet.xml中寫了對應的前綴和後綴:

<!-- 配置視圖解析器,方便頁面返回 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>

這裏使用到了PageHelper的插件,在pom.xml中導入對應的依賴:

<!-- 導入PageHelper分頁插件 -->
<dependency>
    <groupId>com.github.pagehelper</groupId                                 
    <artifactId>pagehelper</artifactId>
    <version>5.0.0</version>
</dependency>

導入完成後,加入了pagehelper和jsqlparser兩個包。

根據PageHelper的說明文檔,這裏需要在mybatis的配置文件mybatis-config.xml中添加插件信息:

<plugins>
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
    </plugin>
</plugins>
  • 編寫對應服務層,使用dao層的Mapper類操作數據庫:
package com.zr.crud.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zr.crud.bean.Employee;
import com.zr.crud.dao.EmployeeMapper;

@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    /**
     * 查詢所有的員工
     * @return
     */
    public List<Employee> getAll() {
        return employeeMapper.selectByExample(null);
    }

}
  • 通過Spring測試對應的數據是否能夠取到:
package com.zr.crud.test;

import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import com.github.pagehelper.PageInfo;
import com.zr.crud.bean.Employee;

/**
 * 使用spring測試模塊測試請求功能
 * Spring4測試的時候,需要servlet3.0支持
 * @author asus
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:applicationContext.xml",
        "file:src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml" })
public class MvcTest {
    // 傳入springMvc的Ioc
    // 這裏需要@WebAppConfiguration註解拿到ioc
    @Autowired
    WebApplicationContext context;
    // 虛擬的mvc請求
    MockMvc mocMvc;

    @Before
    public void initMockMvc() {
        mocMvc = MockMvcBuilders.webAppContextSetup(context).build();
    }

    @Test
    public void testPage() throws Exception {
        // 模擬請求,拿到返回值
        MvcResult result = mocMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "1")).andReturn();

        // 請求成功以後,請求域中會有pageInfo
        // 取出pageInfo來驗證結果
        MockHttpServletRequest request = result.getRequest();
        PageInfo pi = (PageInfo) request.getAttribute("pageInfo");
        System.out.println("當前頁碼:" + pi.getPageNum());
        System.out.println("總頁碼:" + pi.getPages());
        System.out.println("總記錄數" + pi.getTotal());
        System.out.println("在頁面需要連續顯示的頁碼:");
        int[] nums = pi.getNavigatepageNums();
        for (int i : nums) {
            System.out.println("" + i);
        }

        // 獲取員工的數據
        List<Employee> list = pi.getList();
        for (Employee employee : list) {
            System.out.println("ID:" + employee.getEmpId() + "==Name:" + employee.getEmpName());
        }
    }
}

這裏使用Junit測試發現出現ClassNotFound的錯誤,發現是servlet的錯誤,這個時候更改servlet版本,pom中替換爲:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <!-- 這個包服務器裏面已經有了,會報錯 -->
    <scope>provided</scope>
</dependency>

重新測試,能夠在控制檯看到輸出數據,測試完成。

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