SSM框架整合(curd+分頁+導出爲excel)

####前言:

SpringMVC,Mybatis,Spring三大框架的整合,該系統基於Maven做依賴管理。使用MySQL數據庫實現了MIS系統中常用的功能。

項目源代碼GitHub地址:https://github.com/hlk-1135/SSM_StudentInfo

(如果喜歡,請給我點個Star,歡迎一起交流,共同進步!)


####使用的技術:

  • SpringMVC,Mybatis,Spring三大框架的整合;
  • 前端框架集成了Bootstrap,Jquery,集成了Bootstrap插件Bootgrid數據表格實現分頁(分頁案例教程),後臺的分頁使用Mybatis的插件pagehelper實現
  • 實現了學生的增刪改查
  • 包含了數據表導出爲Excel下載的功能,包含了解析Excel內容的API,使用POI實現

**後期將會實現:**註冊時發送郵箱進行驗證;登錄是記住密碼以及驗證碼校驗;權限管理


####項目整體結構:
這裏寫圖片描述


####功能展示:
1、用戶登錄:

@RequestMapping(value = "/loginValidate",method = RequestMethod.POST)
    public String loginValidate(@RequestParam("username") String username,@RequestParam("password") String password,HttpSession httpSession) {
        if(username==null || password==null)
            return "user/login";
        else {
            User user = userService.getUserByUserName(username);
            if(user.getPassword().equals(password)) {
                httpSession.setAttribute("username", username);
                return "student/stuList";
            } else  {
                return "user/login";
            }
        }
    }
<select id="getUserByUserName" resultType="com.hlk.pojo.User" parameterType="string">
  <![CDATA[
    SELECT * FROM USER WHERE user_name=#{user_name}
  ]]>
</select>

這裏寫圖片描述

2、用戶退出:

點擊logout後,返回到登錄頁面:

@RequestMapping(value = "/logout")
    public String logout(HttpSession httpSession) {
        httpSession.removeAttribute("username");
        return "redirect:/user/login";
    }

這裏寫圖片描述

3、系統主頁面:

這裏寫圖片描述

4、添加學生:

這裏寫圖片描述

5、修改信息:

這裏寫圖片描述

Id不可更改,這裏有兩種常用的方式:

  • disabled="true" 禁用 input 元素,不可編輯,不可複製,不可選擇,不能接收焦點,後臺也不會接收到傳值。設置後文字的顏色會變成灰色。
  • readonly="true" 可選擇,可以接收焦點,還可以選中或拷貝其文本。後臺會接收到傳值. readonly 屬性可以防止用戶對值進行修改。

5、分頁功能:

前臺頁面獲取當前頁數以及每頁顯示的數量:

var grid = $("#grid-data").bootgrid({
            ajax:true,
            post: function ()
            {
                return {
                    id: "b0df282a-0d67-40e5-8558-c9e93b7befed"
                };
            },
            url:"/stu/stuList",
            formatters: {
                "commands": function(column, row)
                {
                    return "<button type=\"button\" class=\"btn btn-xs btn-default command-edit\" data-row-id=\"" + row.stuId + "\">編輯<span class=\"fa fa-pencil\"></span></button> " +
                        "<button type=\"button\" class=\"btn btn-xs btn-default command-delete\" data-row-id=\"" + row.stuId + "\">刪除<span class=\"fa fa-trash-o\"></span></button>";
                }
            }
        }).on("loaded.rs.jquery.bootgrid", function()
		{
            grid.find(".command-edit").on("click", function(e)
            {
                alert("You pressed edit on row: " + $(this).data("row-id"));
                $(".stumodal").modal();
                $.post("stu/getStuInfo",{id:$(this).data("row-id")},function(str){
                    alert("You pressed edit on row: " + $(this).data("row-id"));
                    var data=JSON.parse(str);
                    alert(data);
                    $("#sName").val(str.stuName);
                    $("#sAge").val(str.stuAge);
                    $("#sMajor").val(str.stuMajor);
                });
            }).end().find(".command-delete").on("click", function(e)
            {
                alert("You pressed delete on row: " + $(this).data("row-id"));
                $.post("/stu/delStu",{id:$(this).data("row-id")},function(){
                    alert("刪除成功");
                    $("#grid-data").bootgrid("reload");
                });
            });
        });
public class StuGrid {
    private int current;//當前頁面號
    private int rowCount;//每頁行數
    private int total;//總行數
    private List<Stu> rows;
    **省略getter/setter**
}

使用Mybatis插件pagehelper:

public List<Stu> getPageStu(int pagenum, int pagesize) {
     PageHelper.startPage(pagenum,pagesize);//分頁核心代碼
     List<Stu> list = stuMapper.getStuList();
     return list;
 }

Contorller控制類:

 @RequestMapping(value = "/stuList",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public StuGrid getStuList(@RequestParam("current") int current,@RequestParam("rowCount") int rowCount) {
        int total = stuService.getStuNum();
        List<Stu>  list = stuService.getPageStu(current,rowCount);
        StuGrid stuGrid = new StuGrid();
        stuGrid.setCurrent(current);
        stuGrid.setRowCount(rowCount);
        stuGrid.setRows(list);
        stuGrid.setTotal(total);
        return stuGrid;
    }

這裏寫圖片描述

6、將數據導出生成Excel:

從數據庫中獲取並使用poi寫入Excel中:

 public InputStream getInputStream() throws Exception {
        String[] title=new String[]{"stuId","stuName","stuAge","stuMajor"};
        List<Stu> plist=stuMapper.getStuList();
        List<Object[]>  dataList = new ArrayList<Object[]>();
        for(int i=0;i<plist.size();i++){
            Object[] obj=new Object[4];
            obj[0]=plist.get(i).getStuId();
            obj[1]=plist.get(i).getStuName();
            obj[2]=plist.get(i).getStuAge();
            obj[3]=plist.get(i).getStuMajor();
            dataList.add(obj);
        }
        WriteExcel ex = new WriteExcel(title, dataList);
        InputStream in;
        in = ex.export();
        return in;
    }
@RequestMapping("/exportStu")
    public void export(HttpServletResponse response) throws Exception{
        InputStream is=stuService.getInputStream();
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("contentDisposition", "attachment;filename=AllUsers.xls");
        ServletOutputStream output = response.getOutputStream();
        IOUtils.copy(is,output);
    }

這裏寫圖片描述

生成的Excel表格:

這裏寫圖片描述

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