第一天任务如下:
- 建立基本架构
- 完成登录、退出功能
注意:本博客没有使用网上教程里的mybatis-plus,使用的是mybatis;数据库连接池也没有使用教程里的druid,使用的是spring自带的连接池
基本架构
- common包:存放的通用类R,用来给前端返回Json格式的数据
- config包:存放配置类,在今天的任务中主要解决静态资源路径映射问题
- controller包
- entity包:存放实体类
- mapper包
- service包
pom.xml
<dependencies>
<!--springboot启动--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
<!--springboot测试--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- <dependency>-->
<!-- <groupId>com.baomidou</groupId>-->
<!-- <artifactId>mybatis-plus-boot-starter</artifactId>-->
<!-- <version>3.4.2</version>-->
<!-- </dependency>--><!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.20</version></dependency><!--将数据转换成Json格式--><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>
<!--mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.33</version></dependency>
<!--数据库驱动--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!-- <dependency>-->
<!-- <groupId>com.alibaba</groupId>-->
<!-- <artifactId>druid-spring-boot-starter</artifactId>-->
<!-- <version>1.1.23</version>-->
<!-- </dependency>--><!--mybatis整合springboot--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.3.2</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId></dependency>
application.properties
配置文件中主要做了四件事:
- 配置数据源
- 开启驼峰命名映射
- 开启mybatis别名映射
- mybatis映射路径
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/reggie?serverTimezone=UTC&userUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Drivermybatis.configuration.map-underscore-to-camel-case=truemybatis.type-aliases-package=com.itheima.entitymybatis.mapper-locations=classpath:mybatis/mapper/*.xml
config——WebMvcConfig类
我将静态资源导入resource目录下的backend和front中,并没有放在static和templates中,所以idea会找不到这些静态资源,所以才有了上面配置类中解决静态资源路径映射问题
@Slf4j
//表明这个是配置类
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {//静态资源映射,用来解决resource目录下的资源路径问题@Overrideprotected void addResourceHandlers(ResourceHandlerRegistry registry) {log.info("开始静态资源映射");// 将backend目录下的所有文件映射到classpath路径下的backend上registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");}
}
entity——Employee类
@Data
@AllArgsConstructor
@NoArgsConstructor
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;}
mapper——EmployeeMapper接口
这个接口中用了两个注解:
@Mapper 表明这是个mapper类,没有这个注解时,我们需要自己手动配置mybatis的配置文件 @Repository 将这个接口注入spring的IOC容器中
@Mapper
@Repository
public interface EmployeeMapper{//查询后端管理员Employee selectEmployee(String username);}
EmployeeMapper.xml映射文件
这个映射文件放在resource/mybatis/mapper中
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.EmployeeMapper"><select id="selectEmployee" resultType="employee" parameterType="String">select * from employee where username = #{username}</select></mapper>
service——EmployeeService接口
public interface EmployeeService{//查询后端管理员Employee selectEmployee(String username);}
service——EmployeeServiceImpl实现类
做了两件事:
- 将EmployeeMapper自动注入到IOC中
- 重写接口方法
@Service
public class EmployeeServiceImpl implements EmployeeService {@Autowiredprivate EmployeeMapper employeeMapper;@Overridepublic Employee selectEmployee(String username) {return employeeMapper.selectEmployee(username);}}
common——R通用类
上面说了,在使用前后端分离的情况下,controller层通常不会返回视图路径(springboot会使用默认的视图解析器来处理此种情况),而是会返回Json格式的数据,这个通用类帮我们封装好了Json格式的数据(里面是静态方法,我们可以直接使用类名调用这些方法)
@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;}}
controller——EmployeeController类
这个类中完成了两个功能:
- 登录功能
- 退出功能
@RestController
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {@Autowiredprivate EmployeeServiceImpl employeeService;//登录功能@PostMapping("/login")//@RequestBody 主要用来接收前端传递给后端的json字符串中的数据//HttpServletRequest 用来获取sessionpublic R<Employee> login(@RequestBody Employee employee, HttpServletRequest httpServletRequest){/*** 1.将页面提交的密码进行md5加密* 2.根据页面提交的用户名来查询数据库* 3.如果没有查询到则返回登陆失败结果* 4.密码对比,如果不一致则返回登陆失败结果* 5.查看员工状态status,若是已经被禁用则返回员工已禁用结果* 6.登陆成功,将员工的id存入session,并返回登陆成功结果*///第一步://获取前端传来的Json格式的passwordString password = employee.getPassword();//进行md5加密,并返回给passwordpassword = DigestUtils.md5DigestAsHex(password.getBytes());//第二步:Employee selectEmployee = employeeService.selectEmployee(employee.getUsername());//第三步:if (selectEmployee==null){return R.error("登陆失败");}//第四步:if (!selectEmployee.getPassword().equals(password)){return R.error("登陆失败");}//第五步:if (selectEmployee.getStatus() == 0){return R.error("账号已禁用");}//第六步:HttpSession session = httpServletRequest.getSession();session.setAttribute("selectEmployee",selectEmployee.getId());return R.success(selectEmployee);}//退出功能@PostMapping("/logout")public R<String> logout(HttpServletRequest httpServletRequest){//清理SessionHttpSession session = httpServletRequest.getSession();session.removeAttribute("selectEmployee");return R.success("退出成功");}
}