7.2 JAVA

    技术2024-11-19  11

    ssm2 实现增删改查

    //UserMapper.xml <mapper namespace="com.zr.dao.UserDao" > <select id="findUserByUserName" parameterType="String" resultType="user"> select * from db_user where username=#{username} </select> <select id="findAll" resultType="user"> select * from db_user </select> <delete id="deleteById" parameterType="int"> delete from db_user where id=#{id} </delete> <insert id="add" parameterType="user"> insert into db_user (username,password) values (#{username},#{password}) </insert> <select id="selectUserById" parameterType="int" resultType="user"> select * from db_user where id=#{id} </select> <update id="update" parameterType="user"> update db_user set id=#{id},username=#{username},password=#{password} </update> </mapper> //controller @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(){ ModelAndView modelAndView=new ModelAndView(); List<User> users = userService.findAll(); modelAndView.addObject("users", users); modelAndView.setViewName("user-list"); return modelAndView; } @RequestMapping("/deleteById.do") public String deleteById(int id){ userService.deleteById(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){ ModelAndView modelAndView=new ModelAndView(); User user=userService.selectUserById(id); modelAndView.addObject("user",user); modelAndView.setViewName("user-update"); return modelAndView; } @RequestMapping("/update.do") public String update(User user){ ModelAndView modelAndView=new ModelAndView(); userService.update(user); modelAndView.addObject("user",user); modelAndView.setViewName("user-update"); return "redirect:findAll.do"; } } //dao public interface UserDao { User findUserByUserName(String username); List<User> findAll(); void deleteById(int id); void add(User user); User selectUserById(int id); void update(User user); } //service @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() { List<User> users = userDao.findAll(); return users; } @Override public void deleteById(int id) { userDao.deleteById(id); } @Override public void add(User user) { userDao.add(user); } @Override public User selectUserById(int id) { User user = userDao.selectUserById(id); return user; } @Override public void update(User user) { userDao.update(user); } } //serviceimpl public interface IUserService { boolean login(String username,String password); List<User> findAll(); void deleteById(int id); void add(User user); User selectUserById(int id); void update(User user); }
    Processed: 0.024, SQL: 9