专业实习课day05

    技术2022-07-11  87

    ssm实现登录后进行增删改查

    User类

    package com.wsw.bean; import com.mchange.v2.c3p0.ComboPooledDataSource; public class User { public User() { } public User(int id, String username, String password) { this.id = id; this.username = username; this.password = password; } public User(String username, String password) { this.username = username; this.password = password; } private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } }

    UserController类

    package com.wsw.controller; import com.wsw.bean.User; import com.wsw.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import java.util.List; @Controller @RequestMapping("/user") public class UserController { @Autowired private IUserService userService; @RequestMapping("/login.do") public ModelAndView login(User user){ boolean flag= userService.login(user.getUsername(),user.getPassword()); ModelAndView modelAndView = new ModelAndView(); if (flag){ modelAndView.setViewName("main"); }else { modelAndView.setViewName("../failer"); } return modelAndView; } @RequestMapping("/findAll.do") public ModelAndView findAll(){ List<User> userList=userService.findAll(); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("userList",userList); modelAndView.setViewName("user-list"); return modelAndView; } @RequestMapping("/deletByid.do") public String delet(int id){ userService.deletByid(id); return "redirect:findAll.do"; } @RequestMapping("/add.do") public String add(User user){ userService.add(user); return "redirect:findAll.do"; } @RequestMapping("toUpdate.do") public ModelAndView toUpdate(int id){ User user=userService.selectUserById(id); ModelAndView modelAndView=new ModelAndView(); modelAndView.setViewName("user-update"); modelAndView.addObject("user",user); return modelAndView; } @RequestMapping("/updateA.do") public String updateA(User user){ userService.updateA(user); return "redirect:findAll.do"; } }

    UserDao接口

    package com.wsw.dao; import com.wsw.bean.User; import java.util.List; public interface UserDao { User findUserByUserName(String username); List<User> findAll(); void deletByid(int id); void add(User user); User selectById(int id); void updateAll(User user); }

    UserService类

    package com.wsw.service.impl; import com.wsw.bean.User; import com.wsw.dao.UserDao; import com.wsw.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService implements IUserService { @Autowired private UserDao userDao; @Override public boolean login(String username, String password) { User user =userDao.findUserByUserName(username); if (user!=null && user.getPassword().equals(password)){ return true; } return false; } @Override public List<User> findAll(){ return userDao.findAll(); } @Override public void deletByid(int id){ userDao.deletByid(id); } @Override public void add(User user){ userDao.add(user); } @Override public User selectUserById(int id) { return userDao.selectById(id); } @Override public void updateA(User user) { userDao.updateAll(user); } }

    IUserService接口

    package com.wsw.service; import com.wsw.bean.User; import java.util.List; public interface IUserService { boolean login(String username,String password); List<User> findAll(); void deletByid(int id); void add(User user); User selectUserById(int id); void updateA(User user); }

    .jsp关键代码

    login.jsp index.jsp update.jsp user-list.jsp user-add.jsp

    Processed: 0.011, SQL: 9