springboot+mybatis-plus+Ajax+layui+mysql实现员工注册(id自增)
时间: 2023-05-28 20:02:40 浏览: 254
本文将介绍如何使用Spring Boot、MyBatis-Plus、Ajax、Layui和MySQL实现员工注册功能。
这个注册功能包含了员工ID自增的实现,让我们看看如何完成这个任务。
1.创建项目和数据库
首先,我们需要创建一个Spring Boot项目,并创建一个名为employee的数据库,其中包含一个名为emp的员工表。
CREATE TABLE `emp` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`age` int(11) NOT NULL,
`sex` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
2.添加依赖
我们需要添加以下依赖项:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
</dependencies>
3.配置数据源
我们需要在application.properties中配置数据源:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/employee?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456
4.配置MyBatis-Plus
在使用MyBatis-Plus之前,我们需要正常配置MyBatis,并至少创建一个Mapper。这里我们以EmployeeMapper为例:
@Mapper
public interface EmployeeMapper extends BaseMapper<Employee> {
}
然后,我们在application.properties中添加以下配置:
# mapper扫描
mybatis-plus.mapper-locations=classpath*:/mapper/*.xml
# 实体扫描
mybatis-plus.typeAliasesPackage=com.example.demo.entity
5.创建实体类
我们需要创建一个名为Employee的实体类,其属性与emp表中的列相对应。
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Employee {
private Long id;
private String name;
private Integer age;
private String sex;
}
6.创建控制器和页面
接下来,我们需要创建一个EmployeeController,通过此控制器向客户端提供员工注册页面和保存方法。
@Controller
public class EmployeeController {
@GetMapping("/employee")
public String index() {
return "employee/register";
}
@PostMapping("/employee/save")
@ResponseBody
public Long save(@RequestBody Employee employee) {
employeeMapper.insert(employee);
return employee.getId();
}
@Autowired
private EmployeeMapper employeeMapper;
}
然后,我们在resources/templates目录下创建一个register.html页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Employee Register</title>
<link rel="stylesheet" href="//cdn.bootcss.com/layui/2.5.6/css/layui.min.css">
<style>
form {margin: 20px auto; width: 500px;}
.layui-input-block {margin-right: 0;}
</style>
</head>
<body>
<div class="layui-container">
<div class="layui-card layui-anim layui-anim-up">
<div class="layui-card-header">Employee Register</div>
<div class="layui-card-body">
<form class="layui-form">
<div class="layui-form-item">
<label class="layui-form-label">Name</label>
<div class="layui-input-block">
<input type="text" name="name" required lay-verify="required" placeholder="Name" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">Age</label>
<div class="layui-input-block">
<input type="text" name="age" required lay-verify="required|number" placeholder="Age" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">Sex</label>
<div class="layui-input-block">
<input type="radio" name="sex" value="male" title="Male" checked>
<input type="radio" name="sex" value="female" title="Female">
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submit lay-filter="save">Register</button>
</div>
</div>
</form>
</div>
</div>
</div>
<script src="//cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdn.bootcss.com/layui/2.5.6/layui.min.js"></script>
<script>
layui.use(['form', 'layer'], function() {
var form = layui.form,
layer = layui.layer;
form.on('submit(save)', function(data) {
$.ajax({
url: '/employee/save',
type: 'POST',
data: JSON.stringify({
name: data.field.name,
age: data.field.age,
sex: data.field.sex
}),
contentType: 'application/json',
success: function(id) {
layer.msg('Your employee ID is ' + id);
}
});
return false;
});
});
</script>
</body>
</html>
7.运行
现在,我们可以启动Spring Boot应用程序,并打开浏览器访问http://localhost:8080/employee,创建一个员工记录。您将看到一个注册表单,输入完信息后,单击“注册”按钮。之后,它将通过Ajax将信息发送到控制器,并将Employee对象保存到数据库中。成功后,您将获得新创建的员工ID。
到这里,我们已经成功地使用Spring Boot、MyBatis-Plus、Ajax、Layui和MySQL实现了员工注册功能,包含了员工ID自增的实现。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)