本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

没有数据库,如何模拟数据库操作的dao层?

发布于2021-05-29 20:46     阅读(1018)     评论(0)     点赞(23)     收藏(4)


1.两个实体类(包名为pojo)

部门表

package com.kuang.pojo;

// 部门表
public class Department {
    private Integer id;
    private String departmentName;

    public Department() {
    }

    public Department(Integer id, String departmentName) {
        this.id = id;
        this.departmentName = departmentName;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }
}

员工表

package com.kuang.pojo;

import java.util.Date;

//员工表
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();
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }
}

2.dao层(包名为dao)

部门dao

package com.kuang.dao;

import com.kuang.pojo.Department;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

//部门dao
@Repository
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);
    }
}

员工dao

package com.kuang.dao;

import com.kuang.pojo.Department;
import com.kuang.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;

//员工dao
@Repository
public class EmployeeDao {

    //模拟数据库中的数据
    private static Map<Integer, Employee> employees = null;

    //员工所属部门
    @Autowired
    private DepartmentDao departmentDao;

    static {
        employees  = new HashMap<Integer, Employee>();  //创建一个部门表

        employees.put(101, new Employee(1001, "AA","A861352772@qq.com",0,new Department(101,"教学部")));
        employees.put(102, new Employee(1002, "BB","B861352772@qq.com",1,new Department(102,"运营部")));
        employees.put(103, new Employee(1003, "CC","C861352772@qq.com",0,new Department(103,"市场部")));
        employees.put(104, new Employee(1004, "DD","D861352772@qq.com",1,new Department(104,"教研部")));
        employees.put(105, new Employee(1005, "EE","E861352772@qq.com",0,new Department(105,"后勤部")));
    }

    //主键自增
    private static Integer initId = 1006;

    //增加一个员工
    public void save(Employee employee){
        if(employee.getId() == null){
            employee.setId(initId++);
        }

	   //通过员工获得部门再获得id来设置部门
        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);
    }

    //删除员工通过ID
    public void delete(Integer id){
        employees.remove(id);
    }
}

3.Controller类

package com.kuang.controller;

import com.kuang.dao.DepartmentDao;
import com.kuang.dao.EmployeeDao;
import com.kuang.pojo.Department;
import com.kuang.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

@Controller
public class EmployeeController {

    @Autowired
    EmployeeDao employeeDao;
    @Autowired
    DepartmentDao departmentDao;

    //获取所有的员工
    @RequestMapping("/emps")
    public String list(Model model){
        Collection<Employee> employees = employeeDao.getAll();
        model.addAttribute("emps",employees);
        return "emp/list";
    }

    //去添加员工的页面
    @GetMapping("/emp")
    public String toAddPage(Model model){
        //查出所有部门的信息
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments",departments);
        return "emp/add";
    }

    //添加员工
    @PostMapping("/emp")
    public String addEmp(Employee employee){
        employeeDao.save(employee);
        return "redirect:/emps";
    }

    //去修改员工的页面
    @GetMapping("/emp/{id}")
    public String update(@PathVariable("id")Integer id,Model model){
        Employee employee = employeeDao.getEmployeeById(id);
        model.addAttribute("emp",employee);
        //查出所有部门的信息
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments",departments);
        return "emp/update";
    }

    //修改员工
    @PostMapping("/updateEmp")
    public String updateEmp(Employee employee){
        employeeDao.save(employee);
        return "redirect:/emps";
    }

    //删除员工
    @PostMapping("/delemp")
    public String deleteEmp(@PathVariable("id")Integer id){
        employeeDao.delete(id);
        return "redirect:/emps";
    }

}

4.项目页面存放目录(项目为springboot项目),页面可以自己定义来实现或者到我资源这里拿(免费的)

在这里插入图片描述

原文链接:https://blog.csdn.net/weixin_49092628/article/details/117325174



所属网站分类: 技术文章 > 博客

作者:helloworld

链接:http://www.javaheidong.com/blog/article/207325/fcc4cce741ce3f4d6c64/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

23 0
收藏该文
已收藏

评论内容:(最多支持255个字符)