springMvc redis 配置开发案例

springMvc Mybatis redis 开发详解案例


简介 : redis是一个缓存或者理解为数据库的一个key-value的存储机制,方便快捷,下面的如果看着多,不便于理解,青下载本案例,没有任何异常,便于学习

             初步理解,在redis官网 http://www.redis.cn/ 中所有的命令,都对应了redisSerivce类中的方法.即可是实现数据存储

               本实例下载链接 : http://download.youkuaiyun.com/detail/u014201191/9120387


连接信息properties

#redis\u7684\u670d\u52a1\u7aef\u53e3
redis.sentinel.1=120.25.254.164:26379
redis.sentinel.2=120.25.254.164:26380
redis.sentinel.3=120.25.254.164:26381

#\u94fe\u63a5\u6570\u636e\u5e93
redis.pass=
#\u8fde\u63a5\u6570\u636e\u5e93
redis.default.db=4
#\u5ba2\u6237\u7aef\u8d85\u65f6\u65f6\u95f4\u5355\u4f4d\u662f\u6beb\u79d2
redis.timeout=100000
#\u6700\u5927\u8fde\u63a5\u6570
redis.maxTotal=100
#\u6700\u5927\u7a7a\u95f2\u6570
redis.maxIdle=100
#\u6700\u5c0f\u7a7a\u95f2\u6570
redis.minIdle=10
#\u6700\u5927\u5efa\u7acb\u8fde\u63a5\u7b49\u5f85\u65f6\u95f4
redis.maxWaitMillis=1000
#\u591a\u957f\u65f6\u95f4\u68c0\u67e5\u4e00\u6b21\u8fde\u63a5\u6c60\u4e2d\u7a7a\u95f2\u7684\u8fde\u63a5
redis.pool.timeBetweenEvictionRunsMillis=30000
#\u7a7a\u95f2\u8fde\u63a5\u591a\u957f\u65f6\u95f4\u540e\u4f1a\u88ab\u6536\u56de
redis.pool.minEvictableIdleTimeMillis=30000
#\u6307\u660e\u662f\u5426\u5728\u4ece\u6c60\u4e2d\u53d6\u51fa\u8fde\u63a5\u524d\u8fdb\u884c\u68c0\u9a8c,\u5982\u679c\u68c0\u9a8c\u5931\u8d25,\u5219\u4ece\u6c60\u4e2d\u53bb\u9664\u8fde\u63a5\u5e76\u5c1d\u8bd5\u53d6\u51fa\u53e6\u4e00\u4e2a
redis.testOnBorrow=true
#\u5728\u8fdb\u884creturnObject\u5bf9\u8fd4\u56de\u7684connection\u8fdb\u884cvalidateObject\u6821\u9a8c
redis.testOnReturn=true
########reids\u7f16\u7801\u683c\u5f0f
redis.encode=utf-8

spring.xml 配置redis

<?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:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<!-- jedis pool配置 -->
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        	<!--最大连接数-->
		<property name="maxTotal" value="${redis.maxTotal}" />
	        <!--最大空闲连接数-->
		<property name="maxIdle" value="${redis.maxIdle}" />
	        <!--初始化连接数-->
		<property name="minIdle" value="${redis.minIdle}"/>
	        <!--最大等待时间-->
		<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
	        <!--对拿到的connection进行validateObject校验-->
		<property name="testOnBorrow" value="${redis.testOnBorrow}" />
	        <!--在进行returnObject对返回的connection进行validateObject校验-->
		<property name="testOnReturn" value="${redis.testOnReturn}" />
	        <!--定时对线程池中空闲的链接进行validateObject校验-->
        <property name="testWhileIdle" value="true" />
	</bean>

	<!-- <bean id="jedisPool" class="redis.clients.jedis.JedisSentinelPool">
		<constructor-arg index="0" value="mymaster" />
		<constructor-arg index="1">
			<set>
				<value>${redis.sentinel.1}</value>
				<value>${redis.sentinel.2}</value>
				<value>${redis.sentinel.3}</value>
			</set>
		</constructor-arg>
		<constructor-arg index="2" ref="jedisPoolConfig" />
		<constructor-arg index="3" type="int" value="10000" />
	</bean> -->
	
	<!-- 生产环境配置 -->
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="destroy">
        <constructor-arg index="0" ref="jedisPoolConfig"/>
        <constructor-arg index="1" value="120.25.254.164"/>
        <constructor-arg index="2" value="6379"/>
        <constructor-arg index="3" value="100000"/>
        <constructor-arg index="4">
            <null></null>
        </constructor-arg>
        <constructor-arg index="5" value="0"/>
    </bean>

	<bean id="redisService" class="com.zefun.web.service.RedisService" />
</beans>

redis实现类

package com.zefun.web.service;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;
import redis.clients.jedis.exceptions.JedisException;
import redis.clients.util.Pool;

/**
 * 封装Jedis操作
 * @author sfit0254
 * @date 2014-4-15
 */
@Service
public class RedisService {
    
    @Autowired 
    private Pool<Jedis> jedisPool;
    
    private Logger logger = Logger.getLogger(RedisService.class);
    
    private Jedis getJedis() throws JedisException {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
        }
        catch (JedisException e) {
            logger.error("getJedis error : jedisPool getResource.", e);
            if (jedis != null) {
                jedisPool.returnBrokenResource(jedis);
            }
            throw e;
        }
        return jedis;
    }

    protected void release(Jedis jedis, boolean isBroken) {
        if (jedis != null) {
            if (isBroken) {
                jedisPool.returnBrokenResource(jedis);
            }
            else {
                jedisPool.returnResource(jedis);
            }
        }
    }

    public void set(String key, String value) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.set(key, value);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public void set(Integer key, String value) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.set(String.valueOf(key), value);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public String get(String key) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.get(key);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
        return null;
    }

    public String getV(Object key) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.get(key.toString());
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
        return "\001";
    }

    public String get(Integer key) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.get(String.valueOf(key));
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
        return null;
    }

    public void del(String key) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.del(key);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public Boolean exists(String key) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.exists(key);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
        return false;
    }

    public void expire(String key, int seconds) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.expire(key, seconds);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public void expireV(String key, int seconds) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.expire(key, seconds);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public void expireAt(String key, long unixTime) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.expireAt(key, unixTime);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public void hset(String key, Object field, String value) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.hset(key, field.toString(), value);
        }
        catch (Exception e) {
            isBroken = true;
            logger.error("hset " + key + " " + field + " " + value, e);
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public void hset(String key, String field, Integer value) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.hset(key, field, String.valueOf(value));
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public void hset(Integer key, String field, Integer value) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.hset(String.valueOf(key), field, String.valueOf(value));
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public void hset(Integer key, String field, String value) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.hset(String.valueOf(key), field, value);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public String hget(String key, Object field) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.hget(key, field.toString());
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
        return null;
    }

    public String hgetV(String key, String field) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.hget(key, field);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
        return null;
    }

    public String hget(Integer key, String field) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.hget(String.valueOf(key), field);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
        return null;
    }

    public void hdel(String key, String field) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.hdel(key, field);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public Map<String, String> hgetAll(String key) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.hgetAll(key);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
        return null;
    }

    public void sadd(String key, Integer member) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.sadd(key, String.valueOf(member));
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public void srem(String key, Integer member) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.srem(key, String.valueOf(member));
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public void srem(String key, String member) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.srem(key, member);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public void zadd(String key, Double score, String member) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.zadd(key, score, member);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public void zadd(String key, Map<String, Double> scoreMembers) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.zadd(key, scoreMembers);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public void zrem(String key, String member) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.zrem(key, member);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public Set<String> zrevrange(String key, long start, long end) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.zrevrange(key, start, end);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
        return null;
    }

    public Set<String> zrevrangebyscore(String key, String max, String min, int offset, int count) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.zrevrangeByScore(key, max, min, offset, count);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
        return null;
    }

    public boolean sismember(String key, String member) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.sismember(key, member);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
        return false;
    }

    public boolean sismember(String key, Object member) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.sismember(key, member.toString());
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
        return false;
    }

    public void rename(String oldkey, String newkey) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.rename(oldkey, newkey);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
    }


    public long incrByNumber(String key, int number) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.incrBy(key, number);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
        return 0;
    }

    public Set<String> smembers(String key) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.smembers(key);
        }
        catch (Exception e) {
            isBroken = true;
            return null;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public String srandmember(String key) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.srandmember(key);
        }
        catch (Exception e) {
            isBroken = true;
            return null;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public void sadd(String key, String... members) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.sadd(key, members);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public long scard(String key) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.scard(key);
        }
        catch (Exception e) {
            isBroken = true;
            return -1;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public void setex(String key, String value, int seconds) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.setex(key, seconds, value);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public long zcard(String key) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.zcard(key);
        }
        catch (Exception e) {
            isBroken = true;
            return -1;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public Long zrank(String key, String member) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.zrank(key, member);
        }
        catch (Exception e) {
            isBroken = true;
            return -1L;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public double zincrby(String key, double score, String member) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.zincrby(key, score, member);
        }
        catch (Exception e) {
            isBroken = true;
            return -1d;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public Double zscore(String key, String member) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.zscore(key, member);
        }
        catch (Exception e) {
            isBroken = true;
            return -1d;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public Long zcount(String key, double min, double max) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.zcount(key, min, max);
        }
        catch (Exception e) {
            isBroken = true;
            return 0l;
        }
        finally {
            release(jedis, isBroken);
        }
    }
    
    public long zrevrank(String key, String member) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.zrevrank(key, member);
        }
        catch (Exception e) {
            isBroken = true;
            return -1l;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public boolean hset(String key, Map<String, String> hash) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.hmset(key, hash);
            return true;
        }
        catch (Exception e) {
            isBroken = true;
            return false;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public List<String> hmget(String key, String... fields) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.hmget(key, fields);
        }
        catch (Exception e) {
            isBroken = true;
            return null;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public void subscribe(JedisPubSub jedisPubSub, String... channels) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.subscribe(jedisPubSub, channels);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public void lpush(String key, String... elements) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.lpush(key, elements);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public Set<String> hkeys(String key) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.hkeys(key);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
        return null;
    }

    public long hincrBy(String key, String field, long value) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.hincrBy(key, field, value);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
        return 0l;
    }

    public String lpop(String key) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.lpop(key);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
        return null;
    }

    public void publish(String channel, String message) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            jedis.publish(channel, message);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
    }

    public boolean hsetnx(String key, String field, String value) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.hsetnx(key, field, value) == 1;
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
        return false;
    }

    public boolean setnx(String key, String value) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.setnx(key, value) == 1;
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
        return false;
    }

    public boolean hexists(String key, String field) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return jedis.hexists(key, field);
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
        return false;
    }

    public int hgetIntValue(String key, String field) {
        Jedis jedis = null;
        boolean isBroken = false;
        try {
            jedis = this.getJedis();
            return Integer.parseInt(jedis.hget(key, field));
        }
        catch (Exception e) {
            isBroken = true;
        }
        finally {
            release(jedis, isBroken);
        }
        return 0;
    }

}

单元测试类

package com.web.JunitSpringTest.test;

import java.io.IOException;

import org.junit.Assert;
import org.junit.Before;
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.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import com.zefun.web.controller.WechatController;
import com.zefun.web.service.RedisService;

/**
 * 单元测试类
* @author 高国藩
* @date 2015年8月12日 下午5:38:08
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
        "file:src/main/webapp/WEB-INF/dispatcher-servlet.xml",
        "classpath*:spring/spring-context.xml" })
@WebAppConfiguration
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public class BaseTest {
    @Autowired
    private WechatController wechatController;
    @Autowired
    private RedisService redisService;
    @Autowired
    private WebApplicationContext wac;
    public MockMvc mockMvc;
    @Before  
    public void setUp() {  
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();  
    }  
    @Test
    public void TestContoller() throws IOException {
        wechatController.test(null, null);
    }
    
    @Test  
    public void testView() throws Exception {  
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/wechat/test"))
                .andExpect(MockMvcResultMatchers.view().name("wechat/test"))  
                .andExpect(MockMvcResultMatchers.model().attributeExists("userName"))  
                .andDo(MockMvcResultHandlers.print())  
                .andReturn();  
        Assert.assertNotNull(result.getModelAndView().getModel().get("userName"));  
    }  
    
    @Test
    public void testRedis(){
        redisService.set(1, "你好");
        System.out.println(redisService.get(1));
    }
}


有图有真相



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值