我如何从orderserviceimpl.java类中的用户表获取电子邮件数据
如何从orderserviceimpl.java类中的用户表获取emailID,以便可以发送 在调用senttextextemail(字符串电子邮件)时作为字符串 OrderServiceImpl.java类?
user.java class
@Setter
@Getter
@EqualsAndHashCode
@ToString
@NoArgsConstructor
//@AllArgsConstructor
@Entity
@Table(name = "`user`", uniqueConstraints = {@UniqueConstraint(columnNames = "email")})
public class User {
public User(String email, String name, String password, String address) {
this.email = email;
this.name = name;
this.password = password;
this.address = address;
}
@Id
@Column(name = "userId")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Size(max = 50)
@Email
private String email;
@Size(max = 50)
@NotBlank
private String name;
@Size(max = 100)
@NotBlank
private String password;
@Size(max = 50)
@NotBlank
private String address;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "cart", joinColumns = @JoinColumn(name = "userId"),
inverseJoinColumns = @JoinColumn(name = "foodId"))
private List<Food> cart = new ArrayList<Food>();
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "user_roles", joinColumns = @JoinColumn(name = "userId"),
inverseJoinColumns = @JoinColumn(name = "roleId"))
private Set<Role> roles = new HashSet<Role>();
}
order.java class
@Setter
@Getter
@EqualsAndHashCode
@ToString
@NoArgsConstructor
//@AllArgsConstructor
@Entity
@Table(name = "orders", uniqueConstraints = {@UniqueConstraint(columnNames = "orderTransaction")})
public class Order {
@Id
@Column(name = "orderId")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Size(max = 50)
@NotBlank
private String orderTime;
@Size(max = 50)
@NotBlank
private String orderTransaction;
@OneToMany(fetch = FetchType.LAZY)
@JoinTable(name = "users_orders", joinColumns = @JoinColumn(name = "orderId"),
inverseJoinColumns = @JoinColumn(name = "userId"))
private List<Order> orders = new ArrayList<Order>();
}
emailserviceimpl.java class
@Service
public class EmailServiceImpl implements EmailService {
private static final Logger logger = LoggerFactory
.getLogger(EmailServiceImpl.class);
@Autowired
private JavaMailSender javaMailSender;
@Override
//Sending User Object
public void sendTextEmail(String email) {
logger.info("Simple Email sending start");
SimpleMailMessage simpleMessage = new SimpleMailMessage();
simpleMessage.setTo(email); //Enter customer email
simpleMessage.setSubject("Spring Boot=> Sending simple email");
simpleMessage.setText("Dear hunger buddy customer Hope you are doing well.");
javaMailSender.send(simpleMessage);
logger.info("Simple Email sent");
}
}
orderserviceimpl.java类
@Service
public class OrderServiceImpl implements OrderService{
private static final String String = null;
@Autowired
private EmailService emailService;
@Autowired
private OrderRepo orderRepo;
/*@Autowired
private User register;*/
@Override
public Order addOrder(Order order) throws AlreadyExistsException {
if(orderRepo.existsById(order.getId())) {
throw new AlreadyExistsException("This record already exists");
}
else
{
//long temp=order.getId();
//Optional<User> optional = userRepo.findById(temp);
String email = null;
emailService.sendTextEmail(email);
return orderRepo.save(order);
}
}
@Override
public Optional<List<Order>> getAllOrder() {
return Optional.ofNullable(orderRepo.findAll());
}
@Override
public Optional<Order> getOrderById(Long id) throws IdNotFoundException {
Optional<Order> optional = orderRepo.findById(id);
if (!optional.isPresent()) {
throw new IdNotFoundException("Sorry Order Not Found");
}
return optional;
}
}
我删除了所有导入语句,以节省读者的时间。
谢谢
How to get emailId From User table in the OrderServiceImpl.java class so that it can be sent
as a String while calling sendTextEmail(String email) in
OrderServiceImpl.java class?
User.java Class
@Setter
@Getter
@EqualsAndHashCode
@ToString
@NoArgsConstructor
//@AllArgsConstructor
@Entity
@Table(name = "`user`", uniqueConstraints = {@UniqueConstraint(columnNames = "email")})
public class User {
public User(String email, String name, String password, String address) {
this.email = email;
this.name = name;
this.password = password;
this.address = address;
}
@Id
@Column(name = "userId")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Size(max = 50)
@Email
private String email;
@Size(max = 50)
@NotBlank
private String name;
@Size(max = 100)
@NotBlank
private String password;
@Size(max = 50)
@NotBlank
private String address;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "cart", joinColumns = @JoinColumn(name = "userId"),
inverseJoinColumns = @JoinColumn(name = "foodId"))
private List<Food> cart = new ArrayList<Food>();
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "user_roles", joinColumns = @JoinColumn(name = "userId"),
inverseJoinColumns = @JoinColumn(name = "roleId"))
private Set<Role> roles = new HashSet<Role>();
}
Order.java Class
@Setter
@Getter
@EqualsAndHashCode
@ToString
@NoArgsConstructor
//@AllArgsConstructor
@Entity
@Table(name = "orders", uniqueConstraints = {@UniqueConstraint(columnNames = "orderTransaction")})
public class Order {
@Id
@Column(name = "orderId")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Size(max = 50)
@NotBlank
private String orderTime;
@Size(max = 50)
@NotBlank
private String orderTransaction;
@OneToMany(fetch = FetchType.LAZY)
@JoinTable(name = "users_orders", joinColumns = @JoinColumn(name = "orderId"),
inverseJoinColumns = @JoinColumn(name = "userId"))
private List<Order> orders = new ArrayList<Order>();
}
EmailServiceImpl.java Class
@Service
public class EmailServiceImpl implements EmailService {
private static final Logger logger = LoggerFactory
.getLogger(EmailServiceImpl.class);
@Autowired
private JavaMailSender javaMailSender;
@Override
//Sending User Object
public void sendTextEmail(String email) {
logger.info("Simple Email sending start");
SimpleMailMessage simpleMessage = new SimpleMailMessage();
simpleMessage.setTo(email); //Enter customer email
simpleMessage.setSubject("Spring Boot=> Sending simple email");
simpleMessage.setText("Dear hunger buddy customer Hope you are doing well.");
javaMailSender.send(simpleMessage);
logger.info("Simple Email sent");
}
}
OrderServiceImpl.java Class
@Service
public class OrderServiceImpl implements OrderService{
private static final String String = null;
@Autowired
private EmailService emailService;
@Autowired
private OrderRepo orderRepo;
/*@Autowired
private User register;*/
@Override
public Order addOrder(Order order) throws AlreadyExistsException {
if(orderRepo.existsById(order.getId())) {
throw new AlreadyExistsException("This record already exists");
}
else
{
//long temp=order.getId();
//Optional<User> optional = userRepo.findById(temp);
String email = null;
emailService.sendTextEmail(email);
return orderRepo.save(order);
}
}
@Override
public Optional<List<Order>> getAllOrder() {
return Optional.ofNullable(orderRepo.findAll());
}
@Override
public Optional<Order> getOrderById(Long id) throws IdNotFoundException {
Optional<Order> optional = orderRepo.findById(id);
if (!optional.isPresent()) {
throw new IdNotFoundException("Sorry Order Not Found");
}
return optional;
}
}
I have removed all the import statement to save the time of reader.
ThankYou
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您在
order
实体定义中有一个合乎逻辑的问题。订单
不包含其他订单列表。但是它应该包含下订单的用户
。因此,该定义应如下纠正:然后在
orderserviceimpl
中,您可以使用以下方式访问用户信息:I think you have a logical issue in
Order
entity definition.Order
does not contain another list of Orders. But it should contain theUser
who placed the order. Hence, the definition should be corrected as follows:Then in
OrderServiceImpl
you could access user information with following: