Springboot:员工管理之环境准备(十(1))
2024-09-07 08:30:12
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:目录结构
最新文章
- svn强制加注释才能提交
- jQuery刷新包含的<;jsp:include>;页面
- JSP连接mysql数据库的重点
- 原生js与css3结合的电风扇
- Linux 下SVN服务器搭建
- 深入解析Java中volatile关键字的作用
- Ubuntu 12.04 Server OpenStack Havana多节点(OVS+GRE)安装
- 5分钟精通git教程
- [转]RadStudio DELPHI/C++ BUILDER Berlin 10.1 Update2安装破解教程
- switchysharp设置
- require() &#160;module.export &#160; &#160;Object.keys()
- Codeforces 1154F Shovels Shop
- 一、Sql Server 基础培训《进度1-建库建数据表(实际操作)》
- fiddler2抓包数据工具使用教程
- enc28J60 网页控制LED灯
- FLIR ONE PRO热成像仪
- nginx在centos 7中源码编译安装【添加grpc的支持】
- 20170831 php
- C# 6.0 的那些事
- [LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming
热门文章
- 2020年PHP 面试问题(三)
- 在Keras中可视化LSTM
- AC自动机(初步学习)
- POJ - 1276 二进制优化多重背包为01背包
- coding++ :MySQL 使用 SQL 语句查询数据库所有表注释已经表字段注释
- 关于C#三层架构中的“分页”功能
- 关于github报错:ssh: connect to host github.com port 22: Connection timed outfatal: Could not read from remote repository.Please make sure you have the correct access rightsand the repository exists.
- 当const放在function声明后
- Flask 入门(九)
- TP5快速入门