招考管理系统代码idea,登录,注册,选择考试科目,缴费,审核,打印准考证
时间: 2024-06-09 14:10:21 浏览: 168
以下是一个简单的招考管理系统的代码示例,包括登录、注册、选择考试科目、缴费、审核和打印准考证等功能。
需要使用的技术和工具:
- Spring Boot
- MySQL
- Thymeleaf
- Bootstrap
1. 创建数据库表
我们需要创建以下表:
- users: 存储用户信息,包括用户名、密码、邮箱地址等
- exams: 存储考试科目信息,包括科目名称、考试时间、考试费用等
- orders: 存储用户订单信息,包括用户ID、考试ID、订单状态等
下面是创建表的SQL语句:
```sql
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
);
CREATE TABLE `exams` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`time` datetime NOT NULL,
`fee` decimal(10,2) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `orders` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`exam_id` int(11) NOT NULL,
`status` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `exam_id` (`exam_id`),
CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
CONSTRAINT `orders_ibfk_2` FOREIGN KEY (`exam_id`) REFERENCES `exams` (`id`)
);
```
2. 创建Spring Boot项目
使用Spring Initializr创建一个新的Spring Boot项目,添加以下依赖:
- Spring Web
- Thymeleaf
- Spring Data JPA
- MySQL Driver
3. 编写实体类
创建以下实体类:
- User: 用户信息
- Exam: 考试科目信息
- Order: 用户订单信息
User.java:
```java
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(nullable = false, unique = true)
private String username;
@Column(nullable = false)
private String password;
@Column(nullable = false)
private String email;
// 省略getter和setter方法
}
```
Exam.java:
```java
@Entity
@Table(name = "exams")
public class Exam {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private LocalDateTime time;
@Column(nullable = false)
private BigDecimal fee;
// 省略getter和setter方法
}
```
Order.java:
```java
@Entity
@Table(name = "orders")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
private User user;
@ManyToOne
@JoinColumn(name = "exam_id", nullable = false)
private Exam exam;
@Column(nullable = false)
private Integer status;
// 省略getter和setter方法
}
```
4. 编写数据访问层
创建以下接口:
- UserRepository: 用户数据访问接口
- ExamRepository: 考试科目数据访问接口
- OrderRepository: 订单数据访问接口
UserRepository.java:
```java
@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
User findByUsername(String username);
}
```
ExamRepository.java:
```java
@Repository
public interface ExamRepository extends JpaRepository<Exam, Integer> {
}
```
OrderRepository.java:
```java
@Repository
public interface OrderRepository extends JpaRepository<Order, Integer> {
List<Order> findByUser(User user);
List<Order> findByStatus(Integer status);
}
```
5. 编写服务层
创建以下服务:
- UserService: 用户服务
- ExamService: 考试科目服务
- OrderService: 订单服务
UserService.java:
```java
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public User findByUsername(String username) {
return userRepository.findByUsername(username);
}
public User save(User user) {
return userRepository.save(user);
}
}
```
ExamService.java:
```java
@Service
public class ExamService {
@Autowired
private ExamRepository examRepository;
public List<Exam> findAll() {
return examRepository.findAll();
}
public Exam findById(Integer id) {
return examRepository.findById(id).orElse(null);
}
}
```
OrderService.java:
```java
@Service
public class OrderService {
@Autowired
private OrderRepository orderRepository;
public List<Order> findByUser(User user) {
return orderRepository.findByUser(user);
}
public List<Order> findByStatus(Integer status) {
return orderRepository.findByStatus(status);
}
public Order save(Order order) {
return orderRepository.save(order);
}
}
```
6. 编写控制器
创建以下控制器:
- LoginController: 处理登录请求
- RegisterController: 处理注册请求
- ExamController: 处理考试科目请求
- OrderController: 处理订单请求
LoginController.java:
```java
@Controller
public class LoginController {
@Autowired
private UserService userService;
@GetMapping("/login")
public String login() {
return "login";
}
@PostMapping("/login")
public String login(String username, String password, HttpSession session) {
User user = userService.findByUsername(username);
if (user != null && user.getPassword().equals(password)) {
session.setAttribute("user", user);
return "redirect:/exams";
} else {
return "login";
}
}
}
```
RegisterController.java:
```java
@Controller
public class RegisterController {
@Autowired
private UserService userService;
@GetMapping("/register")
public String register() {
return "register";
}
@PostMapping("/register")
public String register(User user) {
userService.save(user);
return "redirect:/login";
}
}
```
ExamController.java:
```java
@Controller
public class ExamController {
@Autowired
private ExamService examService;
@Autowired
private OrderService orderService;
@GetMapping("/exams")
public String exams(HttpSession session, Model model) {
User user = (User)session.getAttribute("user");
List<Order> orders = user != null ? orderService.findByUser(user) : new ArrayList<>();
List<Exam> exams = examService.findAll();
exams.forEach(e -> {
e.setOrdered(orders.stream().anyMatch(o -> o.getExam().getId().equals(e.getId())));
});
model.addAttribute("exams", exams);
return "exams";
}
@GetMapping("/exam/{id}")
public String exam(@PathVariable Integer id, Model model) {
Exam exam = examService.findById(id);
model.addAttribute("exam", exam);
return "exam";
}
}
```
OrderController.java:
```java
@Controller
public class OrderController {
@Autowired
private OrderService orderService;
@PostMapping("/order")
@ResponseBody
public String order(Integer examId, HttpSession session) {
User user = (User)session.getAttribute("user");
Order order = new Order();
order.setUser(user);
order.setExam(new Exam(examId));
order.setStatus(0);
orderService.save(order);
return "success";
}
@GetMapping("/orders")
public String orders(HttpSession session, Model model) {
User user = (User)session.getAttribute("user");
List<Order> orders = user != null ? orderService.findByUser(user) : new ArrayList<>();
model.addAttribute("orders", orders);
return "orders";
}
}
```
7. 编写HTML页面
在src/main/resources/templates目录下,创建以下HTML页面:
- login.html: 登录页面
- register.html: 注册页面
- exams.html: 考试科目列表页面
- exam.html: 考试科目详情页面
- orders.html: 订单列表页面
login.html:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form method="post" th:action="@{/login}">
<div>
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
</div>
<div>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
</div>
<button type="submit">Login</button>
</form>
<a href="/register">Register</a>
</body>
</html>
```
register.html:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Register</title>
</head>
<body>
<h1>Register</h1>
<form method="post" th:action="@{/register}">
<div>
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
</div>
<div>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
</div>
<div>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
</div>
<button type="submit">Register</button>
</form>
<a href="/login">Login</a>
</body>
</html>
```
exams.html:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Exams</title>
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Exams</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="/exams">Exams</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/orders">Orders</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<a class="btn btn-outline-primary my-2 my-sm-0" href="/login">Login</a>
</form>
</div>
</nav>
<div class="container mt-3">
<h1>Exams</h1>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Time</th>
<th>Fee</th>
<th></th>
</tr>
</thead>
<tbody>
<tr th:each="exam : ${exams}">
<td th:text="${exam.name}"></td>
<td th:text="${#dates.format(exam.time, 'yyyy-MM-dd HH:mm')}"></td>
<td th:text="${exam.fee}"></td>
<td>
<a th:if="${!exam.ordered}" href="/exam/__${exam.id}__">Order</a>
<span th:if="${exam.ordered}">Ordered</span>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
```
exam.html:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Exam</title>
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Exams</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="/exams">Exams</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/orders">Orders</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<a class="btn btn-outline-primary my-2 my-sm-0" href="/login">Login</a>
</form>
</div>
</nav>
<div class="container mt-3">
<h1 th:text="${exam.name}"></h1>
<p th:text="${#dates.format(exam.time, 'yyyy-MM-dd HH:mm')}"></p>
<p th:text="${exam.fee}"></p>
<form method="post" th:action="@{/order}">
<input type="hidden" name="examId" th:value="${exam.id}">
<button type="submit" class="btn btn-primary">Order</button>
</form>
</div>
</body>
</html>
```
orders.html:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Orders</title>
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Exams</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="/exams">Exams</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/orders">Orders</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<a class="btn btn-outline-primary my-2 my-sm-0" href="/login">Login</a>
</form>
</div>
</nav>
<div class="container mt-3">
<h1>Orders</h1>
<table class="table">
<thead>
<tr>
<th>Exam</th>
<th>Time</th>
<th>Fee</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr th:each="order : ${orders}">
<td th:text="${order.exam.name}"></td>
<td th:text="${#dates.format(order.exam.time, 'yyyy-MM-dd HH:mm')}"></td>
<td th:text="${order.exam.fee}"></td>
<td>
<span th:if="${order.status == 0}">Pending</span>
<span th:if="${order.status == 1}">Paid</span>
<span th:if="${order.status == 2}">Rejected</span>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
```
8. 运行程序
现在,我们可以运行程序并在浏览器中访问http://localhost:8080/来查看招考管理系统。
阅读全文