spring怎么做点菜系统和登陆界面连接一起源代码
时间: 2024-02-05 19:04:49 浏览: 36
首先,我们需要创建一个数据库,用于存储点菜系统的数据,比如菜品信息、订单信息等等。可以使用Spring框架中的JDBC或者Hibernate来操作数据库。
接下来,我们需要设计菜品信息的实体类和订单信息的实体类,并且在这两个实体类中定义好它们之间的关联关系。同时,我们还需要设计用户信息的实体类,用于存储登录用户的信息。
在控制器层,我们需要编写登录界面和点菜系统的相关逻辑。在登录界面中,我们可以使用Spring Security框架来实现用户的认证和授权。在点菜系统中,我们需要实现菜品的展示、添加、删除和修改等功能,并且要将订单信息保存到数据库中。
最后,在前端页面中,我们可以使用Thymeleaf模板引擎来实现对后端数据的展示和交互。
以下是一个简单的示例代码,供您参考:
```java
// 菜品信息实体类
@Entity
public class Dish {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private Float price;
// 省略其他属性和方法
}
// 订单信息实体类
@Entity
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String customerName;
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL)
private List<OrderItem> orderItems;
// 省略其他属性和方法
}
// 订单项实体类
@Entity
public class OrderItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Integer quantity;
@ManyToOne
private Dish dish;
@ManyToOne
private Order order;
// 省略其他属性和方法
}
// 用户信息实体类
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String password;
// 省略其他属性和方法
}
// 控制器
@Controller
public class DishController {
@Autowired
private DishRepository dishRepository;
@Autowired
private OrderRepository orderRepository;
@Autowired
private OrderItemRepository orderItemRepository;
@Autowired
private UserRepository userRepository;
// 登录页面
@GetMapping("/login")
public String login() {
return "login";
}
// 登录成功后跳转到点菜系统页面
@GetMapping("/menu")
public String menu(Model model) {
List<Dish> dishes = dishRepository.findAll();
model.addAttribute("dishes", dishes);
return "menu";
}
// 添加菜品
@PostMapping("/addDish")
public String addDish(Dish dish) {
dishRepository.save(dish);
return "redirect:/menu";
}
// 删除菜品
@GetMapping("/deleteDish/{id}")
public String deleteDish(@PathVariable Long id) {
dishRepository.deleteById(id);
return "redirect:/menu";
}
// 修改菜品
@PostMapping("/updateDish/{id}")
public String updateDish(@PathVariable Long id, Dish dish) {
dish.setId(id);
dishRepository.save(dish);
return "redirect:/menu";
}
// 提交订单
@PostMapping("/submitOrder")
public String submitOrder(String customerName, Long[] dishIds, Integer[] quantities) {
Order order = new Order();
order.setCustomerName(customerName);
orderRepository.save(order);
for (int i = 0; i < dishIds.length; i++) {
OrderItem orderItem = new OrderItem();
orderItem.setDish(dishRepository.getOne(dishIds[i]));
orderItem.setQuantity(quantities[i]);
orderItem.setOrder(order);
orderItemRepository.save(orderItem);
}
return "redirect:/menu";
}
}
// 用户认证和授权配置
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserRepository userRepository;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/menu")
.permitAll()
.and()
.logout()
.permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userDetailsService())
.passwordEncoder(passwordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
@Bean
public UserDetailsService userDetailsService() {
return username -> userRepository.findByUsername(username)
.orElseThrow(() -> new UsernameNotFoundException("User not found: " + username));
}
}
```
以上代码仅供参考,实际上您需要根据您的具体需求来编写代码。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)