Johin7 2024-04-28 13:00 采纳率: 60%
浏览 6

Spring MVC不能增加

img

img

img


为什么Spring MVC可以查询但是不能增加一点就会出现上面的这种错误

  • 写回答

9条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-04-28 13:00
    关注

    引自免费微信小程序:皆我百晓生

    看起来你在询问关于Spring MVC中一个简单的增删查改(CRUD)功能实现遇到的问题。从你提供的信息来看,你已经实现了用户删除和查询的功能,但是在尝试添加用户时遇到了问题。错误信息没有具体提供,所以我将根据常见情况推测可能的问题并提供解决方案。

    首先,确保你已经在Spring MVC项目中正确配置了以下几个关键点:

    1. Spring MVC的核心依赖:确保在pom.xml文件中包含了Spring WebMVC的依赖。
    2. Web应用上下文配置:确认有一个名为dispatcher-servlet.xml或类似的XML配置文件,并且启用了注解驱动(<mvc:annotation-driven/>)。
    3. Controller类:检查@Controller注解是否已添加到处理请求的类上。
    4. RequestMapping注解:确保每个方法上的@RequestMapping注解路径是正确的,并且与实际发送请求的URL匹配。
    5. ModelAndView/ResponseBody:对于返回数据,使用@ResponseBody注解确保响应体被正确序列化为JSON格式。
    6. Service层和DAO层:确认UserService类和对应的持久层实现(例如JpaRepository或自定义SQL)已经被正确地注入,并且添加用户的业务逻辑是正确的。

    接下来,我会展示如何构建完整的示例代码,包括必要的类结构、配置以及处理请求的方法。这里假设你已经有一个User实体类,包含id, name, email等字段,并且有对应的getter和setter方法。

    User.java

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    
    @Entity
    public class User {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        private String name;
        private String email;
    
        // getters and setters
    }
    

    UserService.java

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class UserService {
        private final UserRepository userRepository;
    
        @Autowired
        public UserService(UserRepository userRepository) {
            this.userRepository = userRepository;
        }
    
        public int addUser(User user) {
            userRepository.save(user);
            return userRepository.count();
        }
    
        public int delUser(Long id) {
            userRepository.deleteById(id);
            return userRepository.count();
        }
    
        public List<User> getUsers(User user) {
            return userRepository.findAll();
        }
    }
    

    UserRepository.java

    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.stereotype.Repository;
    
    @Repository
    public interface UserRepository extends JpaRepository<User, Long> {
    }
    

    UserController.java

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.List;
    
    @RestController
    @RequestMapping("/api/users")
    public class UserController {
        private final UserService userService;
    
        @Autowired
        public UserController(UserService userService) {
            this.userService = userService;
        }
    
        @GetMapping
        @ResponseBody
        public ResponseEntity<List<User>> getUsers(@RequestParam(required = false) User user) {
            return ResponseEntity.ok(userService.getUsers(user));
        }
    
        @PostMapping
        @ResponseBody
        public ResponseEntity<String> addUser(@RequestBody User user) {
            int result = userService.addUser(user);
            if (result > 0) {
                return ResponseEntity.ok("add success");
            } else {
                return ResponseEntity.badRequest().body("add fail");
            }
        }
    
        @DeleteMapping("/{id}")
        @ResponseBody
        public ResponseEntity<String> delUser(@PathVariable Long id) {
            int result = userService.delUser(id);
            if (result > 0) {
                return ResponseEntity.ok("del success");
            } else {
                return ResponseEntity.badRequest().body("del fail");
            }
        }
    }
    

    dispatcher-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:component-scan base-package="your.package.name"/>
    
        <mvc:annotation-driven/>
    
    </beans>
    

    请确保上述代码中的your.package.name替换为你的实际包名。然后运行应用程序,测试添加用户的功能。如果仍然存在问题,请提供更多关于错误的详细信息,以便我能更准确地定位问题。

    评论 编辑记录

报告相同问题?

问题事件

  • 修改了问题 4月28日
  • 修改了问题 4月28日
  • 创建了问题 4月28日