Redis实现存取数据+数据存取
2024-08-31 01:55:24
添加依赖:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.1.3.RELEASE</version>
</de
Mapper接口:
package com.nf147.sim.mapper; import com.nf147.sim.entity.News; import java.util.List; public interface NewsMapper {
List<News> query();
void add(News news);
}
映射文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nf147.sim.mapper.NewsMapper"> <resultMap id="BaseResultMap" type="com.nf147.sim.entity.News">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="title" jdbcType="VARCHAR" property="title" />
<result column="body" jdbcType="VARCHAR" property="body" />
</resultMap> <select id="query" resultType="com.nf147.sim.entity.News">
select id ,title,body from news
</select> <select id="selectAll" resultType="com.nf147.sim.entity.News">
select id ,title,body from news
</select> <insert id="add" keyProperty="id" useGeneratedKeys="true">
insert into news (title,body) values (#{title},#{body})
</insert> </mapper>
服务接口:
package com.nf147.sim.service; import com.nf147.sim.entity.News;
import redis.clients.jedis.Jedis; import java.io.IOException;
import java.util.List; public interface NewsService {
List<News> selectAll() throws IOException;void add (News news);
}
实现:
package com.nf147.sim.service.impl; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nf147.sim.entity.News;
import com.nf147.sim.mapper.NewsMapper;
import com.nf147.sim.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis; import java.io.IOException;
import java.util.List; @Service
public class NewsServiceImpl implements NewsService { @Autowired
private NewsMapper mapper; @Override
public List<News> selectAll() throws IOException { Jedis jedis =new Jedis();
String key = "listNews"; ObjectMapper on = new ObjectMapper(); //josn if (jedis.exists(key)){ //判断缓存有没有存在key
System.out.println("从缓存中取出数据...");
return on.readValue(jedis.get(key),new TypeReference<List<News>>(){}); //如果有就从缓存里面取数据
} //没有则从数据库去取
List<News> news = mapper.query();
jedis.set(key,on.writeValueAsString(news)); //然后设置键和数据
return news; //返回
} @Override
public void add(News news) { //每次添加时判短键是否存在,如果存在首先删除
Jedis jedis = new Jedis();
String key="listNews";
if(jedis.exists(key))
jedis.del(key);
mapper.add(news);
} }
测试:
package com.nf147.sim.service.impl; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nf147.sim.configuration.RootConfig;
import com.nf147.sim.entity.News;
import com.nf147.sim.mapper.NewsMapper;
import com.nf147.sim.service.NewsService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.Jedis; import java.io.*;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; @RunWith(SpringRunner.class)
@ContextConfiguration(classes = RootConfig.class)
public class NewsServiceImplTest { @Autowired
private NewsServiceImpl NewsServiceImpl; @Test
public void selectAll() throws IOException {
List<News> news = NewsServiceImpl.selectAll();
System.out.println(news);
} }
}
结果:
最新文章
- Swift基础--可选绑定和守护绑定
- 初学java之菜单条,菜单,菜单项的设置
- linux+apache+mod_Jk+tomcat实现tomcat集群
- asp.net利用ajax和jquery-ui实现进度条
- js时间戳转为日期格式
- Netbeans7.4下搭建struts2.3.16
- js 记忆函数
- sqlite性能简单測试
- linux查看文件及文件夹的大小
- Git与码云(Git@OSC)入门-如何在实验室和宿舍同步你的代码(2)
- 序列化之对象,字符串,byte数组,XML之间的转换(一)
- 数据库【mysql】之pymysql
- Webpack + vue 搭建
- MariaDB基于GTID主从复制及多主复制
- 【Windows】cmd条件判断
- JMeter 生成CSV文件中文变乱码的问题
- AI的新增功能(定义图案)(描边渐变)(图像描摹)5.1
- 感谢大家对《Cocos2d-JS开发之旅》的支持
- win10拖拽的问题
- KVM虚拟机IO处理过程(二) ----QEMU/KVM I/O 处理过程