B站学习springboot笔记

Posted 小智RE0

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了B站学习springboot笔记相关的知识,希望对你有一定的参考价值。

学习来源–>B站 传送门–> 【狂神说Java】SpringBoot最新教程IDEA版通俗易懂

这部分整理20p~28p的初始员工管理系统完成(伪造数据库,即并未创建真实存在的数据库);
视频中的springboot版本为2.2.0版本;
本次学习时springboot版本使用–>2.5.6版本;


1. 准备工作

首先新建项目;

导入静态资源

css,img,js,这几个文件在bootstrap官网应该可以找到的

本次的项目不使用数据库,而是伪造数据库;

先去pom.xml下导入thymeleaf以及lombok依赖

<!--使用thymeleaf-->
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
<!--lombok依赖-->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

若没有安装插件,记得去设置中plugins下载安装Lombok插件,安装后记得要重启IDEA

pojo包下创建Department类;作为部门类;

package com.xiaozhi.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author by CSDN@小智RE0
 * @date 2021-10-28 12:59
 * 部门实体类;
 */
//使用lombok注解;生成get,set方法,构造方法,toString()方法.....
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Department {
    //部门ID,名称;
    private Integer id;
    private String departmentName;
}

pojo包下创建Employee员工类;

package com.xiaozhi.pojo;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

/**
 * @author by CSDN@小智RE0
 * @date 2021-10-28 13:08
 * 员工实体类
 */
@Data
@NoArgsConstructor
public class Employee {
    //员工ID,名称,邮箱,性别;生日
    private Integer id;
    private String lastName;
    private String email;
    private Integer  sex;
    private Date birthday;
    //部门;
    private Department department;
    //构造方法;
    public Employee(Integer id, String lastName, String email, Integer sex, Department department) {
        this.id = id;
        this.lastName = lastName;
        this.email = email;
        this.sex = sex;
        this.department = department;
        this.birthday = new Date();
    }
}

dao包下创建DepartmentDao; 作为部门持久层;
这里开始先放两个处理;查所有部门以及根据部门ID查询部门;

package com.xiaozhi.dao;

import com.xiaozhi.pojo.Department;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
 * @author by CSDN@小智RE0
 * @date 2021-10-28 13:17
 * 部门类持久层;
 */
 @Repository
public class DepartmentDao {
    //模拟数据库;
    private static Map<Integer, Department> departmens=null;

    static {
        //创建部门表;
        departmens= new HashMap<>();
        //添加数据;
        departmens.put(100,new Department(100,"人事部"));
        departmens.put(101,new Department(101,"财务部"));
        departmens.put(102,new Department(102,"研发部"));
        departmens.put(103,new Department(103,"开发部"));
        departmens.put(104,new Department(104,"产品部"));
    }

    //对虚拟数据库进行操作;

    //获取所有的部门信息;
    public Collection<Department> findAllDepartment(){
        return departmens.values();
    }

    //根据ID获取部门;
    public Department findDepartById(Integer id){
        return departmens.get(id);
    }
}

dao包下创建EmployeeDao类; 作为员工持久层;

package com.xiaozhi.dao;

import com.xiaozhi.pojo.Department;
import com.xiaozhi.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author by CSDN@小智RE0
 * @date 2021-10-28 13:17
 * 员工持久层
 */
@Repository
public class EmployeeDao {
    //模拟数据库;
    private static Map<Integer, Employee> employees = null;

    //员工还有对应的部门表;
    @Autowired
    private DepartmentDao departmentDao;

    static {
        //创建数据表;
        employees = new HashMap<>();
        //存放数据;
        employees.put(0,new Employee(0,"xiaozhire0","1236547@qq.com",1,new Department(102,"研发部")));
        employees.put(1,new Employee(1,"阿杰","1236547@qq.com",0,new Department(103,"开发部")));
        employees.put(2,new Employee(2,"大白","asda@qq.com",1,new Department(100,"人事部")));
        employees.put(3,new Employee(3,"村头","1237@qq.com",0,new Department(103,"开发部")));
        employees.put(4,new Employee(4,"杨树","122547@qq.com",0,new Department(102,"研发部")));
        employees.put(5,new Employee(5,"柳树","1326547@qq.com",1,new Department(101,"财务部")));
        employees.put(6,new Employee(6,"苹果树","31236547@qq.com",0,new Department(103,"开发部")));
        employees.put(7,new Employee(7,"面包","112316547@qq.com",1,new Department(103,"开发部")));
        employees.put(8,new Employee(8,"炒面","1231547@qq.com",0,new Department(104,"产品部")));
        employees.put(9,new Employee(9,"大话","1231231547@qq.com",1,new Department(104,"产品部")));
    }

    //做到添加员工时,ID自动递增;
    private static Integer initId =10;

    //添加员工;
    public void  addEmployee(Employee employee){
        //先看这个员工是否有Id;
        if(employee.getId()==null){
            employee.setId(initId++);
        }
        //设置部门;
        employee.setDepartment(departmentDao.findDepartById(employee.getDepartment().getId()));

        //添加到模拟数据表中;
        employees.put(employee.getId(),employee);
    }

    //获取全部员工;
    public Collection<Employee> findAllEmployees(){
        return employees.values();
    }

    //根据员工Id查员工;
    public Employee findEmployeeByEId(Integer id){
        return employees.get(id);
    }

    //根据员工ID删除员工;
    public void deleteEmployee(Integer id){
        employees.remove(id);
    }
}

2.首页实现

controller包下创建WelcomeController类,控制首页的请求;

package com.xiaozhi.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author by CSDN@小智RE0
 * @date 2021-10-28 14:10
 * 首页请求处理;
 */
@Controller
public class WelcomeController {
    //到达首页;
    @RequestMapping({"/", "/index.html"})
    public String toIndex(){
        return "index";
    }
}

启动项目;访问http://localhost:8080/时;页面虽然加载,但是css样式表的内容却没内嵌导入;

实际上,这个首页跳转也可以写在自定义配置的config中;

WelcomeController类中刚才写的请求处理直接删除掉;

然后在config包下创建MyMvcConfig类,自定义一些mvc的配置;
进行视图跳转;

package com.xiaozhi.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author by CSDN@小智RE0
 * @date 2021-10-28 12:52
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    //添加视图跳转;
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //跳转首页
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
    }
}

application.properties下配置关闭模板引擎的缓存;改改虚拟路径;改改端口;

# 关闭模板template的缓存
spring.thymeleaf.cache=false

#更改项目的端口
server.port=5277

#更改项目的虚拟目录;
server.servlet.context-path=/xiaozhire0

OK,对templates模板下的首页index.html进行改造

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<meta name="description" content="">
		<meta name="author" content="">
		<title>Signin Template for Bootstrap</title>
		<!-- Bootstrap core CSS -->
		<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
		<!-- Custom styles for this template -->
		<link th:href="@{/css/signin.css}" rel="stylesheet">
	</head>

	<body class="text-center">
		<form class="form-signin" action="dashboard.html">
			<img class="mb-4" th:src="@{/img/bootstrap-solid.svg}" alt="" width="72" height="72">
			<h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
			<label class="sr-only">Username</label>
			<input type="text"  name="username" class="form-control" placeholder="Username" required="" autofocus="">
			<label class="sr-only">Password</label>
			<input type="password" name="password" class="form-control" placeholder="Password" required="">
			<div class="checkbox mb-3">
				<label>
          <input type="checkbox" value="remember-me"> Remember me
        </label>
			</div>
			<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
			<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
			<a class="btn btn-sm">中文</a>
			<a class="btn btn-sm">English</a>
		</form>

	</body>

</html>

清除浏览器缓存,

以上是关于B站学习springboot笔记的主要内容,如果未能解决你的问题,请参考以下文章

B站学习springboot笔记

B站首发的“SpringBoot+Vue全栈项目”有多牛X?

Python学习笔记---B站黑马程序员

B站学习笔记之初探JVM学习笔记

Swagger初学习笔记

B站学习Vue入门笔记