java 将实体从一个方法传递到位于不同类中的另一个方法

ax6ht2ek  于 2023-02-11  发布在  Java
关注(0)|答案(1)|浏览(242)

我有一个对象,有两个值 在对象中,用户名和电子邮件,我有一个控制器类,控制器显示user.html,它有两个输入和一个按钮,当点击按钮时,输入中的数据被写入数据,程序工作,但我如何将数据写入功能转移到UserService?
我尝试过这样做,但是我在UserService中实现的实现数据库操作的方法需要User数据类型,而UserController中的usersShow中的方法具有String数据类型。
示例:

public User usersShow(User user) {
//Implementation of the database
}

下面是我的程序中当前存在的现有代码的示例。
MainController.class:

@Controller
@RequestMapping
public class MainController {

private final UserRepository userRepository;

@Autowired
public MainController(UserRepository userRepository) {
this.userRepository = userRepository;
}

@GetMapping(path = "/users")
public String usersPage(Model model) {
model.addAttribute("user", new User());
return "user";
    
}

@PostMapping(path = "/users")
public String usersShow(@ModelAttribute("user") User user) {
System.out.println(userRepository.findAll());
userRepository.save(user);
return "redirect:/user";
}

@Controller
@RequestMapping
public class MainController {

private final UserRepository userRepository;

@Autowired
public MainController(UserRepository userRepository) {
this.userRepository = userRepository;
}

@GetMapping(path = "/users")
public String usersPage(Model model) {
model.addAttribute("user", new User());
return "user";
    
}

@PostMapping(path = "/users")
public String usersShow(@ModelAttribute("user") User user) {
System.out.println(userRepository.findAll());
userRepository.save(user);
return "redirect:/user";
}

}

UserService.class(这里我想转移数据库的工作,但我不知道如何转移):

@Service
public class UserService {

private final UserRepository userRepository;

@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
bf1o4zei

bf1o4zei1#

我不能做我想做的事情,但我做得更好,我很高兴。
我将UserService重新制作成一个接口,并创建了一个UserServiceImpl,创建了以下方法,下面是代码:
UserService.interface:

package authentication.service;

import java.util.List;
import java.util.Optional;

import authentication.entity.User;

public interface UserService {

User addUser(User user);

}

UserServiceImpl.class:

package authentication.service;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import authentication.entity.User;
import authentication.repository.UserRepository;

@Service
public class UserServiceImpl implements UserService {

private final UserRepository userRepository;

@Autowired
public UserServiceImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}

@Override
public User addUser(User user) {
User saveUser = userRepository.saveAndFlush(user);
    
return saveUser;
}

}

MainController.class:

package authentication.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import authentication.entity.User;
import authentication.service.UserService;

@Controller
@RequestMapping
public class MainController {

private final UserService userService;

@Autowired
public MainController(UserService userService) {
this.userService = userService;
}

@GetMapping(path = "/users")
public String usersPage(Model model) {
model.addAttribute("user", new User());
return "user";
    
}

@PostMapping(path = "/users")
public String usersShow(@ModelAttribute("user") User user) {
userService.addUser(user);
System.out.println(userService.findAll());
return "redirect:/user";
}

}

相关问题