Redis实例

Maven的继承和聚合 =========================


<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>


<!-- JSP相关 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>




Maven垂直切分
ts-parent  -- 父工程,pom类型,统一管理公用jar依赖
ts-common  -- jar类型,通用的组件、工具类 
ts-manage  -- pom类型,后台管理系统,管理商品、分类等功能


Maven水平切分
ts-manage-pojo  -- jar类型,后台持久类
ts-manage-mapper -- jar类型,后台持久层接口
ts-manage-service -- jar类型,后台业务层
ts-manage-web -- war类型,后台的前台controller、jsp、映射文件 


依赖关系
ts-manage-mapper 依赖pojo
ts-manage-service 依赖pojo,依赖mapper,依赖common
ts-manage-web 依赖pojo,依赖mapper,依赖service,依赖common


利用依赖的传递性简化
pojo依赖common,mapper依赖pojo,service依赖mapper,web依赖service




Maven和Tomcat整合 =========================


<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8090</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>


http://localhost:8090/main/index.do




jedis引入 =========================


<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.0</version>
</dependency>


package jedis;


import java.util.ArrayList;
import java.util.List;


import org.junit.Test;


import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;


public class TestJedis {
@Test //完成单实例链接
public void jedis(){
//Jedis jedis = new Jedis("192.168.27.113", 6379);
Jedis jedis = new Jedis("192.168.163.7", 6379);
//jedis.auth("123456");
jedis.set("name", "tony3"); //调用redis命令set
System.out.println(jedis.get("name"));
jedis.close();
}

@Test
public void pool(){
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(200);

List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
JedisShardInfo info = new JedisShardInfo("192.168.163.7", 6379);
shards.add(info);

ShardedJedisPool pool = new ShardedJedisPool(config, shards);
ShardedJedis jedis = pool.getResource();

System.out.println(jedis.get("name"));
}
}








spring-redis.xml =========================


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">


<!-- 构建连接池配置信息 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大连接数 -->
<property name="maxTotal" value="200"/>
</bean>

<!-- 定义集群连接池 -->
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" destroy-method="close">
<!-- 第一个参数 -->
<constructor-arg index="0" ref="jedisPoolConfig"/>
<constructor-arg index="1">
<list>
<bean class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="192.168.163.7"/>
<constructor-arg type="int" index="1" value="6379"/>
</bean>
</list>
</constructor-arg>
</bean>


</beans>




DictServiceImpl ============================================================================


package cn.tedu.store.service;


import java.util.List;


import javax.annotation.Resource;


import org.springframework.stereotype.Service;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;


import cn.tedu.store.bean.dict.Area;
import cn.tedu.store.bean.dict.City;
import cn.tedu.store.bean.dict.Province;
import cn.tedu.store.mapper.DictMapper;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;


@Service("dictService")
public class DictServiceImpl implements DictService {

@Resource
private DictMapper dictMapper;
@Resource
private ShardedJedisPool shardedJedisPool;
private static final ObjectMapper MAPPER = new ObjectMapper();


public List<Province> getProvinceList() {
return dictMapper.getProvinceList();
}


public List<City> getCityList(String provinceCode) {
ShardedJedis jedis = shardedJedisPool.getResource();

String RedisKey = "TS_CITY_"+provinceCode;

if(jedis.exists(RedisKey)){
            Object obj = null;
try {
String json = jedis.get(RedisKey);
JsonNode jsonNode = MAPPER.readTree(json);
obj = MAPPER.readValue(jsonNode.traverse(),
MAPPER.getTypeFactory().constructCollectionType(List.class, City.class));
} catch (Exception e) {
e.printStackTrace();
}
return (List<City>) obj;
}else{
List<City> listCity = dictMapper.getCityList(provinceCode);
try {
String json = MAPPER.writeValueAsString(listCity);
jedis.set(RedisKey, json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return listCity;
}
}






}

猜你喜欢

转载自blog.csdn.net/linsa_pursuer/article/details/80326337
今日推荐