您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

SpringBoot 之 Dao层模拟数据库操作

bubuko 2022/1/25 19:53:15 java 字数 2254 阅读 969 来源 http://www.bubuko.com/infolist-5-1.html

单表操作: 关联表操作: ...

单表操作:

@Repository
public class DepartmentDao {
	private static Map<Integer, Department> departments = null;

	private static Integer initId = 5;

	static {
		departments = new HashMap<Integer, Department>();
		departments.put(1, new Department(1, "行政部"));
		departments.put(2, new Department(2, "财务部"));
		departments.put(3, new Department(3, "运营部"));
		departments.put(4, new Department(4, "开发部"));
	}

	public void addDepartment(Department department) {
		department.setId(initId);
		departments.put(initId, department);
		initId++;
	}

	public Collection<Department> getDepartments() {
		return departments.values();
	}

	public Department getDepartment(Integer id) {
		return departments.get(id);
	}

	public void delDepartment(Integer id) {
		departments.remove(id);
	}
}

关联表操作:

@Repository
public class EmployeeDao {
	private static Map<Integer, Employee> employees = null;

	@Autowired
	private DepartmentDao departmentDao;

	static {
		employees = new HashMap<Integer, Employee>();
		employees.put(1, new Employee(1, "张三", 3));
		employees.put(2, new Employee(2, "李四", 1));
		employees.put(3, new Employee(3, "王五", 4));
		employees.put(4, new Employee(4, "赵六", 2));
	}

	private static Integer initId = 5;

	public void addEmployee(Employee employee) {
		employee.setId(initId);
		employees.put(initId, employee);
		initId++;
	}

	public Collection<Employee> getEmployees() {
		return employees.values();
	}

	public Employee getEmployee(Integer id) {
		Employee employee = employees.get(id);
		Integer departmentId = employee.getDepartmentId();
		Department department = departmentDao.getDepartment(departmentId);
		employee.setDepartment(department);
		return employee;
	}

	public void delEmployee(Integer id) {
		employees.remove(id);
	}
}

SpringBoot 之 Dao层模拟数据库操作

原文:https://www.cnblogs.com/danhuang/p/12821701.html


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶