添加依赖:

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

结果:

Redis实现存取数据+数据存取的更多相关文章

  1. sqlite3的图片的(二进制数据)存取操作

    sqlite3的图片的(二进制数据)存取操作   前言 上篇介绍了sqlite3的一些常用插入操作方法和注意事项,在实际项目中遇到了图片缓存的问题,由于服务器不是很稳定,且受到外界环境的干扰(例如断电 ...

  2. 使用ADO实现BLOB数据的存取 -- ADO开发实践之二

    使用ADO实现BLOB数据的存取 -- ADO开发实践之二 http://www.360doc.com/content/11/0113/16/4780948_86256633.shtml 一.前言 在 ...

  3. 用string存取二进制数据

    STL的string很强大,用起来也感觉很舒服,这段时间在代码中涉及到了用string存取二进制数据的问题,这里记录一下,以供以后参考. 首先提一下STL中string的参考资料:http://www ...

  4. (三)初识NumPy(数据CSV文件存取和多维数据的存取)

    本章主要介绍的是数据的CSV文件存取和多维数据的存取. 一.数据的CSV文件存取 1.CSV的写文件: np.savetxt(frame, array, fmt='%.18e', delimiter= ...

  5. [转]在nodejs使用Redis缓存和查询数据及Session持久化(Express)

    本文转自:https://blog.csdn.net/wellway/article/details/76176760 在之前的这篇文章 在ExpressJS(NodeJS)中设置二级域名跨域共享Co ...

  6. redis实现mysql的数据缓存

    环境设定base2 172.25.78.12 nginx+phpbase3 172.25.78.13 redis端base4 172.25.78.14 mysql端# 1.在base2(nginx+p ...

  7. Redis学习总结(1)——数据持久化

    以前研究Redis的时候,很多东西都不太明白,理解得也不太深,现在有时间重新拾起来看看,将一些心得记录下来,希望和大家一起探讨. 一.简介 Redis是一个单线程高可用的Key-Value存储系统,和 ...

  8. Redis各种数据结构性能数据对比和性能优化实践

    很对不起大家,又是一篇乱序的文章,但是满满的干货,来源于实践,相信大家会有所收获.里面穿插一些感悟和生活故事,可以忽略不看.不过听大家普遍的反馈说这是其中最喜欢看的部分,好吧,就当学习之后轻松一下. ...

  9. redis增删查改数据Util

    目录 (1)需要导入的包 (2)redis配置文件 (3)RedisUtil类 (1)需要导入的包 <dependency> <groupId>org.springframew ...

  10. redis哈希缓存数据表

    redis哈希缓存数据表 REDIS HASH可以用来缓存数据表的数据,以后可以从REDIS内存数据库中读取数据. 从内存中取数,无疑是很快的. var FRedis: IRedisClient; F ...

随机推荐

  1. HashMap源码分析-jdk1.7

    注:转载请注明出处!!!!!!!这里咱们看的是JDK1.7版本的HashMap 学习HashMap前先知道熟悉运算符合 *左移 << :就是该数对应二进制码整体左移,左边超出的部分舍弃,右 ...

  2. Windows系统里Oracle 11g R2 Client(64bit)的下载与安装

    环境: windows10系统(64位) 最好先安装jre或jdk(此软件用来打开oracle自带的可视化操作界面,不装也没关系:可以安装plsql,或者直接用命令行操作) Oracle 11g 是仅 ...

  3. ubuntu 配置 tftp 服务器

    一. 安装 tftp 1.1. 安装 tftp 所需的软件. a. 安装 tftp-hpa,tftpd-hpa,前者是客户端,后者是服务程序, 在终端下输入 sudo apt-get install ...

  4. Python 入门之格式化输出

    Python 入门之格式化输出 1.格式化 (1)%为占位 (2)%s --- 站字符串的位置(数字.字符串都能够进行填充) name = input('请输入姓名:') age = input('请 ...

  5. linux:输入/输出、重定向、管道

    输入.输出: 程序的默认输入设备,叫标准输入. stdin    键盘   0 程序的默认输出设备,叫标准输出. stdout    监视器    1 程序的默认错误输出设备,叫标准错误输出.stde ...

  6. linux安装mysql8(完整图文笔记)

    基本命令 安装 : yum install mysql-community-server 启动 : service mysqld start/restart 停止 : service mysqld s ...

  7. 【问题解决方案】Linux中命令useradd与adduser的区别

    参考链接: useradd与adduser的区别 useradd与adduser:创建新的用户 CentOs: useradd与adduser是没有区别的 都是在创建用户,在home下自动创建目录,没 ...

  8. web.xml文件头声明各个版本参考

    Servlet 3.1 Java EE 7 XML schema, namespace is http://xmlns.jcp.org/xml/ns/javaee/ <web-app xmlns ...

  9. 内存分析工具MAT(Memory Analyzer Tool)从安装到使用

    一.安装 首先,你得有一个Eclipse(因为MAT是Eclipse的插件) 然后,你要在Eclipse上安装MAT,步骤如下: 1.点击Help,Install New Soft,就出现了以下Ins ...

  10. 牛客练习赛14 B 区间的连续段 (倍增)

    链接:https://ac.nowcoder.com/acm/contest/82/B来源:牛客网 区间的连续段 时间限制:C/C++ 7秒,其他语言14秒 空间限制:C/C++ 262144K,其他 ...