当前位置: 首页 > article >正文

Springboot实现增删改差

一、包结构

二、各层代码

(1)数据User

public class User {
    private Integer id;
    private String userName;
    private String note;
    public User() {
        super();
    }

    public User(Integer i, String userName, String note) {
        super();
        this.id = i;
        this.userName = userName;
        this.note = note;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getNote() {
        return note;
    }
    public void setNote(String note) {
        this.note = note;
    }

}

(2)dao层

IUserDao接口

public interface IUserDao {
    public User getUser(Integer id);
    public Boolean insertUser(User user);
   public List<User> findAllUser();
    Boolean deleteUser(Integer id);
    Boolean modifyUserName(Integer id, String newUserName);
}

实现类:

@Repository
public class UserDaoImpl implements IUserDao {
    private List<User> users = new ArrayList<User>();
    public UserDaoImpl() {
        super();
        users.add(new User(1, "user_name_1", "note_1"));
        users.add(new User(2, "user_name_2", "note_2"));
        users.add(new User(3, "user_name_3", "note_3"));
    }
    @Override
      /*   根据id来返回对象*/
    public User getUser(Integer id) {
        // TODO Auto-generated method stub
        for (User user : users) {
            if (user.getId().equals(id)) {
                return user;
            }
        }
        return null;
    }
    @Override
    public Boolean insertUser(User user) {
        // TODO Auto-generated method stub
        Boolean flag=false;
        System.out.println(user.getUserName()+">>"+user.getNote());
        users.add(user);        //User为上上面申明的集合 add方法为List集合的方法
        flag=true;
        return flag;
    }
    @Override public List<User> findAllUser() {   return users; }
}

(3)service层:

IUserService接口:

public interface IUserService {
    public User getUser(Integer id);
    public List<User> findAllUser();
    public Boolean insertUser(User user);
    public List<User> queryUser(String userName,String note);

    Boolean deleteUser(Integer id);

    Boolean modifyUserName(Integer id, String newUserName);
}

实现类:


@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserDao userDao;
  /*  省去了申明传值的步骤*/
    @Override
    /*根据User来获取对象*/
    public User getUser(Integer id) {
        // TODO Auto-generated method stub
        return userDao.getUser(id);
    }
    @Override
    public Boolean insertUser(User user) {
        // TODO Auto-generated method stub
        Boolean flag=false;
        System.out.println(user.getUserName()+">>"+user.getNote());
        flag=userDao.insertUser(user);
        return flag;
    }
    @Override
    public List<User> findAllUser() {
        List<User> users = userDao.findAllUser();
        if (users.size() > 1) {
            for (int i = 1; i < users.size(); i++) {
                User currentUser = users.get(i);
                User previousUser = users.get(i-1);
                if (currentUser.getId() == null) {
                    currentUser.setId(previousUser.getId()+1);
                }
            }
        }
        return users;
    }

    @Override
    public List<User> queryUser(String userName1, String note1) {
        List<User> users = userDao.findAllUser();
        List<User> result = new ArrayList<>();
        for (User user : users) {
            if (user.getUserName().equals(userName1) && user.getNote().equals(note1)) {
                result.add(user);
            }
        }
        return result;
    }
    public Boolean deleteUser(Integer id) {
        return userDao.deleteUser(id);
    }
    @Override
    public Boolean modifyUserName(Integer id, String newUserName) {
        return userDao.modifyUserName(id, newUserName);
    }
}

(4)controller层

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService=null;
    @GetMapping("/add")
    public String add() {

        return "add";
    }
    /*
     * 新增用户
     *@param user 通过@RequestBody注解得到JSON参数
     *return 返回用户信息
     */
    @PostMapping("/insert")
    @ResponseBody
    public User insert(@RequestBody User user) {
        userService.insertUser(user);
        return user;
    }
    //显示数据 http://localhost:8080/user/list
    @RequestMapping("/list")
    @ResponseBody
    public List<User> list(){
        //访问模型层得到数据
        List<User> users=userService.findAllUser();
        return users;
    }
    //显示数据 http://localhost:8080/user/table
    @RequestMapping("/table")
    public ModelAndView table0() {
        //访问模型层得到数据
        List<User> users=userService.findAllUser();
        System.out.println("111");
        //模型和视图
        ModelAndView mv=new ModelAndView();
        //定义模型视图 1
        mv.setViewName("table");
        //加入数据模型
        mv.addObject("users",users);
        //返回模型和视图
        return mv;
    }

    @RequestMapping(value = "/query1")
    @ResponseBody
    public ModelAndView query1(@RequestParam(value = "userName") String userName,@RequestParam("note") String note){
        List<User> users=userService.queryUser(userName,note);
        ModelAndView mv=new ModelAndView();
        mv.setViewName("table");
        mv.addObject("users",users);
        return mv;
    }

    @RequestMapping("/delete/{id}")
    public String deleteUser(@PathVariable Integer id, Model model) {
        boolean success = userService.deleteUser(id);
        model.addAttribute("success", success);
        model.addAttribute("deletedUserId", id);
        return "delete";
    }

    @GetMapping("/modify")
    public String showModifyPage() {
        return "modify";
    }
    @PostMapping("/modify")
    @ResponseBody
    public ResponseEntity<Map<String, String>> modifyUser(
            @RequestParam int userId,
            @RequestParam String newUserName) {

        // 进行修改用户的逻辑,假设这里调用了修改用户的 service 方法
        boolean success = userService.modifyUserName(userId, newUserName);

        // 返回修改后的结果
        Map<String, String> result = new HashMap<>();
        result.put("modifiedUserId", String.valueOf(userId));
        result.put("newUserName", newUserName);

        // 返回 ResponseEntity
        return ResponseEntity.ok(result);
    }
}


http://www.kler.cn/a/148425.html

相关文章:

  • MySQL与Oracle对比及区别
  • Python 随笔
  • 【数据结构与算法】第11课—数据结构之选择排序和交换排序
  • 11张思维导图带你快速学习java
  • 常用的Anaconda Prompt命令行指令
  • 24/11/13 算法笔记<强化学习> DQN算法
  • 如何把 Oracle 19C RAC+DG加入到ORACLE EM 13C监控
  • 使用 sync.Cond 来协调并发 goroutine 的访问共享资源
  • Python实现Windows服务自启动、禁用、启动、停止、删除
  • 【传智杯】儒略历、评委打分、萝卜数据库题解
  • 【漫谈】信创
  • 位运算总结
  • linux下的工具---yum
  • 为第一个原生Spring5应用程序添加上Log4J日志框架!
  • Vue2问题:如何全局使用less和sass变量?
  • TypeScript 学习笔记 第二部分 webpack 创建typescript项目
  • vue3中shallowReactive与shallowRef
  • Kotlin语言的一些技术点
  • Redis 与其他数据库的不同之处 | Navicat
  • 【业务实战】mysql数据库中<标签tag功能>一般是怎么设计的?
  • 【RLChina2023】CCF 苏州 记录
  • 微信小程序开发——项目开发入门
  • C++ 泛型编程,函数模版和类模版
  • 高等数学零基础篇复习笔记
  • 离线配置conda环境
  • 基于Python的网络爬虫设计与实现