Redis實現存取數據+數據存取


添加依賴:

 

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

} }

 

結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM