SSM framework for integration (curd + page + export to excel)

#### Introduction:

SpringMVC, Mybatis, Spring Framework integration of the three, the system is based on Maven dependency management to do. Use MySQL database to achieve the MIS system commonly used functions.

Project source code GitHub address: https://github.com/hlk-1135/SSM_StudentInfo

(If you like, please give me a Star, welcomed the exchange and common progress!)


#### Use of Technology:

  • SpringMVC, Mybatis, Spring Framework integration of the three;
  • Integrated front-end framework Bootstrap, Jquery, integrates Bootstrap plugin Bootgrid data tables implement paging ( paging case tutorial ), background page using Mybatis plug pagehelper achieve
  • Students achieve CRUD
  • It contains the data tables are exported as Excel download features, including the analytical content of the Excel API, using POI achieve

The latter will be achieved **: ** Send email to verify registration; logon to remember password and a code check; rights management


#### overall project structure:
Write pictures described here


#### Function display:
1, user login:

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

Write pictures described here

2, the user exits:

After clicking logout, to return to the login page:

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

Write pictures described here

3, the system main page:

Write pictures described here

4, add students:

Write pictures described here

5, modify the information:

Write pictures described here

Id can not be changed, there are two common ways:

  • disabled="true"Input element is disabled, can not be edited, not copied, can not be selected, can not receive focus, the background does not pass the received value. After setting the text color turns gray.
  • readonly="true"Alternatively, the focus can receive, or copy can also select the text. Background receives the traditional values. Readonly attribute values ​​may be modified to prevent users.

5, paging functions:

Get the current front page and the number of pages displayed per page:

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**
}

Use Mybatis plug pagehelper:

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

Contorller control categories:

 @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;
    }

Write pictures described here

6, the data generated by the export Excel:

Acquired from the database and using poi write 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);
    }

Write pictures described here

Excel spreadsheet generated:

Write pictures described here

Published 128 original articles · won praise 239 · views 330 000 +

Guess you like

Origin blog.csdn.net/HLK_1135/article/details/70833251