首页 > 技术文章 > Springboot:员工管理之环境准备(十(1))

applesnt 2020-04-18 20:04 原文

1:静态资源

下载静态资源:https://files.cnblogs.com/files/applesnt/ztzy.zip

项目下载:https://files.cnblogs.com/files/applesnt/hellospringboot.zip

把css、js、img文件夹放在static目录下

把index.html、list.html、dashboard.html、404.html放在templates目录下

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!--父项目 springboot版本-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <!--项目坐标:gav-->
    <groupId>com.springboot</groupId>
    <artifactId>hellospringboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hellospringboot</name>
    <description>Demo project for Spring Boot</description>

    <!--jdk版本-->
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <!--环境依赖-->
    <dependencies>

        <!--web环境依赖,会自动装配tomcat、静态资源目录、templates目录-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--测试依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--热部署插件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!--@ConfigurationProperties注解防变红-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <!--thymeleaf模板支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!--lombok支持-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

    </dependencies>

    <!--maven打包插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3:application.yml文件初始配置

server:
  port: 80 #访问端口

spring:
  thymeleaf:
    cache: false #开发时关闭缓存,不然没法看到实时页面
    prefix: classpath:/templates/ #文件存放路径
    suffix: .html  #文件后缀
    encoding: utf-8
    mode: HTML5
    servlet:
      content-type: text/html

4:构建java Bean

com\springboot\vo\Department.java #部门
使用@Data注解 如要安装lombok插件

package com.springboot.vo;

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

/*部门*/
@Data  //set get toString
@NoArgsConstructor //无参构造器
@AllArgsConstructor //有参构造器
public class Department {

	/*部门id*/
	private Integer id;
	/*部门名称*/
	private String departmentName;

}

com\springboot\vo\Employee.java #员工

package com.springboot.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;

/*员工*/
@Data //set get toString
@AllArgsConstructor //有参构造器
@NoArgsConstructor  //无参构造器
public class Employee {

    /*员工id*/
	private Integer id;
	/*员工姓名*/
    private String lastName;
    /*邮箱*/
    private String email;
    /*性别 0:女  1:男*/
    private Integer gender;
    /*部门*/
    private Department department;
    /*生日*/
    private Date birth;
}

5:构建模拟数据

com\springboot\dao\DepartmentDao.java #构建部门数据

package com.springboot.dao;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import com.springboot.vo.Department;
import org.springframework.stereotype.Repository;

/*把部门dao注入spring容器*/
@Repository
public class DepartmentDao {

	private static Map<Integer, Department> departments = null;
	/*模拟部门表 数据库数据*/
	static{
		departments = new HashMap<Integer, Department>();
		
		departments.put(101, new Department(101, "D-AA"));
		departments.put(102, new Department(102, "D-BB"));
		departments.put(103, new Department(103, "D-CC"));
		departments.put(104, new Department(104, "D-DD"));
		departments.put(105, new Department(105, "D-EE"));
	}

	/*查询所有部门*/
	public Collection<Department> getDepartments(){
		return departments.values();
	}

	/*通过id查询部门*/
	public Department getDepartment(Integer id){
		return departments.get(id);
	}
	
}

com\springboot\dao\EmployeeDao.java #构建员工数据

package com.springboot.dao;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.springboot.vo.Department;
import com.springboot.vo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


@Repository
public class EmployeeDao {

	private static Map<Integer, Employee> employees = null;
	
	@Autowired
	private DepartmentDao departmentDao;
	/*模拟员工表 数据库中的数据*/
	static{
		employees = new HashMap<Integer, Employee>();

		employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1, new Department(101, "D-AA"),new Date()));
		employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1, new Department(102, "D-BB"),new Date()));
		employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0, new Department(103, "D-CC"),new Date()));
		employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0, new Department(104, "D-DD"),new Date()));
		employees.put(1005, new Employee(1005, "E-EE", "ee@163.com", 1, new Department(105, "D-EE"),new Date()));
	}
	
	private static Integer initId = 1006;

	/*添加员工*/
	public void save(Employee employee){
		if(employee.getId() == null){
			employee.setId(initId++);
		}
		
		employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
		employees.put(employee.getId(), employee);
	}

	/*查询所有员工*/
	public Collection<Employee> getAll(){
		return employees.values();
	}

	/*通过id查询员工*/
	public Employee get(Integer id){
		return employees.get(id);
	}

	/*删除员工*/
	public void delete(Integer id){
		employees.remove(id);
	}
}

6:目录结构

推荐阅读