20240114
跟着黑马学习瑞吉外卖项目
导入数据库
表含义
检查maven配置
jre
sdk 1.8
pom设置
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> <relativePath/> <!-- lookup parent from repository --> </parent>
依赖加入
pom.xml parent爆红解决:idea导入项目爆红问题记录以及解决_java_脚本之家
resource导入
server:
port: 8080 //启动时tomcat端口号
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: root
mybatis-plus:
configuration:
#address_book----->> AddressBook
#user_name----->>userName
#在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
id-type: ASSIGN_ID //主键生成策略
创建启动类
创建启动类:
package com.itheima.reggie;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//日志 添加后可以直接使用log变量 log.info() 表示输出日志
@Slf4j
@SpringBootApplication
public class RiggieApplication {
public static void main(String[] args) {
SpringApplication.run(RiggieApplication.class,args);
log.info("项目启动成功");
}
}
启动成功截图:
前端资料导入
需要设置静态资源映射,不然无法访问页面。告诉MVC框架,这两个目录下就是静态资源
重写addResourceHandlers方法
package com.itheima.reggie.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Slf4j
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
/**
* 设置静态资源映射
* @param registry
*/
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
// super.addResourceHandlers(registry);
log.info("开始进行静态资源映射...");
registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");
}
}
启动效果图
访问图
点击登录按钮发请求-请求到服务端 control ler-调service?? -service调mapper -mapper和数据库交互根据用户名密码进行查询
http://localhost:8080/backend/page/login/login.html
login.html????????????? 这三个需要后端给前端相应对应的json数据
创建包
创建EmployeeMapper
package com.itheima.reggie.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.reggie.entity.Employee;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface EmployeeMapper extends BaseMapper<Employee> {
}
创建EmployeeService
package com.itheima.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.itheima.reggie.entity.Employee;
public interface EmployeeService extends IService<Employee> {
}
创建EmployeeServiceImpl
package com.itheima.reggie.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.stereotype.Service;
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
}
创建EmployeeController
package com.itheima.reggie.controller;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
}
P9后台系统登录功能_代码开发(导入通用返回结果类)
r类
package com.itheima.reggie.common;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
/**
* 通用返回结果,服务的相应的数据最终都会封装成此对象
* @param <T>
*/
@Data
public class R<T> {
private Integer code; //编码:1成功,0和其它数字为失败
private String msg; //错误信息
private T data; //数据
private Map map = new HashMap(); //动态数据
/**
* 返回成功时返回R对象
* @param object
* @param <T>
* @return
*/
public static <T> R<T> success(T object) {
R<T> r = new R<T>();
r.data = object;
r.code = 1;
return r;
}
/**
* 失败时候反馈msg
* @param msg
* @param <T>
* @return
*/
public static <T> R<T> error(String msg) {
R r = new R();
r.msg = msg;
r.code = 0;
return r;
}
/**
* map操作动态数据,等用到时再说
* @param key
* @param value
* @return
*/
public R<T> add(String key, Object value) {
this.map.put(key, value);
return this;
}
}
编写登录接口
P11后台系统登录功能_代码开发(实现登陆处理逻辑)
package com.itheima.reggie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
/**
*员工登录
*/
@PostMapping("/login")
public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
/**
* 1、将页面提交的密码password进行md5加密处理
* 2、根据页面提交的用户名username查询数据库
* 3、如果没有查询到则返回登录失败结果
* 4、密码比对,如果不一致则返回登录失败结果
* 5、查看员工状态,如果为已禁用状态,则返回员工已禁用结果
* 6、登录成功,将员工id存入Session并返回登录成功结果
*/
// * 1、将页面提交的密码password进行md5加密处理
String password = employee.getPassword();
password = DigestUtils.md5DigestAsHex(password.getBytes());
// * 2、根据页面提交的用户名username查询数据库
LambdaQueryWrapper<Employee> 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);
}
}
一直报错500,数据库名称在pom.xml里修改也能没问题
后来发现是NAVCAT里创建的数据库前面有个空格导致的。
页面创建时 VUE的钩子函数created() 获取到userinfo
localstorege
点击退出按钮,前端页面发生ajx请求
后端编写controller接受这个请求
/**
* 员工退出
* @param request
* @return
*/
@PostMapping("logout")
public R<String> logout(HttpServletRequest request){
//清理Session中保存的当前登陆员工的id
request.getSession().removeAttribute("employee");
return R.success("退出成功");
}
el-menu菜单
遍历
切换页面的地址
持续更新分享-------------------------------------------------------------------------------------
day02链接: