springboot+mybatis-plus+Ajax+layui+mysql实现员工注册(id自增,姓名,年龄,性别,部门,入职时间,密码)
时间: 2023-05-28 10:02:48 浏览: 169
springboot+mybatis+mysql员工管理系统
5星 · 资源好评率100%
首先,我们需要创建一个员工实体类,包含上述属性。假设这个实体类的名字为Employee。
```
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String sex;
private String department;
private Date hireDate;
private String password;
}
```
在pom.xml文件中加入以下依赖:
```xml
<!-- springboot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>latest-version</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<!-- layui -->
<dependency>
<groupId>com.clay</groupId>
<artifactId>layui-spring-boot-starter</artifactId>
<version>latest-version</version>
</dependency>
```
配置Mybatis-plus的分页插件、数据源和Mapper扫描路径。在application.yml文件中加入以下配置:
```yml
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.demo.entity.*
global-config:
db-config:
id-type: auto
table-prefix: emp_
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-undeserialized-object: true
use-generated-keys: true
default-fetch-size: 1000
default-statement-timeout: 30000
use-column-label: true
call-setters-on-nulls: true
plugins:
- com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor
spring:
datasource:
url: jdbc:mysql://localhost:3306/demo?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
hikari:
maximum-pool-size: 5
minimum-idle: 5
auto-commit: true
idle-timeout: 180000
mybatis:
mapper-locations: classpath:mapper/**/*.xml
type-aliases-package: com.example.demo.entity.*
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
```
创建Mapper接口,用于对Employee的增删改查操作。
```java
@Mapper
public interface EmployeeMapper extends BaseMapper<Employee> {
}
```
创建Controller类,包含员工注册的接口方法。
```java
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeMapper employeeMapper;
@PostMapping("/register")
public Object register(@RequestBody Employee employee) {
employeeMapper.insert(employee);
return Result.success();
}
}
```
在前端页面中,使用Layui框架实现员工注册的表单页面。在JavaScript代码中使用Ajax提交表单数据。
```html
<div class="layui-form-item">
<label class="layui-form-label">姓名</label>
<div class="layui-input-inline">
<input type="text" name="name" id="name" required
lay-verify="required" autocomplete="off"
class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">年龄</label>
<div class="layui-input-inline">
<input type="number" name="age" id="age" required
lay-verify="required|number" autocomplete="off"
class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">性别</label>
<div class="layui-input-inline">
<select name="sex" id="sex" required lay-verify="required">
<option value="">请选择</option>
<option value="male">男</option>
<option value="female">女</option>
</select>
</div>
</div>
<!-- 其他表单项略 -->
<button class="layui-btn" onclick="register()">注册</button>
<script>
function register() {
$.ajax({
type: "POST",
url: "/employee/register",
data: $("#form").serialize(),
dataType: "json",
success: function (data) {
if (data.code === 0) {
layer.msg("注册成功!");
} else {
layer.msg("注册失败!");
}
},
error: function () {
layer.msg("请求失败!");
}
});
}
</script>
```
阅读全文