zoukankan      html  css  js  c++  java
  • SpringBoot 之 Dao层模拟数据库操作

    单表操作:

    # src/main/java/com/wu/dao/DepartmentDao .java
    
    @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 Collection<Department> index() {
    		return departments.values();
    	}
    
    	public void store(Department department) {
    		department.setId(initId);
    		departments.put(initId, department);
    		initId++;
    	}
    
    	public Department show(Integer id) {
    		return departments.get(id);
    	}
    
    	public void destroy(Integer id) {
    		departments.remove(id);
    	}
    }
    

    关联表操作:

    # src/main/java/com/wu/dao/EmployeeDao.java
    
    @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 Collection<Employee> index() {
    		return employees.values();
    	}
    
    	public void store(Employee employee) {
    		employee.setId(initId);
    		employees.put(initId, employee);
    		initId++;
    	}
    
    	public Employee show(Integer id) {
    		Employee employee = employees.get(id);
    		Integer departmentId = employee.getDepartmentId();
    		Department department = departmentDao.getDepartment(departmentId);
    		employee.setDepartment(department);
    		return employee;
    	}
    
    	public void destroy(Integer id) {
    		employees.remove(id);
    	}
    }
    
  • 相关阅读:
    搞笑的口误 [调剂一下生活 :D]
    Win 2003远程管理的实现
    asp.net 实现购物车(DataSet)详细代码[转]
    SQL Server:定时作业的设置方法
    URL Rewrite
    Asp.net 2.0 C#实现压缩/解压功能 [转=向作者‘肖相’无私共享精神致敬]
    网站的赢利模式 从只看排名流量中解脱出来[转]
    一些.net的工具[转]
    Expression孟岩
    sql作业,执行表间数据导入的实验
  • 原文地址:https://www.cnblogs.com/danhuang/p/12821701.html
Copyright © 2011-2022 走看看