本项目参考狂神老师的springboot项目,项目具体讲解视频请参考:
https://www.bilibili.com/video/BV1PE411i7CV?t=5&p=20
第一步自然是先创建一个干净的springboot项目,如何创建前面的博客已经详细介绍~
1、导入依赖
写项目的第一步就是先检查项目的配置文件有没有问题,并导入我们需要的依赖
<dependencies>
<!--web依赖-->
<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>
</dependency>
<!--模板引擎依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
2、pojo实体类
Department类
//部门表
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
private Integer id;
private String departmentName;
}
Employee类
//员工表
@Data
@NoArgsConstructor
public class Employee {
private Integer id;
private String lastName;
private String email;
private Integer gender;//0:女;1:男
private Department department;
private Date birth;
public Employee(Integer id, String lastName, String email, Integer gender, Department department) {
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
this.department = department;
//默认创建日期,这样日期就不用自己创建了
this.birth = new Date();
}
}
3、Dao层
实体类编写完毕后,我们来编写对实体类进行操作的Dao层,由于我没有先行编写设计数据库,所以我们在Dao层中用java代码以伪数据的方式来模拟数据库的操作。
DepartmentDao
//部门Dao
@Repository//spring接管Dao
public class DepartmentDao {
//当我们没有数据库时,可以采用以下方式模拟数据库
private static Map<Integer, Department> departments = null;
static {
departments = new HashMap<Integer, Department>();//创建一个部门表
departments.put(101, new Department(101, "教学部"));
departments.put(102, new Department(102, "市场部"));
departments.put(103, new Department(103, "教研部"));
departments.put(104, new Department(104, "运营部"));
departments.put(105, new Department(105, "后勤部"));
}
//获得所有部门信息
public Collection<Department> getDepartments() {
return departments.values();
}
//通过id得到部门
public Department getDepartmentByID(Integer id) {
return departments.get(id);
}
}
EmployeeDao
//员工Dao
@Repository//spring接管Dao
public class EmployeeDao {
//当我们没有数据库时,可以采用以下方式模拟数据库
private static Map<Integer, Employee> employees = null;
//员工应当有所属的部门
@Autowired//令spring托管Dao之后需要加这个注解,否则会报空指针
private DepartmentDao departmentDao;
static {//静态代码块会优先加载
employees = new HashMap<Integer, Employee>();//创建一个部门表
employees.put(1001, new Employee(1001, "hsyA", "A1416555145@qq.com", 0, new Department(101, "教学部")));
employees.put(1002, new Employee(1002, "hsyB", "B1416555145@qq.com", 1, new Department(102, "市场部")));
employees.put(1003, new Employee(1003, "hsyC", "C1416555145@qq.com", 0, new Department(103, "教研部")));
employees.put(1004, new Employee(1004, "hsyD", "D1416555145@qq.com", 1, new Department(104, "运营部")));
employees.put(1005, new Employee(1005, "hsyE", "E1416555145@qq.com", 0, new Department(105, "后勤部")));
}
//增加一个员工(主键自增!!!)
private static Integer initID=1006;
public void add(Employee employee){
if (employee.getId()==null){
employee.setId(initID++);
}
employee.setDepartment(departmentDao.getDepartmentByID(employee.getDepartment().getId()));
employees.put(employee.getId(),employee);
}
//查询全部员工
public Collection<Employee> getAll(){
return employees.values();
}
//通过ID查询员工
public Employee getEmployeeByID(Integer id){
return employees.get(id);
}
//删除员工
public void delete(Integer id){
employees.remove(id);
}
}
4、导入静态资源
然后我们就可以把静态资源导入进项目,作为一个后端程序员,我们自然是不会花太多时间放在页面上,为了响应“不要重复造轮子”的号召,我们从bootstrap上找了一些个模板嘿嘿,大家也可以去找自己喜欢的,这些是我用的,非常普通,如果有想要的可以私聊我
注意!!!
1、css,js等放在static文件夹下
2、html 放在 templates文件夹下
最终的项目结构应当如下图: