SpringMVC学习记录(九)----SSM 框架实战 用户信息增删改查

网友投稿 597 2022-11-10

SpringMVC学习记录(九)----SSM 框架实战 用户信息增删改查

SpringMVC学习记录(九)----SSM 框架实战 用户信息增删改查

文章目录

​​SSM 框架实战---用户信息增删改查​​

​​(1)效果展示​​​​(2)pojo层​​​​(3)mapper层​​​​(4)service层​​​​(5)controller层​​​​(6)前端主页​​

SSM 框架实战—用户信息增删改查

(1)效果展示

(2)pojo层

User 实体类

package com.bit.pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data@AllArgsConstructor@NoArgsConstructorpublic class User { private int id; private String username; private String password;}

(3)mapper层

UserMapper接口以及对应的xml文件

UserMapper接口

package com.bit.mapper;import com.bit.pojo.User;import org.apache.ibatis.annotations.Param;import java.util.HashMap;import java.util.List;public interface UserMapper { int insert(User user); int delete(@Param("id") int id); int update(HashMap map); List selectAll(); User selectById(int id);}

UserMapper.xml

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

(4)service层

UserService接口

package com.bit.service;import com.bit.pojo.User;import java.util.List;public interface UserService { int addUser(User user); int delteteUser(int id); int updateUser(int id,User user); List selectAllUser(); User selectOne(int id);}

UserServiceImpl 实现类

package com.bit.service;import com.bit.mapper.UserMapper;import com.bit.pojo.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.HashMap;import java.util.List;@Servicepublic class UserServiceImpl implements UserService{ @Autowired private UserMapper userMapper; public int addUser(User user) { return userMapper.insert(user); } public int delteteUser(int id) { return userMapper.delete(id); } public int updateUser(int id, User user) { HashMap map = new HashMap(); map.put("id",id); map.put("username",user.getUsername()); map.put("password",user.getPassword()); return userMapper.update(map); } public List selectAllUser() { return userMapper.selectAll(); } public User selectOne(int id) { return userMapper.selectById(id); } public static void main(String[] args) { }}

(5)controller层

UserController 类

package com.bit.controller;import com.bit.pojo.User;import com.bit.service.UserService;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.ResponseBody;import javax.jws.WebParam;@Controller@RequestMapping("/user")public class UserController { @Autowired private UserService userService; @RequestMapping("/select") @ResponseBody public Object select(){ return userService.selectAllUser(); } @RequestMapping("/insert") public Object insert(String username,String password){ System.out.println("进入到后端的insert接口中!"); User user = new User(); user.setUsername(username); user.setPassword(password); System.out.println(user); userService.addUser(user); return "redirect:/static/index.html"; } @RequestMapping("/update") public Object update(Integer id, String username,String password){ System.out.println("进入到后端的update接口中!"); User user = new User(); System.out.println(id); user.setUsername(username); user.setPassword(password); System.out.println(user); userService.updateUser(id,user); return "redirect:/static/index.html"; } @RequestMapping("/delete") public Object delete(Integer id){ System.out.println("进入到后端的delete接口中!"); System.out.println(id); int ret = userService.delteteUser(id); if(ret==1){ System.out.println("删除成功!"); }else{ System.out.println("删除失败!"); } return "redirect:/static/index.html"; }}

(6)前端主页

使用form表单实现增加、修改,使用Ajax、Dom API 实现查询和修改

练习Spring MVC


用户信息的增删改查



用户密码:


用户名: 用户密码:


用户ID 用户名 密码 编辑

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

标签:js 数据
上一篇:springboot利用aop实现接口异步(进度条)的全过程
下一篇:springboot实现对注解的切面案例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~