英文:
Passing an entity from one method to another method located in different classes
问题
我有一个对象,对象中有两个值,分别是用户名和电子邮件。我有一个控制器类,该控制器显示user.html
,其中包含两个输入框和一个按钮。当点击按钮时,输入框中的数据应该写入到数据库中。程序可以正常工作,但我不知道如何将数据写入的功能移到UserService
中。
我尝试过这样做,但是在UserService
中实现数据库操作的方法需要User
数据类型,而MainController
中的usersShow
方法使用了String
数据类型。
示例代码:
public User usersShow(User user) {
// 数据库操作的实现
}
以下是当前程序中的现有代码示例。
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";
}
}
UserService.class(这里我想把与数据库的操作移到这里,但我不知道如何做):
@Service
public class UserService {
private final UserRepository userRepository;
@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
// 你需要在这里实现与数据库相关的操作
}
如果你想将数据写入功能移到UserService
中,你需要在UserService
类中实现与数据库相关的操作,例如保存用户数据。你可以将usersShow
方法中的数据库操作代码移到UserService
的相应方法中,并确保该方法接受User
类型的参数。这样,你就可以在MainController
中调用UserService
的方法来处理数据写入操作。
英文:
I have an object, there are two values in the object, username and email, and I have a controller class, the controller shows user.html, which has two inputs and one button, when the button is clicked, the data in the inputs is written to the data, the program works, but how can i move data writing function to UserService?
I tried to do this, but the method that I implement in UserService to implement database operation requires User data type, but the method in usersShow in UserController has String data type.
Example:
public User usersShow(User user) {
//Implementation of the database
}
The following is an example of existing code that is currently in my program.
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 (Here I want to transfer work with the database, but I don’t know how):
@Service
public class UserService {
private final UserRepository userRepository;
@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
答案1
得分: 0
I couldn't do what I wanted to do, but I did better and I'm quite happy with it.
我无法做我想做的事情,但我做得更好,我对此感到非常满意。
I remade the UserService into an interface and created a UserServiceImpl created the following methods, here is the code:
我将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";
}
}
英文:
I couldn't do what I wanted to do, but I did better and I'm quite happy with it.
I remade the UserService into an interface and created a UserServiceImpl created the following methods, here is the code:
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";
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论