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);
}
}
SpringBoot 之 Dao层模拟数据库操作的更多相关文章
- iBatis——自动生成DAO层接口提供操作函数(详解)
iBatis——自动生成DAO层接口提供操作函数(详解) 在使用iBatis进行持久层管理时,发现在使用DAO层的updateByPrimaryKey.updateByPrimaryKeySelect ...
- Spring框架针对dao层的jdbcTemplate操作crud之update修改数据库操作
使用jdbcTemplate 原理是把加载驱动Class.forName("com.mysql.jdbc.Driver"); 和连接数据库Connection conn=Drive ...
- Spring框架针对dao层的jdbcTemplate操作crud之add添加数据库操作
使用jdbcTemplate 原理是把加载驱动Class.forName("com.mysql.jdbc.Driver"); 和连接数据库Connection conn=Drive ...
- springboot 注册dao层 service 层
可以使用三种注解来引入DAO层的接口到spring容器中.1.@Mapper,写在每一个DAO层接口上,如下: 2.@MapperScan和@ComponentScan两者之一.前者的意义是将指定包中 ...
- Spring框架针对dao层的jdbcTemplate操作crud之query查询数据操作
查询目标是完成3个功能: (1)查询表,返回某一个值.例如查询表中记录的条数,返回一个int类型数据 (2)查询表,返回结果为某一个对象. (3)查询表,返回结果为某一个泛型的list集合. 一.查询 ...
- Spring框架针对dao层的jdbcTemplate操作crud之delete删除数据库操作 Spring相关Jar包下载
首先,找齐Spring框架中IoC功能.aop功能.JdbcTemplate功能所需的jar包,当前13个Jar包 1.Spring压缩包中的四个核心JAR包,实现IoC控制反转的根据xml配置文件或 ...
- Spring框架针对dao层的jdbcTemplate操作之jdbc数据库连接原始操作方法 所需安装包下载
crud指数据库或者持久层的基本操作,包括 增加(Create).读取查询(Retrieve 取回).更新(Update)和删除(Delete) Spring不仅对JDBC进行了封装,也对Hibern ...
- node14---分层结构数据库操作
/**回调函数(函数作为参数): 0. 外层函数调用的地方,一定是外层函数体先执行,回调函数和普通函数地址一样,然后看函数体规定回调函数怎么执行. 1. 异步时候使用回调函数, 无论是否异步,回调函数 ...
- Spring框架针对dao层的jdbcTemplate操作crud之query查询数据操作 —— 查询表,返回结果为对象的list集合
用JdbcTemplate的方法完成, 查询数据库表,把用户表sw_user所有数据以List<User>集合返回 在JdbcTemplateDemo类中增加查询返回所有对象集合的方法qu ...
随机推荐
- RAC中常见的高级用法-bind方法
RAC操作思想: Hook(钩子)思想 RAC核心方法:bind bind方法 假设想监听文本框的内容,并且在每次输出结果的时候,都在文本框的内容拼接一段文字" ...
- MySQL(3):SELECT语法
一,数据库语句 数据库数据是通过DML语句管理数据库数据,包括: INSERT (添加数据语句) UPDATE (更新数据语句) DELETE (删除数据语句) 1:INSERT (添加数据语句) I ...
- redis入门到精通系列(七):redis高级数据类型详解(BitMaps,HyperLogLog,GEO)
高级数据类型和五种基本数据类型不同,并非新的数据结构.高级数据类型往往是用来解决一些业务场景. (一)BitMaps (1.1) BitMaps概述 在应用场景中,有一些数据只有两个属性,比如是否是学 ...
- Spring Boot下使用拦截器
Spring Boot对于原来在配置文件配置的内容,现在全部体现在一个类中,该类需要继承自WebMvcConfigurationSupport类,并使用@Configuration进行注解,表示该类为 ...
- 【Linux】【Shell】【text】文本处理工具
文本查看及处理工具:wc, cut, sort, uniq, diff, patch wc:word count wc [OPTION]... [FILE]... -l: lines -w:words ...
- XML名命空间
XML的名命空间就类似于java的包,命名空间定义:xmlns:***="URI",默认命名空间定义:xmlns="URI" 引号中的URl内容用来唯一标识命名 ...
- 索引以及Mysql中的索引
一.什么是索引 索引是表的目录,会保存在额外的文件中,针对表中的指定列建立,专门用于帮助用户快速查询数据的一种数据结构.类似于字典中的目录,查找字典内容时可以根据目录查找到数据的存放位置,然后直接获取 ...
- 🔥🔥🔥Flutter 字节跳动穿山甲广告插件发布 - FlutterAds
前言 Flutter 已成为目前最流行的跨平台框架之一,在近期的几个大版本的发布中都提到了 Flutter 版本 Google 广告插件 [google_mobile_ads] .对于"出海 ...
- Python模块和函数
目录 一.基础 二.特殊函数 一.基础 #导入模块 import xxx #调用 xxx.dd() from xxx import xx as dd #导入某个函数,as给函数加别名,调用xx() ...
- LuoguB2147 求 f(x,n) 题解
Content 求给定 \(x,n\),求 \(f(x,n)=\sqrt{n+\sqrt{(n-1)+\sqrt{(n-2)+\sqrt{\dots+2+\sqrt{1+x}}}}}\) 的值. So ...