网站开发的项目开发,wap浏览器手机版下载,做网站图标按钮素材,保定网站建设13.1 静态资源下载
https://download.csdn.net/download/no996yes885/88151513
13.2 静态资源位置 css样式文件放在static的css目录下#xff1b;static的img下放图片#xff1b;template目录下放其余的html文件。 13.3 创建两个实体类 导入依赖#xff1a;lombok
!…13.1 静态资源下载
https://download.csdn.net/download/no996yes885/88151513
13.2 静态资源位置 css样式文件放在static的css目录下static的img下放图片template目录下放其余的html文件。 13.3 创建两个实体类 导入依赖lombok
!--lombok--
dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId
/dependency 加入Data注解自动创建set、get等方法 加入AllArgsConstructor注解自动创建有参构造方法 加入NoArgsConstructor注解自动创建无参构造方法 Department
package jiang.com.springbootstudy.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;Data
AllArgsConstructor
NoArgsConstructor
public class Department {private Integer id;private String departmentName;
}Employee
package jiang.com.springbootstudy.pojo;
import lombok.Data;
import lombok.NoArgsConstructor;import java.util.Date;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(); // 一创建对象调用有参的话自动生成了日期属性}
}13.4 创建实体类对应的dao类 DepartmentDao 注意这里类中的加载顺序静态的属性和代码要先加载然后再加载其他的。使用静态代码块结合Map来模拟数据库数据库的删查使用的是remove、values、get方法。
package jiang.com.springbootstudy.dao;import jiang.com.springbootstudy.pojo.Department;
import lombok.Data;
import org.springframework.stereotype.Repository;import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
Data
Repository
public class DepartmentDao {/*模拟数据库中的数据*/private static MapInteger, Department departments null;static {departments new HashMapInteger, 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 CollectionDepartment getDepartments(){return departments.values();}// 通过id得到部门public Department getDepartmentById(Integer id){return departments.get(id);}
}EmployeeDao:
package jiang.com.springbootstudy.dao;import jiang.com.springbootstudy.pojo.Department;
import jiang.com.springbootstudy.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;Repository //自动注入需要的注解
public class EmployeeDao {private static MapInteger, Employee employees null;// 员工所属的部门// 放数据先加载静态的东西比如说静态的遍历或者静态的代码Autowired //自动注入private DepartmentDao departmentDao;static {employees new HashMapInteger, Employee();//前面只是定义了成员变量的类型并没有赋值null无法调用put方法所以要创建对象。employees.put(1001,new Employee(1001,AA,A24736743qq.com,0,new Department(101,教学部)));employees.put(1002,new Employee(1002,AA,B24736743qq.com,1,new Department(101,市场部)));employees.put(1003,new Employee(1003,AA,C24736743qq.com,0,new Department(101,教研部)));employees.put(1004,new Employee(1004,AA,D24736743qq.com,1,new Department(101,运营部)));employees.put(1005,new Employee(1005,AA,E24736743qq.com,0,new Department(101,后勤部)));}// 主键自增private static Integer initId 1006; // static和没有有什么区别// 增加员工public void save(Employee employee){if (employee.getId()null){employee.setId(initId);//自增变量的值也会改变}employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId())); //这句话可以忽略原理是调用了接口的getDepartmentById方法传入从员工的部门属性获取的部门id得到一个部门然后把部门属性赋值给了员工employees.put(employee.getId(),employee);}// 查询全部员工信息public CollectionEmployee getAll(){ //集合需要掌握一下return employees.values();}// 通过id查询员工public Employee getEmployeeById(Integer id){return employees.get(id);}// 删除员工public void delete(Integer id){employees.remove(id);}}