流程图外部循环结构怎么画
时间: 2023-05-28 20:02:36 浏览: 133
为了实现员工的登录和注册功能,我们需要使用SpringBoot,Mybatis-Plus,Ajax,Layui和MySQL等技术实现。
前置条件:
- 已经安装了Java(JDK8+)
- 已经安装了Maven
- 已经安装了MySQL数据库
- 已经学习过SpringBoot,Mybatis-Plus,Ajax和Layui
1. 创建一个SpringBoot项目
在IDE中创建一个新的SpringBoot项目。
2. 添加依赖
添加Mybatis-Plus的依赖。
```
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.1</version>
</dependency>
```
添加Lombok的依赖。
```
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
```
添加Ajax和Layui的CDN链接。
```
<!-- Ajax -->
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<!-- Layui -->
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/layui/2.5.7/css/layui.min.css">
<script src="https://cdn.bootcdn.net/ajax/libs/layui/2.5.7/layui.all.min.js"></script>
```
3. 创建数据库
创建一个名为employee的数据库,并创建employee表。
```
CREATE DATABASE employee;
CREATE TABLE `employee` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id',
`name` varchar(30) DEFAULT NULL COMMENT '员工姓名',
`password` varchar(30) DEFAULT NULL COMMENT '登录密码',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='员工表';
```
4. 配置数据库连接
在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&serverTimezone=GMT%2B8&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=root
```
5. 创建实体类
创建一个Employee实体类,并使用Lombok注解简化代码。
```java
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
private Long id;
private String name;
private String password;
}
```
6. 创建Mapper类
创建一个EmployeeMapper接口,并继承Mybatis-Plus的BaseMapper接口,用于操作employee表。
```java
@Mapper
public interface EmployeeMapper extends BaseMapper<Employee> {
}
```
7. 创建Service类
创建一个EmployeeService类,并注入EmployeeMapper,实现员工的登录和注册功能。其中,登录功能通过查询指定用户名和密码的员工记录进行验证,注册功能通过新增员工记录实现。
```java
@Service
public class EmployeeService {
@Autowired
private EmployeeMapper employeeMapper;
public Employee login(String name, String password) {
QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", name);
queryWrapper.eq("password", password);
return employeeMapper.selectOne(queryWrapper);
}
public boolean register(Employee employee) {
return employeeMapper.insert(employee) > 0;
}
}
```
8. 创建Controller类
创建一个EmployeeController类,并注入EmployeeService,实现员工的登录和注册请求处理方法。
```java
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@PostMapping("/login")
public Result login(@RequestParam String name, @RequestParam String password) {
Employee employee = employeeService.login(name, password);
if (employee != null) {
return Result.success("登录成功");
} else {
return Result.error("用户名或密码错误");
}
}
@PostMapping("/register")
public Result register(@RequestBody Employee employee) {
boolean result = employeeService.register(employee);
if (result) {
return Result.success("注册成功");
} else {
return Result.error("注册失败");
}
}
}
```
9. 创建Result类
创建一个Result类,并封装请求响应数据,用于统一管理请求响应格式。
```java
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result {
private Integer code;
private String msg;
private Object data;
public static Result success(String msg) {
return new Result(200, msg, null);
}
public static Result success(String msg, Object data) {
return new Result(200, msg, data);
}
public static Result error(String msg) {
return new Result(500, msg, null);
}
public static Result error(String msg, Object data) {
return new Result(500, msg, data);
}
}
```
10. 创建HTML页面
创建一个login.html页面和register.html页面,分别用于员工的登录和注册操作。
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/layui/2.5.7/css/layui.min.css">
<script src="https://cdn.bootcdn.net/ajax/libs/layui/2.5.7/layui.all.min.js"></script>
</head>
<body>
<div style="margin: 100px auto; width: 400px;">
<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">
<div class="layui-input-block">
<button class="layui-btn" lay-submit lay-filter="login">登录</button>
<button type="reset" class="layui-btn layui-btn-primary">重置</button>
</div>
</div>
</form>
</div>
<script>
layui.use(['form', 'layer'], function(){
var form = layui.form;
var layer = layui.layer;
// 监听表单提交按钮
form.on('submit(login)', function(data){
$.ajax({
url: '/employee/login',
type: 'POST',
data: {
name: data.field.name,
password: data.field.password
},
success: function(res) {
if (res.code === 200) {
layer.msg(res.msg);
setTimeout(function() {
window.location.href = '/index.html';
}, 1000);
} else {
layer.msg(res.msg);
}
},
error: function() {
layer.msg('系统异常,请稍后重试');
}
});
return false;
});
});
</script>
</body>
</html>
```
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/layui/2.5.7/css/layui.min.css">
<script src="https://cdn.bootcdn.net/ajax/libs/layui/2.5.7/layui.all.min.js"></script>
</head>
<body>
<div style="margin: 100px auto; width: 400px;">
<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">
<div class="layui-input-block">
<button class="layui-btn" lay-submit lay-filter="register">注册</button>
<button type="reset" class="layui-btn layui-btn-primary">重置</button>
</div>
</div>
</form>
</div>
<script>
layui.use(['form', 'layer'], function(){
var form = layui.form;
var layer = layui.layer;
// 监听表单提交按钮
form.on('submit(register)', function(data){
$.ajax({
url: '/employee/register',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
name: data.field.name,
password: data.field.password
}),
success: function(res) {
if (res.code === 200) {
layer.msg(res.msg);
setTimeout(function() {
window.location.href = '/login.html';
}, 1000);
} else {
layer.msg(res.msg);
}
},
error: function() {
layer.msg('系统异常,请稍后重试');
}
});
return false;
});
});
</script>
</body>
</html>
```
至此,我们已经完成了员工的登录和注册功能实现。可以通过运行SpringBoot应用来访问/login.html和/register.html页面,并进行登录和注册操作。
阅读全文