大家好,我开始从头开始制作我的Spring Boot应用程序。我想为我的投资组合制作预订申请表。我是编码的先驱,过去一年我学习Java。我整天都在挣扎,当我想在Controller类中调用getUser
方法时,我不知道我有什么问题?简单的控制器类没有看到
方法是我的服务类。
班级分为四个不同的包
com.booking.controller
com.booking.model
com.booking.repository
com.service
这里我有问题,简直就是看不到服务类。
@Controller
@RequestMapping(path = "/user")
public class UserController{
@Autowired
private UserRepository userRepository;
@GetMapping(path = "/add")
public @ResponseBody String addNewUser(@RequestParam String name,
@RequestParam String username,
@RequestParam String email, @RequestParam String password) {
User n = new User();
n.setName(name);
n.setUsername(username);
n.setEmail(email);
n.setPassword(password);
userRepository.save(n);
return "Saved";
}
@GetMapping(path = "/all")
public @ResponseBody Iterable<User> getAllUsers() {
return userRepository.findAll();
}
@RequestMapping("/{Id}")
public User getUser(@PathVariable Long id) {
return userRepository.getUser(id);
/*Here you can see problem*/
}
}
我的用户分类
@Entity
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String name;
@Column
private String username;
@Column
private String email;
@Column
private String password;
public User() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
**我的存储库类 -
public interface UserRepository extends CrudRepository<User, Long> {
public List<User> findByUsername(String username);
}
我的服务类
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public List<User> getAllUsers(String username) {
List<User> users = new ArrayList<>();
userRepository.findByUsername(username).forEach(users::add);
return users;
}
public User getUser(Long id) {
return userRepository.findOne(id);
}
public void addUser(User user) {
userRepository.save(user);
}
public void updateUser(Long id, User user) {
userRepository.save(user);
}
public void deleteUser(Long id) {
userRepository.delete(id);
}
}