springboot使用HashMap的数据库

注解

@Data相当于类中有get与set方法

@NoArgsConstructor:相当于类中有无参构造器

@AllArgsConstructor:相当于类中有有参构造器

@Repository:使用@Repository注解可以确保DAO或者repositories提供异常转译,这个注解修饰的DAO或者repositories类会被ComponetScan发现并配置,同时也不需要为它们提供XML配置项。

@Autowired:自动导入依赖bean

1:Department表

@Data
@NoArgsConstructor
public class Department {
    private Integer id;
    private String departmentName;
    public Department(Integer id,String departmentName){
        this.id=id;
        this.departmentName=departmentName;
    }
}

2:Employee表

@Data
@AllArgsConstructor
@NoArgsConstructor//无参
public class Employee {
    private Integer id;
    private String lastName;
    private String email;
    private int sex;//0表示女,1表示男
    private Department department;
    private Date birth;

    public Employee(Integer id, String lastName, String email, int sex, Department department) {
        this.id = id;
        this.lastName = lastName;
        this.email = email;
        this.sex = sex;
        this.department = department;
        this.birth = new Date();
    }
}

3:EmployeeDao

Collection类

//员工Dao
@Repository
public class EmployeeDao {
    private static Map<Integer, Employee> employees=null;
    @Autowired
    private DepartmentDao departmentDao;
    static{
        employees=new HashMap<Integer,Employee>();
        employees.put(1001,new Employee
                (1001,"sheepbotany","A2464018412@qq.com",0,new Department(101,"教学部")));
        employees.put(1002,new Employee
                (1002,"sheep","B2464018412@qq.com",0,new Department(102,"市场部")));
        employees.put(1003,new Employee
                (1003,"botany","C2464018412@qq.com",0,new Department(103,"教研部")));
        employees.put(1004,new Employee
                (1004,"wood","D2464018412@qq.com",1,new Department(104,"运营部")));
        employees.put(1005,new Employee
                (1005,"woodwhale","E2464018412@qq.com",1,new Department(105,"后勤部")));
    }

    //主键自增
    private static Integer initId=1006;
    //增加一个员工
    public void save(Employee employee){
        if(employee.getId()==null){
            employee.setId(initId++);
        }
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        employees.put(employee.getId(),employee);
    }

    //查询所有的员工信息
    public Collection<Employee> getAll() {
        return employees.values();
    }
    //通过id查询员工
    public Employee getElementById(Integer id) {
        return employees.get(id);
    }
    //删除员工
    public void delete(Integer id){
        employees.remove(id);
    }
}

4:DepartmentDao

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

    }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要在前端使用 Element UI 的 el-select 组件来实现多选功能。el-select 组件的 multiple 属性可以设置为 true 来启用多选功能。 例如: ```html <el-select v-model="selectedValues" multiple> <el-option v-for="option in options" :key="option.value" :label="option.label" :value="option.value"></el-option> </el-select> ``` 其中,selectedValues 是一个绑定到 Vue 实例上的数组,用于存储用户选择的值。options 可以是从后端获取的数据,例如: ```javascript axios.get('/api/options').then(response => { this.options = response.data }) ``` 接下来,在后端使用 Spring Boot 来查询数据库并返回数据。可以使用 Spring Data JPA 来简化数据访问操作。 例如,定义一个实体类: ```java @Entity @Table(name = "my_table") public class MyEntity { @Id private Long id; private String name; // getter and setter methods } ``` 然后,定义一个 Spring Data JPA 的 repository 接口: ```java public interface MyEntityRepository extends JpaRepository<MyEntity, Long> { } ``` 最后,在控制器中使用 MyEntityRepository 来查询数据并返回给前端: ```java @RestController @RequestMapping("/api") public class MyController { @Autowired private MyEntityRepository repository; @GetMapping("/options") public List<Map<String, Object>> getOptions() { List<MyEntity> entities = repository.findAll(); return entities.stream().map(entity -> { Map<String, Object> option = new HashMap<>(); option.put("label", entity.getName()); option.put("value", entity.getId()); return option; }).collect(Collectors.toList()); } } ``` 这样,前端就可以使用 el-select 组件来实现从数据库取出的值进行多选了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值