赞
踩
需求分析-设计-编码-测试-上线运维
软件开发角色分工
软件环境:开发环境、测试环境、生产环境
Navicat中新建数据库,运行sql,一共建好11张表:
1.新建maven项目,检查maven仓库配置,检查JDK版本
2.导入pom
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.zxn</groupId> <artifactId>reggieTakeout</artifactId> <version>1.0-SNAPSHOT</version> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <scope>compile</scope> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.76</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.23</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.4.5</version> </plugin> </plugins> </build> </project>
3.导入application.yml配置文件
server: port: 8080 spring: application: name: reggieTakeout 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: #在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射 map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: db-config: id-type: ASSIGN_ID
package com.zxn.reggie;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@Slf4j
@SpringBootApplication
public class ReggieApplication {
public static void main(String[] args) {
SpringApplication.run(ReggieApplication.class,args);
log.info("项目启动成功......");
}
}
默认情况下,只能访问static或者templet目录下的资源,为了实现访问非默认目录下的资源,需要编写配置类,来设置静态资源映射
package com.zxn.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) { log.info("开始进行静态资源映射..."); registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/"); registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/"); } }
需求分析:
数据模型:
后端需要给前端传的关键参数:
@Data public class Employee { private Long id; private String name; private String username; 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; private static final long serialVersionUID = 1L; }
其中service层下应该有impl实现包
以下部分都是按照MybatisPlus的规范来按步骤进行的
@Mapper注解
继承****BaseMapper
@Mapper
public interface EmployeeMapper extends BaseMapper<Employee> {
}
继承****IService
public interface EmployeeService extends IService<Employee> {
}
@Service注解
继承****ServiceImpl<EmployeeMapper, Employee>,实现****EmployeeService接口
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
}
因为登录时页面发送的请求是http://localhost:8080/employee/login,最外面的就是**@RequestMapping(“/employee”)**
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
}
@Data public class R<T> { private Integer code; //编码:1成功,0和其它数字为失败 private String msg; //错误信息 private T data; //数据 private Map map = new HashMap(); //动态数据 public static <T> R<T> success(T object) { R<T> r = new R<T>(); r.data = object; r.code = 1; return r; } public static <T> R<T> error(String msg) { R r = new R(); r.msg = msg; r.code = 0; return r; } public R<T> add(String key, Object value) { this.map.put(key, value); return this; } }
@PostMapping("/login") //传进来的json封装成Employee对象(需要检查属性名是否一致) public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){ //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());//eq等值查询 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); }
发现登录不上去,控制台报错:
java.sql.SQLNonTransientConnectionException: CLIENT_PLUGIN_AUTH is required
网上一查,首先是由于MySQL驱动过高,与当前MySQL版本不匹配,其次,MySQL6以上版本驱动名称为:com.mysql.cj.jdbc.Driver,而5之前为com.mysql.jdbc.Driver,而我自己用的是MySQL5。
于是,在pom中把MySQL驱动指定版本号为低版本:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
<scope>runtime</scope>
</dependency>
且在application.yml中修改此条:
driver-class-name: com.mysql.jdbc.Driver
再次测试,运行,登录成功。
@PostMapping("/logout")
public R<String> logout(HttpServletRequest request){
//清理session
request.getSession().removeAttribute("employee");
return R.success("退出成功");
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。