添加依赖:

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

结果:

最新文章

  1. Swift基础--可选绑定和守护绑定
  2. 初学java之菜单条,菜单,菜单项的设置
  3. linux+apache+mod_Jk+tomcat实现tomcat集群
  4. asp.net利用ajax和jquery-ui实现进度条
  5. js时间戳转为日期格式
  6. Netbeans7.4下搭建struts2.3.16
  7. js 记忆函数
  8. sqlite性能简单測试
  9. linux查看文件及文件夹的大小
  10. Git与码云(Git@OSC)入门-如何在实验室和宿舍同步你的代码(2)
  11. 序列化之对象,字符串,byte数组,XML之间的转换(一)
  12. 数据库【mysql】之pymysql
  13. Webpack + vue 搭建
  14. MariaDB基于GTID主从复制及多主复制
  15. 【Windows】cmd条件判断
  16. JMeter 生成CSV文件中文变乱码的问题
  17. AI的新增功能(定义图案)(描边渐变)(图像描摹)5.1
  18. 感谢大家对《Cocos2d-JS开发之旅》的支持
  19. win10拖拽的问题
  20. KVM虚拟机IO处理过程(二) ----QEMU/KVM I/O 处理过程

热门文章

  1. [Python3] 003 变量类型概述 &amp; 数字类型详叙
  2. Linux常用命令基础
  3. java8--- (Function、Predicate、Consumer) 通用函数式接口
  4. Java关于继承中的内存分配
  5. 剑指offer-二叉搜索树与双向链表-python
  6. JCTF 2014 小菜两碟
  7. python学习笔记(4)
  8. linux MySQL 初始化数据库
  9. 将数据转为tfrecord格式
  10. Codeforces 959 树构造 暴力求最小字典序互质序列