JAVA实战项目-瑞吉外卖项目

瑞吉外卖开发笔记 一

数据库环境搭建

创建数据库

JAVA实战项目-瑞吉外卖项目_第1张图片

执行SQL脚本

JAVA实战项目-瑞吉外卖项目_第2张图片

数据表

JAVA实战项目-瑞吉外卖项目_第3张图片

maven项目搭建

JAVA实战项目-瑞吉外卖项目_第4张图片

添加依赖:

 
        org.springframework.boot
        spring-boot-starter-parent
        2.4.5
         
    
    com.ka
    reggie
    1.0-SNAPSHOT
    
        1.8
    
    

        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-starter-web
            compile
        

        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.2
        

        
            org.projectlombok
            lombok
            1.18.20
        

        
            com.alibaba
            fastjson
            1.2.76
        

        
            commons-lang
            commons-lang
            2.6
        

        
            mysql
            mysql-connector-java
            runtime
        

        
            com.alibaba
            druid-spring-boot-starter
            1.1.23
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                2.4.5
            
        
    

application.yml

server:
  port: 8080 #端口号
spring:
  application:
    #应用名称
    name: reggie_take_out
  datasource: #数据源
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/reggie?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
      username: root #账号
      password: 1234 #密码
mybatis-plus:
  configuration:
    #address_book->AddressBook
    #在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config:
    db-config:
      id-type: ASSIGN_ID

配置application启动类

@Slf4j
@SpringBootApplication
public class ReggieApplication {
    public static void main(String[] args) {
        SpringApplication.run(ReggieApplication.class,args);
        log.info("项目启动成功!!!");
    }
}

前端的静态资源放在resource目录

JAVA实战项目-瑞吉外卖项目_第5张图片

设置静态资源映射

@Slf4j
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开始静态资源映射");
        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
        registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");
    }
}

启动项目,访问http://localhost:8080/backend/index.html

JAVA实战项目-瑞吉外卖项目_第6张图片

后台登录功能开发


登录页面展示(http://localhost:8080/backend/page/login/login.html)

JAVA实战项目-瑞吉外卖项目_第7张图片

查看登录请求信息

JAVA实战项目-瑞吉外卖项目_第8张图片

代码开发

  • 创建实体类Employee,和employee表进行映射

@Data
public class Employee implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;

    private String username;

    private String name;

    private String password;

    private String phone;

    private String sex;

    private String idNumber;//身份证号码

    private Integer status;

    private LocalDateTime createTime;

    private LocalDateTime updateTime;

    @TableField(fill = FieldFill.INSERT)
    private Long createUser;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Long updateUser;

}
  • 创建Controller,Service,Mapper

@Mapper
public interface EmployeeMapper extends BaseMapper {
}
public interface EmployeeService extends IService {
}
@Service
public class EmployeeServiceImpl extends ServiceImpl implements EmployeeService {
}
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeServiceImpl employeeService;
}
  • 导入返回结果类R

  • 此类是一个通用结果类,服务端响应的所有结果最终都会包装成此种类型返回给前端页面

@Data
public class R {

    private Integer code; //编码:1成功,0和其它数字为失败

    private String msg; //错误信息

    private T data; //数据

    private Map map = new HashMap(); //动态数据

    public static  R success(T object) {
        R r = new R();
        r.data = object;
        r.code = 1;
        return r;
    }

    public static  R error(String msg) {
        R r = new R();
        r.msg = msg;
        r.code = 0;
        return r;
    }

    public R add(String key, Object value) {
        this.map.put(key, value);
        return this;
    }

}

在Controller中创建登录方法

JAVA实战项目-瑞吉外卖项目_第9张图片
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeServiceImpl employeeService;

    @PostMapping("/login")
    public R login(HttpServletRequest request, @RequestBody Employee employee) {

        //1、将页面提交的密码进行md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2、根据页面提交的用户名来查数据库
        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);

        //3、如果没有查询到则返回失败结果
        if (emp == null) {
            return R.error("登录失败");
        }

        //4、比对密码,如果不一致则返回失败结果
        if (!emp.getPassword().equals(password)) {
            return R.error("密码错误");
        }

        //5、查看员工状态,如果已禁用状态,则返回员工已禁用结果
        if (emp.getStatus() == 0) {
            return R.error("账号已禁用");
        }

        //6、登录成功,将用户id存入Session并返回成功结果
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }
}

后台退出功能开发


代码实现

在Controller中创建退出方法

  //员工退出
    @PostMapping("/logout")
    public R logout(HttpServletRequest request){
        //清理Session中保存的当前员工登录的id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

你可能感兴趣的:(java)