jdbc servlet ssm 实现简单的增删改查

    技术2022-07-11  80

    jdbc servlet ssm 实现简单的增删改查

    UserMapper

    <mapper namespace="com.wu.dao.UserDao" > <select id="findUserByUserName" parameterType="String" resultType="user"> select * from tb_user where username=#{username} </select> <select id="findAll" parameterType="String" resultType="user"> select * from tb_user </select> <select id="deleteById" parameterType="int"> delete from tb_user where id =#{id} </select> <select id="addUser" parameterType="String" resultType="user"> insert into tb_user(username,password) value (#{username},#{password}) </select> <select id="selectById" parameterType="int" resultType="user"> select * from tb_user where id=#{id} </select> <select id="update" parameterType="user" resultType="user"> update tb_user set username=#{username},password=#{password} where id=#{id} </select> </mapper>

    IUserService

    public interface IUserService { boolean login(String username,String password); List<User> findAll(); void deleteById(int id); void addUser(User user); User selectUserById(int id); void update(User user); }

    UserDao

    public interface UserDao { User findUserByUserName(String username); List<User> findAll(); void deleteById(int id); void addUser(User user); User selectById(int id); void update(User user); }

    UserService

    @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 deleteById(int id) { userDao.deleteById(id); } @Override public void addUser(User user) { userDao.addUser(user); } @Override public User selectUserById(int id) { return userDao.selectById(id); } @Override public void update(User user) { userDao.update(user); } }

    UserController

    @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("/deleteById.do") public String delete(int id){ userService.deleteById(id); return "redirect:findAll.do"; } @RequestMapping("/addUser.do") public String add(User user){ userService.addUser(user); return "redirect:findAll.do"; } @RequestMapping("/toUpdate.do") public ModelAndView toUpdate(int id){ User user=userService.selectUserById(id); ModelAndView modelAndView=new ModelAndView(); modelAndView.addObject("user",user); modelAndView.setViewName("user-update"); return modelAndView; } @RequestMapping("/update") public String update(User user){ userService.update(user); return "redirect:findAll.do"; } }
    Processed: 0.013, SQL: 9