使用idea搭建ssm框架,并实现简单的CRUD和分页查询功能

1.使用idea搭建ssm框架步骤,请参考本人博客

2.项目搭建好,创建以下目录

使用idea搭建ssm框架,并实现简单的CRUD和分页查询功能_第1张图片

3.在pom.xml文件中引入依赖




  4.0.0

  com.kude.ssm
  SsmTest
  1.0-SNAPSHOT
  war

  SsmTest Maven Webapp
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
  

  
    
      junit
      junit
      4.11
      test
    
    
    
      org.springframework
      spring-webmvc
      5.1.5.RELEASE
    
    
    
      org.springframework
      spring-webmvc
      4.3.14.RELEASE
    

    
    
      org.springframework
      spring-jdbc
      4.3.14.RELEASE
    

    
    
      aopalliance
      aopalliance
      1.0
    

    
      org.aspectj
      aspectjweaver
      1.8.10
    

    
    
      org.mybatis
      mybatis
      3.4.1
    

    
    
      tk.mybatis
      mapper
      3.3.9
    

    
    
      org.mybatis
      mybatis-spring
      1.3.1
    

    
    
      mysql
      mysql-connector-java
      5.1.38
    

    
      com.alibaba
      druid
      1.1.10
    

    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.9.5
    

    
    
      javax.servlet
      servlet-api
      2.5
      provided
    

    
      javax.servlet.jsp
      jsp-api
      2.2
      provided
    
    
    
      commons-io
      commons-io
      2.4
    
    
    
      org.apache.commons
      commons-dbcp2
      2.1.1
    
    
      org.junit.jupiter
      junit-jupiter-api
      RELEASE
      compile
    

    
    
      taglibs
      standard
      1.1.2
    
    
      jstl
      jstl
      1.1.2
    
    
    
      log4j
      log4j
      1.2.17
    

  

  
    SsmTest
    
      
        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-war-plugin
          3.2.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
    
  


4.在Resources目录下引入spring配置文件applicationContext.xml,springMVC配置文件springMVC.xml,以及mapper文件mapper.xml

使用idea搭建ssm框架,并实现简单的CRUD和分页查询功能_第2张图片

5.创建分页类Page 实体类跟数据库中的表字段与数据库中的一一对应

package com.kude.entity;

public class Employee {
    private Integer id;
    private String name;
    private String password;

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getPassword() {
        return password;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Employee(Integer id, String name, String password) {
        this.id = id;
        this.name = name;
        this.password = password;
    }
    public Employee(){

    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

package com.kude.entity;

import java.util.List;

public class Page {
    private int currentPage;//当前页
    private int totalPage;//总页数
    private int pageSize;//每页记录数
    private int totalCount;//总的记录数
    private List list;

    public Page(){

    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public Page(int currentPage, int totalPage, int pageSize, int totalCount, List list) {
        this.currentPage = currentPage;
        this.totalPage = totalPage;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        this.list = list;
    }

    @Override
    public String toString() {
        return "Page{" +
                "currentPage=" + currentPage +
                ", totalPage=" + totalPage +
                ", pageSize=" + pageSize +
                ", totalCount=" + totalCount +
                ", list=" + list +
                '}';
    }
}

6.Dao层开发

package com.kude.dao;

import com.kude.entity.Employee;
import org.apache.ibatis.annotations.Select;

import java.util.HashMap;
import java.util.List;

public interface EmployeeDao {
    // 插入
    public void insert(Employee employee);
    //删除
    public void delete(Integer id);
    //显示所有
    public List queryAll();
    // 查询单条数据
    public Employee query(String name);
    // 修改
    public void update(Employee employee);
    // 查询单个
    public Employee queryone(Integer id);
    @Select("")
    List findByPage(HashMap map);

    @Select("select count(*) from employee")
        //查询总记录数
    int findAllCount();

}

7.mapper.xml




    
	
		insert into employee values (#{id},#{name},#{password})
	
    
        delete from employee where id=#{id}
    
    
    
    
        update employee set
		name=#{name},password=#{password}
		where id=#{id}
    



8.Service层开发

package com.kude.service;

import com.kude.entity.Employee;
import com.kude.entity.Page;
import java.util.List;

public interface EmployeeService {
    //显示所有
     List showall();
    // 注册
     void register (Employee employee);
    //删除
     void delete(Integer id);
    //登录
     Employee login(String name);
    // 修改
     void update(Employee employee);

     Employee query(Integer id);
    //查询总记录数
    int findAllCount();

    Page findPage(int currentPage);

}

package com.kude.service;

import com.kude.dao.EmployeeDao;
import com.kude.entity.Employee;
import com.kude.entity.Page;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;



@Service
@Transactional
public class EmployeeServiceimpl implements EmployeeService {
    @Autowired
    private EmployeeDao employeeDao;

    public EmployeeDao getEmployeeDao() {
        return employeeDao;
    }

    public void setEmployeeDao(EmployeeDao employeeDao) {
        this.employeeDao = employeeDao;
    }

    /***
     * 显示所有人员信息
     * @return
     */
    @Override
    public List showall() {
        List list=employeeDao.queryAll();
        return list;
    }

    @Override
    public void register(Employee employee) {
        employeeDao.insert(employee);
    }

    @Override
    public void delete(Integer id) {
        employeeDao.delete(id);
    }

    @Override
    public Employee login(String name) {
        Employee employee=employeeDao.query(name);
        return employee;
    }

    @Override
    public void update(Employee employee) {
       employeeDao.update(employee);

    }

    @Override
    public Employee query(Integer id) {
       Employee employee= employeeDao.queryone(id);
       return employee;
    }

    @Override
    public int findAllCount() {
        return employeeDao.findAllCount();
    }

    @Override
    public Page findPage(int currentPage) {
        HashMap map=new HashMap<>();
        Page page=new Page<>();
        //封装当前的页数
        page.setCurrentPage(currentPage);
        //每页显示的数据
        int pageSize=5;
        page.setPageSize(pageSize);
        //封装总记录数
        int totalCount=employeeDao.findAllCount();
        page.setTotalCount(totalCount);
        //封装总页数
        double totalPage=totalCount;
        //向上取整
        Double number=Math.ceil(totalPage/pageSize);
        page.setTotalPage(number.intValue());

        map.put("start",(currentPage-1)*pageSize);
        map.put("size",page.getPageSize());
        //封装每页显示的数据
        List list=employeeDao.findByPage(map);
        page.setList(list);
        return page;

    }
}

9.controller开发

package com.kude.action;

import com.kude.entity.Employee;
import com.kude.service.EmployeeService;
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 javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;

    public EmployeeService getEmployeeService() {
        return employeeService;
    }

    public void setEmployeeService(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    @RequestMapping("/register")
    public String register(Employee employee, HttpServletRequest request) {
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        System.out.println(name);
        System.out.println(password);
        if (name == null && password == null) {
            return "register";
        } else {
            employeeService.register(employee);
            return "login";
        }
    }

    @RequestMapping("/login")
    public String login(Employee employee, HttpServletRequest request) {
        Employee employee1 = employeeService.login(employee.getName());
        request.getSession().setAttribute("employee1", employee1);
        if (employee != null) {
            if (employee1.getPassword().equals(employee.getPassword())) {
                return "forward:/employee/showAll.do";
            } else {
                return "login";
            }
        } else {
            return "login";
        }
    }
    @RequestMapping("/showAll")
    public String showAll(@RequestParam(value = "currentPage",defaultValue = "1",required = false)int currentPage, Model model){
        model.addAttribute("pagemsg",employeeService.findPage(currentPage));

        return "showAllList";
    }
    @RequestMapping("/delete")
    public String delete(Integer id){
        employeeService.delete(id);
        return "forward:/employee/showAll.do";
    }
    @RequestMapping("/select")
    public String select(HttpServletRequest request){
       String id= request.getParameter("id");
       int id2=Integer.parseInt(id);
      Employee employee= employeeService.query(id2);
      request.setAttribute("employee",employee);
      return "updateList";
    }
    @RequestMapping("/update")
    public String update(Employee employee){
        employeeService.update(employee);
        return "forward:/employee/showAll.do";
    }
}

10.applicationContext.xml



    
    
    
    
        
        
        
        

    
    
    
        
        
            com.kude.entity
        
        
        
            
        
        
            
                
                classpath:mapper.xml
            
        
    
    
    
        

        
            com.kude.dao
        
    
    
    
        
            
        
    
    
    




11.springMVC.xml




    
    
    
    
    
    
    
        
        
    



12.简单的前端页面

regiser.jsp

<%--
  Created by IntelliJ IDEA.
  User: 石光辉
  Date: 2019/5/26
  Time: 10:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    注册页面


    
用户名:

密码:

login.jsp

<%--
  Created by IntelliJ IDEA.
  User: 石光辉
  Date: 2019/5/26
  Time: 10:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    登录页面


用户名:

密码:

showAllList.jsp

<%--
  Created by IntelliJ IDEA.
  User: 石光辉
  Date: 2019/5/26
  Time: 11:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


    展示所有

尊敬的用户:${sessionScope.employee1.name}

ID 用户名    密码
${employee.id} ${employee.name} ${employee.password} 删除   修改
第${requestScope.pagemsg.currentPage}/${requestScope.pagemsg.totalPage}页 总记录数:${requestScope.pagemsg.totalCount} 每页显示:${requestScope.pagemsg.pageSize} [首页] [上一页] [下一页] [尾页]

updateList.jsp

<%--
  Created by IntelliJ IDEA.
  User: 石光辉
  Date: 2019/5/26
  Time: 16:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    修改页面


id:
name:
password:

最终效果

使用idea搭建ssm框架,并实现简单的CRUD和分页查询功能_第3张图片
使用idea搭建ssm框架,并实现简单的CRUD和分页查询功能_第4张图片

使用idea搭建ssm框架,并实现简单的CRUD和分页查询功能_第5张图片

使用idea搭建ssm框架,并实现简单的CRUD和分页查询功能_第6张图片
总结:此案例是最简单的demo,有一些bug没修复,同学们可以自己修改bug,比如没有登录状态下不能通过url直接访问显示所有页面。

你可能感兴趣的:(SSM框架学习)