springboot+ajax+layui+mysql实现注册(id自增,姓名,密码,部门,入职时间,性别,年龄)
时间: 2023-05-28 14:08:02 浏览: 116
1. 创建数据库表
在MySQL中创建一个名为user的表,表结构如下:
```sql
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`password` varchar(20) DEFAULT NULL,
`department` varchar(20) DEFAULT NULL,
`hire_date` date DEFAULT NULL,
`gender` varchar(10) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
```
2. 创建SpringBoot项目
创建一个SpringBoot项目,并在pom.xml文件中添加以下依赖:
```xml
<!-- SpringBoot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
```
3. 配置数据库连接
在application.properties文件中配置数据库连接信息:
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
```
4. 创建实体类
创建一个名为User的实体类,并使用Lombok注解简化代码:
```java
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String name;
private String password;
private String department;
private Date hireDate;
private String gender;
private Integer age;
}
```
5. 创建Mapper接口
创建一个名为UserMapper的Mapper接口,并在其中定义增加用户的方法:
```java
@Mapper
public interface UserMapper {
@Insert("INSERT INTO user(name, password, department, hire_date, gender, age) VALUES(#{name}, #{password}, #{department}, #{hireDate}, #{gender}, #{age})")
void addUser(User user);
}
```
6. 创建Controller
创建一个名为UserController的Controller,并在其中定义增加用户的方法:
```java
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserMapper userMapper;
@PostMapping("/addUser")
public String addUser(@RequestBody User user) {
userMapper.addUser(user);
return "success";
}
}
```
7. 创建前端页面
使用Layui框架创建一个名为register.html的前端页面,并在其中添加以下代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户注册</title>
<link rel="stylesheet" href="layui/css/layui.css">
<script src="layui/layui.js"></script>
</head>
<body>
<div class="layui-container">
<form class="layui-form" action="">
<div class="layui-form-item">
<label class="layui-form-label">姓名</label>
<div class="layui-input-block">
<input type="text" name="name" required lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">密码</label>
<div class="layui-input-block">
<input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">部门</label>
<div class="layui-input-block">
<input type="text" name="department" required lay-verify="required" placeholder="请输入部门" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">入职时间</label>
<div class="layui-input-block">
<input type="text" name="hireDate" required lay-verify="required" placeholder="请选择入职时间" autocomplete="off" class="layui-input" id="hireDate">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">性别</label>
<div class="layui-input-block">
<input type="radio" name="gender" value="男" title="男" checked>
<input type="radio" name="gender" value="女" title="女">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">年龄</label>
<div class="layui-input-block">
<input type="number" name="age" required lay-verify="required" placeholder="请输入年龄" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
<button type="reset" class="layui-btn layui-btn-primary">重置</button>
</div>
</div>
</form>
</div>
<script>
//Demo
layui.use('laydate', function(){
var laydate = layui.laydate;
//执行一个laydate实例
laydate.render({
elem: '#hireDate' //指定元素
});
});
layui.use('form', function(){
var form = layui.form;
//监听提交
form.on('submit(formDemo)', function(data){
console.log(data.field);
$.ajax({
url: "/user/addUser",
type: "POST",
contentType: "application/json;charset=utf-8",
data: JSON.stringify(data.field),
success: function(res) {
if (res === "success") {
alert("注册成功!");
} else {
alert("注册失败!");
}
}
});
return false;
});
});
</script>
</body>
</html>
```
8. 运行项目
在浏览器中输入http://localhost:8080/register.html,即可进入用户注册页面。在页面中填写用户信息并点击“立即提交”按钮,即可完成用户注册。注册成功后会弹出提示框。可以在MySQL中查看是否添加了新的用户。
阅读全文