瑞吉外卖01

1.创建数据库(导入sql文件)

2.创建springboot项目,完成pom.xml依赖

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.7.4</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.example</groupId>
12     <artifactId>reggie</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <name>reggie</name>
15     <description>reggie</description>
16     <properties>
17         <java.version>11</java.version>
18     </properties>
19     <dependencies>
20         <dependency>
21             <groupId>org.mybatis.spring.boot</groupId>
22             <artifactId>mybatis-spring-boot-starter</artifactId>
23             <version>2.2.2</version>
24         </dependency>
25 
26 
27         <dependency>
28             <groupId>org.projectlombok</groupId>
29             <artifactId>lombok</artifactId>
30             <optional>true</optional>
31         </dependency>
32         <dependency>
33             <groupId>org.springframework.boot</groupId>
34             <artifactId>spring-boot-starter-test</artifactId>
35             <scope>test</scope>
36         </dependency>
37         <dependency>
38             <groupId>com.baomidou</groupId>
39             <artifactId>mybatis-plus-boot-starter</artifactId>
40             <version>3.4.2</version>
41         </dependency>
42         <dependency>
43             <groupId>com.alibaba</groupId>
44             <artifactId>fastjson</artifactId>
45             <version>1.2.76</version>
46         </dependency>
47         <dependency>
48             <groupId>org.springframework.boot</groupId>
49             <artifactId>spring-boot-starter-web</artifactId>
50         </dependency>
51 
52         <dependency>
53             <groupId>commons-lang</groupId>
54             <artifactId>commons-lang</artifactId>
55             <version>2.6</version>
56         </dependency>
57 
58         <dependency>
59             <groupId>mysql</groupId>
60             <artifactId>mysql-connector-java</artifactId>
61             <scope>runtime</scope>
62         </dependency>
63 
64         <dependency>
65             <groupId>com.alibaba</groupId>
66             <artifactId>druid-spring-boot-starter</artifactId>
67             <version>1.1.23</version>
68         </dependency>
69         <dependency>
70             <groupId>org.springframework</groupId>
71             <artifactId>spring-webmvc</artifactId>
72             <version>5.3.20</version>
73         </dependency>
74     </dependencies>
75 
76     <build>
77         <plugins>
78             <plugin>
79                 <groupId>org.springframework.boot</groupId>
80                 <artifactId>spring-boot-maven-plugin</artifactId>
81                 <configuration>
82                     <excludes>
83                         <exclude>
84                             <groupId>org.projectlombok</groupId>
85                             <artifactId>lombok</artifactId>
86                         </exclude>
87                     </excludes>
88                 </configuration>
89             </plugin>
90         </plugins>
91     </build>
92 
93 </project>

3.完成yml文件配置

 1 server:
 2   port: 8080
 3 spring:
 4   application:
 5     name: reggie_take_out
 6   datasource:
 7     druid:
 8       driver-class-name: com.mysql.cj.jdbc.Driver
 9       url: jdbc:mysql://localhost:3306/reggie?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
10       username: root
11       password: 123456
12 mybatis-plus:
13   configuration:
14     #在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射
15     map-underscore-to-camel-case: true
16     log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
17   global-config:
18     db-config:
19       id-type: ASSIGN_ID

 

4.完成前端资源导入

 

 

 5.创建config包,并创建WebMvcConfig类(放行静态资源)

 1 @Slf4j
 2 @Configuration
 3 public class WebMvcConfig extends WebMvcConfigurationSupport {
 4     /**
 5      * 放行静态资源
 6      */
 7     @Override
 8     protected void addResourceHandlers(ResourceHandlerRegistry registry) {
 9         log.info("成功!");
10         registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
11         registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");
12     }
13 }

6.创建entity包导入员工类

 1 /**
 2  * 员工实体类
 3  */
 4 @Data
 5 public class Employee implements Serializable {
 6 
 7     private static final long serialVersionUID = 1L;
 8 
 9     private Long id;
10 
11     private String username;
12 
13     private String name;
14 
15     private String password;
16 
17     private String phone;
18 
19     private String sex;
20 
21     private String idNumber;//身份证号码  驼峰命名(对应id_number)
22 
23     private Integer status;
24 
25     private LocalDateTime createTime;
26 
27     private LocalDateTime updateTime;
28 
29     @TableField(fill = FieldFill.INSERT)
30     private Long createUser;
31 
32     @TableField(fill = FieldFill.INSERT_UPDATE)
33     private Long updateUser;
34 
35 }

7.创建mapper包并创建Employeemapper接口

 1 @Mapper 2 public interface EmployeeMapper extends BaseMapper<Employee> {  } 

8.创建service包,和iml包

 

 

 9.创建EmpoleeService接口

 

 

 10.EmployeeService接口实现类

1 @Service
2 public class EmployeeServiceIml extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
3 }

11.创建controller包并创建EmployeeController类(实现登录和退出)

 1 package com.example.reggie.controller;
 2 
 3 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 4 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 5 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 6 import com.example.reggie.common.R;
 7 import com.example.reggie.entity.Employee;
 8 import com.example.reggie.service.EmployeeService;
 9 import lombok.extern.slf4j.Slf4j;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.util.DigestUtils;
12 import org.springframework.web.bind.annotation.PostMapping;
13 import org.springframework.web.bind.annotation.RequestBody;
14 import org.springframework.web.bind.annotation.RequestMapping;
15 import org.springframework.web.bind.annotation.RestController;
16 
17 import javax.servlet.http.HttpServletRequest;
18 
19 /**
20  * 员工登录
21 
22  */
23 @Slf4j
24 @RestController
25 @RequestMapping("/employee")
26 public class EmployeeController {
27     @Autowired
28     private EmployeeService employeeService;
29     @PostMapping("/login")
30     public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {//@RequestBody的作用其实是将json格式的数据转为java对象
31         //1.将页面提交的密码进行md5加密
32         String password = employee.getPassword();
33         password=DigestUtils.md5DigestAsHex(password.getBytes());
34         //2.将页面提交的username进行数据库查询
35         LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
36         queryWrapper.eq(Employee::getUsername, employee.getUsername());//queryWrapper.eq(实体类::属性,值);
37         Employee emp = employeeService.getOne(queryWrapper);
38 
39         //3.如果没有查询到将返回查询失败结果
40         if(emp==null){
41             return  R.error("登录失败!");
42         }
43 
44         //4.密码比对,如果不一致将返回登陆失败结果
45         if(!emp.getPassword().equals(password)){
46             return  R.error("登录失败!");
47         }
48 
49         //5.查询员工状态,如果为已禁用状态,将返回员工已禁用结果
50         if (emp.getStatus()==0){
51 
52             return R.error("账号已禁用");
53         }
54         //6.登陆成功,将员工id存入session并返回登陆成功结果
55         request.getSession().setAttribute("employee",emp.getId());
56         return R.success(emp);
57 
58 
59     }
60     /**
61      * 员工退出
62      * @param request
63      * @return
64      */
65     @PostMapping("/logout")
66     public R<String> logout(HttpServletRequest request){
67         //清理Session中保存的当前登录员工的id
68         request.getSession().removeAttribute("employee");
69         return R.success("退出成功");
70     }
71 
72 }

 




posted @ 2022-10-04 22:50  赵三毛  阅读(99)  评论(0)    收藏  举报