根据CREATE TABLE `tb_dic` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增id', `name` vCREATEarchar(255) DEFAULT NULL COMMENT '数据项名称', `other_name1` varchar(255) DEFAULT NULL COMMENT '数据项别名1', `other_name2` varchar(255) DEFAULT NULL COMMENT '数据项别名2', `detail` varchar(255) DEFAULT NULL COMMENT '详细描述', `type_id` int(11) DEFAULT NULL COMMENT '类别id', `order` varchar(255) DEFAULT NULL COMMENT '排序', `valid` int(11) DEFAULT NULL COMMENT '是否有效', PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='字典表'; 生成字典缓存
时间: 2023-11-21 10:10:33 浏览: 43
生成字典缓存可以通过以下步骤实现:
1. 定义一个Java类来表示字典项,例如:
```
public class DictionaryItem {
private int id; // 自增id
private String name; // 数据项名称
private String otherName1; // 数据项别名1
private String otherName2; // 数据项别名2
private String detail; // 详细描述
private int typeId; // 类别id
private String order; // 排序
private int valid; // 是否有效
// Getters/Setters
}
```
2. 使用Java中的JDBC技术连接到MySQL数据库,并执行如下SQL语句查询字典表中的所有数据项:
```
SELECT id, name, other_name1, other_name2, detail, type_id, `order`, valid FROM tb_dic;
```
3. 将查询结果封装成DictionaryItem对象,并存储到一个Map中,其中DictionaryItem对象的id作为Map的key,DictionaryItem对象本身作为Map的value。可以使用如下代码实现:
```
Map<Integer, DictionaryItem> dictionaryCache = new HashMap<>();
while (resultSet.next()) {
DictionaryItem item = new DictionaryItem();
item.setId(resultSet.getInt("id"));
item.setName(resultSet.getString("name"));
item.setOtherName1(resultSet.getString("other_name1"));
item.setOtherName2(resultSet.getString("other_name2"));
item.setDetail(resultSet.getString("detail"));
item.setTypeId(resultSet.getInt("type_id"));
item.setOrder(resultSet.getString("order"));
item.setValid(resultSet.getInt("valid"));
dictionaryCache.put(item.getId(), item);
}
```
4. 可以将上述代码封装成一个静态方法,每次需要使用字典缓存时直接调用该方法即可。可以使用ConcurrentHashMap等线程安全的数据结构来实现并发访问的问题。例如:
```
public class DictionaryCache {
private static Map<Integer, DictionaryItem> dictionaryCache = new ConcurrentHashMap<>();
static {
init();
}
public static void init() {
// 连接到数据库并查询字典表数据,将结果存储到dictionaryCache中
}
public static DictionaryItem getItemById(int id) {
return dictionaryCache.get(id);
}
// 其他操作字典缓存的方法
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)