ssm整合redis

ssm整合redisTemplete

1允趟,引入坐標(biāo)(版本一定要對的上屎慢,不然就可能出現(xiàn)錯誤)

    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.4.2</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>1.3.0.RELEASE</version>
    </dependency>

2亚享,redis.properties

redis.host=192.168.240.135
redis.port=6379
redis.password=***
redis.timeout=100000
redis.maxTotal=300
redis.maxIdle=200
redis.maxWait=10000
redis.testOnBorrow=true
redis.testOnReturn=true

# 默認緩存失效時間
defaultCacheExpireTime=3600

3咽块,springRedis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    <!--掃描redis配置文件-->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:redis.properties"/>
    <!--設(shè)置連接池-->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}"/>
        <property name="maxTotal" value="${redis.maxTotal}" />
        <property name="maxWaitMillis" value="${redis.maxWait}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
        <property name="testOnReturn" value="${redis.testOnReturn}" />
    </bean>
    <!--設(shè)置鏈接屬性-->
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:hostName="${redis.host}"
          p:port="${redis.port}"
          p:password="${redis.password}"
          p:pool-config-ref="poolConfig"
          p:timeout="100000"/>
    <!-- Jedis模板配置  -->
<!--    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">-->
<!--        <property name="connectionFactory"   ref="connectionFactory" />-->
<!--        -->
<!--    </bean>-->
    <bean id="redisTemplate"
          class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
        <!-- 如果不配置Serializer,那么存儲的時候只能使用String欺税,如果用對象類型存儲侈沪,那么會提示錯誤 can't cast to String!M碓洹亭罪!-->
        <property name="keySerializer">
            <!--對key的默認序列化器。默認值是StringSerializer-->
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <!--是對value的默認序列化器歼秽,默認值是取自DefaultSerializer的JdkSerializationRedisSerializer应役。-->
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
        <!--存儲Map時key需要的序列化配置-->
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <!--存儲Map時value需要的序列化配置-->
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
    </bean>

</beans>

3,引入到主applicationContext.xml

    <!--    redis -->
    <import resource="classpath:springRedis.xml"/>

4,測試

4.1 Controller 添加一段代碼

@Controller
@ResponseBody
@RequestMapping("/admin")
@CrossOrigin
public class AdminController {
    @Autowired
    AdminService adminService;
    @Autowired
    JWTUtils jwt;
    @Autowired
    RedisTemplate redisTemplate;
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    DataReturn login(@RequestBody Admin admin) {
        redisTemplate.opsForValue().append("teskinfly","handsome");//在這里測試
        if (!adminService.checkPwd(admin.getAName(), admin.getAPwd()))
            return new DataReturn(ReturnCode.FAIL);
        Admin byName = adminService.findByName(admin.getAName());
        String s = jwt.create(byName.getAId(), byName.getAName());
        return new DataReturn(ReturnCode.SUCCESS, s);
    }
}

4.2 用postman發(fā)送請求


image.png

4.3 查看redis,已經(jīng)存進去


image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末箩祥,一起剝皮案震驚了整個濱河市院崇,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌袍祖,老刑警劉巖底瓣,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異蕉陋,居然都是意外死亡濒持,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進店門寺滚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來柑营,“玉大人,你說我怎么就攤上這事村视」偬祝” “怎么了?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵蚁孔,是天一觀的道長奶赔。 經(jīng)常有香客問我,道長杠氢,這世上最難降的妖魔是什么站刑? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮鼻百,結(jié)果婚禮上绞旅,老公的妹妹穿的比我還像新娘。我一直安慰自己温艇,他們只是感情好因悲,可當(dāng)我...
    茶點故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著勺爱,像睡著了一般晃琳。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上琐鲁,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天卫旱,我揣著相機與錄音,去河邊找鬼围段。 笑死顾翼,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的蒜撮。 我是一名探鬼主播暴构,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼跪呈,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了取逾?” 一聲冷哼從身側(cè)響起耗绿,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎砾隅,沒想到半個月后误阻,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡晴埂,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年究反,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片儒洛。...
    茶點故事閱讀 39,977評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡精耐,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出琅锻,到底是詐尸還是另有隱情卦停,我是刑警寧澤,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布恼蓬,位于F島的核電站惊完,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏处硬。R本人自食惡果不足惜小槐,卻給世界環(huán)境...
    茶點故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望荷辕。 院中可真熱鬧凿跳,春花似錦、人聲如沸桐腌。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽案站。三九已至,卻和暖如春棘街,著一層夾襖步出監(jiān)牢的瞬間蟆盐,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工遭殉, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留石挂,地道東北人。 一個月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓险污,卻偏偏與公主長得像痹愚,于是被迫代替她去往敵國和親富岳。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,927評論 2 355