SpringBoot 数据库操作

单表操作:

@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);
}

}
深圳网站建设www.sz886.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值