SSM (IDEA) —— 实现CURD

StudentController

package com.etc.controller;

import com.etc.entity.Student;
import com.etc.service.StudentService;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
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.HttpSession;
import java.util.List;

@Controller
public class StudentController {

    @Autowired
    private StudentService service;

    @RequestMapping("/tologin")
    public String tologin(){
        System.out.println("tologin");
        return "login";
    }

/*
    ModelAndView方法实现
    @RequestMapping("/login")
    public ModelAndView login(HttpSession session, ModelAndView modelAndView, Student student){
        System.out.println("login:"+student.getUsername());
        session.setAttribute("username",student.getUsername());
        if (service.login(student)){
            modelAndView.setViewName("list");
            return modelAndView;
        }else {
            modelAndView.addObject("msg","用户名或密码错误");
            modelAndView.setViewName("login");
            return modelAndView;
        }
    }*/

    @RequestMapping("/login")
    public String login(HttpSession session, Model model, Student student){
        System.out.println("login:"+student.getUsername());
        session.setAttribute("username",student.getUsername());
        if (service.login(student)){
            model.addAttribute("username",student.getUsername());
            return "index";
        }else {
            model.addAttribute("msg","用户名或密码错误");
            return "login";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        System.out.println("logout");
        session.invalidate();
        return "login";
    }

    @RequestMapping("/toregister")
    public String toregister(Model model){
        model.addAttribute("msg","注册");
        return "register";
    }

    @RequestMapping("/register")
    public String register(Model model,Student student){
        if(service.register(student)){
            model.addAttribute("msg","注册成功");
            return "login";
        }else{
            model.addAttribute("msg","注册失败");
            return "register";
        }
    }

    @RequestMapping("/queryall")
    public String queryall(Model model){
        List list = service.queryall();
        model.addAttribute("list",list);
        return "queryall";
    }

    @RequestMapping("/queryonebyid")
    public String queryonebyid(Model model,Integer id){
        System.out.println(id);
        Student student = service.queryonebyid(id);
        model.addAttribute("student",student);
        return "editor";
    }

    @RequestMapping("/update")
    public String update(Student student){
        service.update(student);
        return "redirect:queryall";
    }

    @RequestMapping("/deletebyid")
    public String deletebyid(Integer id){
        service.deletebyid(id);
        return "redirect:queryall";
    }

    @RequestMapping("/deletes")
    public String deletes(String[] ids){
        for (int i=0;i<ids.length;i++){
            String id = ids[i];
            System.out.println(id);
        }
        service.deletes(ids);
        return "redirect:queryall";
    }
}

StudentService

package com.etc.service;

import com.etc.dao.StudentDao;
import com.etc.entity.Student;
import com.etc.entity.StudentExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class StudentService {

    @Autowired
    private StudentDao studentDao;

    public boolean login(Student student){
        StudentExample example = new StudentExample();
        example.createCriteria().andUsernameEqualTo(student.getUsername()).
                andPasswordEqualTo(student.getPassword());
        List result = studentDao.selectByExample(example);
        return result.size()>0;
    }

    public boolean register(Student student){
        int result = studentDao.insertSelective(student);
        return result>0;
    }

    public List<Student> queryall(){
        return studentDao.selectByExample(null);
    }

    public Student queryonebyid(Integer id){
        return studentDao.selectByPrimaryKey(id);
    }

    public boolean update(Student student){
        StudentExample example = new StudentExample();
        example.createCriteria().andIdEqualTo(student.getId());
        int result = studentDao.updateByExampleSelective(student,example);
        return result>0;
    }

    public boolean deletebyid(Integer id){
        int result = studentDao.deleteByPrimaryKey(id);
        return result>0;
    }

    public boolean deletes(String[] ids){
        int start = new Integer(ids[0]);
        int end = new Integer(ids[ids.length-1]);
        StudentExample example = new StudentExample();
        example.createCriteria().andIdBetween(start,end);
        int result = studentDao.deleteByExample(example);
        return result>0;
    }
}

最外层index

<jsp:forward page="/WEB-INF/jsp/login.jsp"/>

index

  欢迎${username}
<form action="">
    <input type="submit" formaction="${pageContext.request.contextPath }/logout" value="注销" />
    <input type="submit" formaction="${pageContext.request.contextPath }/queryall" value="查看所有" />
</form>

login

    ${msg}
    <%-- 提交后的位置:/WEB-INF/jsp/login.jsp--%>
    <form action="${pageContext.request.contextPath }/login" method="post" >
        姓名:<input id="username" type="text" name="username" />
        <br />
        密码:<input id="password" type="password" name="password" />
        <br />
        <input type="submit" value="登录" />
        <input type="submit" formaction="${pageContext.request.contextPath }/toregister" value="注册" />
    </form>

queryall

<form method="post">
    <table>
        <tr>
            <th>姓名</th>
            <th>密码</th>
            <th>专业</th>
        </tr>
        <c:forEach var="c" items="${list}">
            <tr>
                <td>${c.username}</td>
                <td>${c.password}</td>
                <td>${c.major}</td>
                <td><a href="/queryonebyid?id=${c.id}">修改</a></td>
                <td><a href="/deletebyid?id=${c.id}">删除</a></td>
                <td><input type="checkbox" name="ids" value="${c.id}"></td>
            </tr>
        </c:forEach>
    </table>
    <input type="button" onclick="deletes()" value="批量删除">
</form>
<script type="text/javascript">

    function deletes(){
        if(confirm('确定删除')){
            var f = document.forms[0];
            f.action = 'deletes';
            f.submit();
        }
    }
</script>

register

<form action="/register">
    <table>
        <tr>
            <td>姓名</td>
            <td><input type="text" name="username">
            </td>
        </tr>
        <tr>
            <td>密码</td>
            <td><input type="Password" name="password">
            </td>
        </tr>
        <tr>
            <td>专业</td>
            <td><input type="text" name="major">
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center"><input type="submit" value="注册">
            </td>
        </tr>
    </table>
</form>

editor

<form action="/update">
    <table>
            <input type="hidden" name="id" value="${student.id}">
        <tr>
            <td>姓名</td>
            <td><input type="text" name="username" value="${student.username}">
            </td>
        </tr>
        <tr>
            <td>密码</td>
            <td><input type="Password" name="password" value="${student.password}">
            </td>
        </tr>
        <tr>
            <td>专业</td>
            <td><input type="text" name="major" value="${student.major}">
            </td>
        </tr>

        <tr>
            <td colspan="2" align="center"><input type="submit" value="修改">
            </td>
        </tr>
    </table>
</form>

猜你喜欢

转载自blog.csdn.net/Milan__Kundera/article/details/82629603
今日推荐