目录
- 学习初衷
- 准备
- 代码实现
- 结果
- 感谢阅读
学习初衷
- 用于满足自己的测试要求,不连接数据库,也不在意数据丢失
准备
- maven依赖
org.springframework.boot
spring-boot-starter-test
test
代码实现
- 内存数据库(InMemoryDatabase)
- 使用了单例模式,程序启动就创建
package com.kwan.springbootkwan.test.lykj;import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;import java.util.HashMap;
import java.util.Map;
import java.util.Set;public class InMemoryDatabase {private static InMemoryDatabase instance;public final Map<String, HashMap<String, String>> database;private InMemoryDatabase() {this.database = new HashMap<>();}public static synchronized InMemoryDatabase getInstance() {if (instance == null) {instance = new InMemoryDatabase();}return instance;}public void insert(String tableName, String primaryKey, String data) throws JsonProcessingException {if (!database.containsKey(tableName)) {database.put(tableName, new HashMap<String,String>());}database.get(tableName).put(primaryKey, data);}public Object query(String tableName, String primaryKey, Class<?> clazz) throws JsonProcessingException {Set<String> tableSet = database.keySet();if (!tableSet.contains(tableName)) {return null;}String jsonData = database.get(tableName).get(primaryKey);if (jsonData == null) {return null;}ObjectMapper mapper = new ObjectMapper();return mapper.readValue(jsonData, clazz);}public String delete(String tableName, String primaryKey) {return database.get(tableName).remove(primaryKey);}public void update(String tableName, String primaryKey, String updatedJsonData) {Map<String, String> table = database.get(tableName);if (table != null) {table.put(primaryKey, updatedJsonData);} else {throw new IllegalArgumentException("Table " + tableName + " does not exist.");}}
}
- 测试增删改查方法
package com.kwan.springbootkwan.test.lykj.memorydb;import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;import java.util.HashMap;
import java.util.Map;
import java.util.Set;public class InMemoryDatabase {private static InMemoryDatabase instance;public final Map<String, HashMap<String, String>> database;private InMemoryDatabase() {this.database = new HashMap<>();}public static synchronized InMemoryDatabase getInstance() {if (instance == null) {instance = new InMemoryDatabase();}return instance;}public void insert(String tableName, String primaryKey, String data) throws JsonProcessingException {if (!database.containsKey(tableName)) {database.put(tableName, new HashMap<String,String>());}database.get(tableName).put(primaryKey, data);}public Object query(String tableName, String primaryKey, Class<?> clazz) throws JsonProcessingException {Set<String> tableSet = database.keySet();if (!tableSet.contains(tableName)) {return null;}String jsonData = database.get(tableName).get(primaryKey);if (jsonData == null) {return null;}ObjectMapper mapper = new ObjectMapper();return mapper.readValue(jsonData, clazz);}public String delete(String tableName, String primaryKey) {return database.get(tableName).remove(primaryKey);}public void update(String tableName, String primaryKey, String updatedJsonData) {Map<String, String> table = database.get(tableName);if (table != null) {table.put(primaryKey, updatedJsonData);} else {throw new IllegalArgumentException("Table " + tableName + " does not exist.");}}
}
结果
感谢阅读
感谢您阅读 这篇文章,我们探索了自己本地模拟内存数据库增删改查
谢谢您的陪伴! 如果您有任何问题、建议或想要了解的特定主题,请随时在评论中告诉我们。我们期待与您共同探索java,共同提升我们的Java开发技能!