springmvc+spring+mybatis分页查询实例版本3,添加条件检索

在第二个版本上添加了姓名模糊查询,年龄区间查询;自以为easy,结果发现mybatis的各种参数写法基本搞混或是忘了,zuo啊,直接上代码,然后赶紧把mybatis整理一遍再研究自己的项目,应该还会有个版本4,基本就可以搬到实际项目上使用了,话说自己的项目要抓紧了,最近做的有点慢,也是没办法,工作偏忙,空闲时想看的东西太多,还是要科学计划一下,

参考资料 
1 ibatis2.x与mybatis(ibatis3.x)的比较 
http://zhaohe162.blog.163.com/blog/static/382167972011111114742371/ 
2 MyBatis学习 之 三、动态SQL语句 
http://limingnihao.iteye.com/blog/782190 
 

package com.mi.controller;

import javax.servlet.http.HttpServletRequest;

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 com.mi.form.PageTableForm;
import com.mi.service.impl.UserInfoServiceImpl;

@Controller
@RequestMapping("/user")
public class UserInfoCotroller {

    @Autowired
    private UserInfoServiceImpl userInfoServiceImpl;

    private PageTableForm pageTableForm;

    @RequestMapping("/init")
    public String init(Model model) {
        pageTableForm = new PageTableForm();
        return "redirect:/user/query.do";
    }

    @RequestMapping("/query")
    public String queryUserInfo(Model model,HttpServletRequest request, PageTableForm pageTableForm) {
        String name = request.getParameter("name")!=null ? request.getParameter("name") : null;
        String ageFrom =  (request.getParameter("ageFrom")!=null ? request.getParameter("ageFrom") : null);
        String ageTo =  (request.getParameter("ageTo")!=null ? request.getParameter("ageTo") : null);
        if(name!=null){
            pageTableForm.setName(name);
        }
        pageTableForm.setAgeFrom(ageFrom);
        pageTableForm.setAgeTo(ageTo);
        pageTableForm = userInfoServiceImpl.queryUserInfo(pageTableForm);
        model.addAttribute("pageTableForm", pageTableForm);
        return "userInfo";
    }

    public UserInfoServiceImpl getUserInfoServiceImpl() {
        return userInfoServiceImpl;
    }

    public void setUserInfoServiceImpl(UserInfoServiceImpl userInfoServiceImpl) {
        this.userInfoServiceImpl = userInfoServiceImpl;
    }

    public PageTableForm getPageTableForm() {
        return pageTableForm;
    }

    public void setPageTableForm(PageTableForm pageTableForm) {
        this.pageTableForm = pageTableForm;
    }

}
package com.mi.service.impl;

import java.util.List;

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

import com.mi.dao.UserInfoMapper;
import com.mi.entity.User;
import com.mi.form.PageTableForm;
import com.mi.service.UserInfoService;

@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;
    
    private List<User> userList;
    @Override
    public PageTableForm queryUserInfo(PageTableForm pageTableForm) {
        userList = userInfoMapper.queryUserInfo(pageTableForm);
        pageTableForm.setUserList(userList);
        return pageTableForm;
    }
    

    public UserInfoMapper getUserInfoMapper() {
        return userInfoMapper;
    }
    public void setUserInfoMapper(UserInfoMapper userInfoMapper) {
        this.userInfoMapper = userInfoMapper;
    }

    @Override
    public int getCount() {
        return userInfoMapper.getCount();
    }

    public List<User> getUserList() {
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

}
package com.mi.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import com.mi.entity.User;
import com.mi.form.PageTableForm;

@Repository("userInfoMapper")
public interface UserInfoMapper {
    
    public List<User> queryUserInfo(@Param("pageTableForm") PageTableForm pageTableForm);
    
    public int getCount();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mi.dao.UserInfoMapper">
    
    <select id="queryUserInfo" parameterType="com.mi.form.PageTableForm" resultType="com.mi.entity.User">
        SELECT id,user_name userName,age FROM user_t where 1=1
        <if test="pageTableForm.name !=null and pageTableForm.name !=''">
            and user_name like concat('%',#{pageTableForm.name},'%')
        </if>
        <if test="pageTableForm.ageFrom !=null and pageTableForm.ageFrom != ''">
            and age >= #{pageTableForm.ageFrom}
        </if>
        <if test="pageTableForm.ageTo !=null and pageTableForm.ageTo != ''">
            and age  &lt;= #{pageTableForm.ageTo}
        </if>
    </select>
    
    <select id="getCount" resultType="int">
        SELECT count(*) FROM user_t
    </select>
    <!-- <insert id="addUser" parameterType="com.mi.entity.User"
        flushCache="true">
        INSERT INTO user_t (id,user_name,password,age) VALUES
        (#{id},#{userName},#{password},#{age})
    </insert>
    
    <delete id="deleteUser" parameterType="com.mi.entity.User" flushCache="true">
        DELETE FROM user_t where id=#{id}
    </delete>
    
    <update id="updateUser" parameterType="com.mi.entity.User" flushCache="true">
        UPDATE user_t SET user_name = 'zzxy' WHERE id=#{id}
    </update> -->
    
</mapper>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
.page{
    width:200px;
}
.page span{
    margin-left:30px;
}
a{
    text-decoration:none;
}
</style>
<script type="text/javascript"
    src="<%=request.getContextPath()%>/js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
    $(function() {
        var $table = $(".user_table");
        var currentPage = 1;
        var pageSize = 3;
        var sumRows = $table.find("tbody tr").length;
        var sumPages = Math.ceil(sumRows/pageSize);
        
        init();
        paging(currentPage)
        
        $("#prev").click(function(){
            currentPage--;
            init();
            paging(currentPage);
        })
        
        $("#next").click(function(){
            currentPage++;
            init();
            paging(currentPage);
        })
        
        var $page = $("<div class='page'></div>");
        for(var pageIndex=1;pageIndex<=sumPages;pageIndex++){
            $("<a href='#'><span>["+(pageIndex)+"]</span></a>").bind("click",{"newPage":pageIndex},function(event){
                currentPage=event.data["newPage"];
                init();
                paging(currentPage);
            }).appendTo($page);
        }
        $page.insertAfter($table);
        
        function paging(currentPage){
            $table.find("tbody tr:not(.prevnext)").hide().slice((currentPage-1)*pageSize,(currentPage)*pageSize).show();
            $("#currentPage").val(currentPage+1);
            $("#currentPage").text(currentPage);
            $("#sumPages").text(sumPages);
        }
        
        function init(){
            if(currentPage==1){
                $("#prev").attr({"disabled":"disabled"});
            }else{
                $("#prev").removeAttr("disabled");
            }
            if(currentPage==sumPages){
                $("#next").attr({"disabled":"disabled"});
            }else{
                $("#next").removeAttr("disabled");
            }
        }
        $("#search").click(function(){
            var name = $("#name").val();
            var ageFrom = $("#ageFrom").val();
            var ageTo = $("#ageTo").val();
            if(name==""&&ageFrom==""&&ageTo==""){
                $("#msg").text("未输入检索条件!");
                return;
            }
            $("#form0").submit();
        });
        
    })
</script>
</head>
<body>
    <form action="${pageContext.request.contextPath}/user/query.do" id="form0" method="POST">
        <table>
            <tr><td>name:</td><td><input name="name" id="name" type="text"></td></tr>
            <tr><td>ageFrom:</td><td><input name="ageFrom" id="ageFrom" type="text" style="width: 40px"></td></tr>
            <tr><td>ageTo:</td><td><input name="ageTo" id="ageTo" type="text" style="width: 40px">&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type="button" value="search" id="search"></td></tr>
        </table>
        <span id="msg" style="color:red;"></span>
        <table class="user_table" border="1">
            <thead>
                <tr>
                    <td width="60px">id</td>
                    <td width="120px">name</td>
                    <td width="60px">age</td>
                </tr>
            </thead>
            <tbody>
                <c:forEach var="user" items="${pageTableForm.userList}" varStatus="status">
                    <tr>
                        <td>${user.id}</td>
                        <td>${user.userName}</td>
                        <td>${user.age}</td>
                    </tr>
                </c:forEach>
                <tr class="prevnext">
                    <td>
                        <input id="prev" type="button" value="上一页">
                    </td>
                    <td>当前<label id="currentPage"></label>页/共<label id="sumPages"></label></td>
                    <td>
                        <input id="next" type="button" value="下一页">
                    </td>
                </tr>
                
            </tbody>
        </table>
        <input id="currentPage" type="hidden" name="currentPage" value="${pageTableForm.currentPage}">
    </form>
</body>
</html>
package com.mi.form;

import java.util.List;

import com.mi.entity.User;

public class PageTableForm {

    private int currentPage;// 当前页
    private int pageSize = 3;// 每页记录数
    private int beginIndex;// 开始位置
    private int endIndex;// 结束位置
    private int pageCount;// 共多少页
    private int userCount;// 共多少条记录
    private String name;
    private String ageFrom;
    private String ageTo;
    
    private List<User> userList;
    ...省略get set
}

效果如下:

还原再来

其实应该再加个清空条件后重新检索全部的功能,不过想想算了,没必要,就一个显示全部,复制一个search去掉js的验证就可以了,实际用起来还要再细致化去使用,先这样

 又看了这个http://liuzidong.iteye.com/blog/1055491神的代码,嗯感觉可以哒,时间有限,等到实际项目再采用练手吧

posted @ 2016-09-12 15:44  公众号java-codestack  阅读(4709)  评论(0编辑  收藏  举报