1.redis_demo Maven
ItemMapper.xml:
mybatis-config.xml:
/p>
"http://mybatis.org/dtd/mybatis-3-config.dtd">
applicationContext-db.xml:
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
mappers=tk.mybatis.mapper.common.Mapper
applicationContext-redis.xml:
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
applicationContext-tx.xml:
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
applicationContext-mvc.xml:
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
application.properties:
#mysql connector
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root
#redis cluster connector
redis.host1=192.168.1.186
redis.port1=6380
redis.host2=192.168.1.186
redis.port2=6381
redis.host3=192.168.1.186
redis.port3=6382
redis.host4=192.168.1.186
redis.port4=6383
redis.host5=192.168.1.186
redis.port5=6384
redis.host6=192.168.1.186
redis.port6=6385
#item key
ITEM_INFO_KEY=ITEM_INFO_KEY
applicationContext.xml:
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
web.xml:
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0">
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
CharacterEncodingFilter
/*
redis_demo
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc/applicationContext-mvc.xml
1
redis_demo
/
pom.xml:
4.0.0
com.redis
redis_demo
war
0.0.1-SNAPSHOT
redis_demo Maven Webapp
http://maven.apache.org
4.12
4.3.4.RELEASE
2.8.1
1.2.17
3.0.1
2.0
1.2
5.1.40
1.0.26
3.3.0
1.2.3
1.2.15
3.3.9
2.10.3
2.9.0
1.7.2.RELEASE
junit
junit
${junit.version}
test
org.springframework
spring-context
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-test
${spring.version}
test
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
com.fasterxml.jackson.core
jackson-core
${jackson.version}
com.fasterxml.jackson.core
jackson-databind
${jackson.version}
com.fasterxml.jackson.core
jackson-annotations
${jackson.version}
com.alibaba
fastjson
${fastjson.version}
javax.servlet
javax.servlet-api
${servlet-api.version}
provided
javax.servlet
jsp-api
${jsp-api.version}
provided
javax.servlet
jstl
${jstl.version}
runtime
mysql
mysql-connector-java
${mysql.version}
runtime
com.alibaba
druid
${druid.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
${mybatis.spring.version}
tk.mybatis
mapper
${mybatis.mapper.version}
net.sf.ehcache
ehcache
${ehcache.version}
redis.clients
jedis
${jedis.version}
org.springframework.data
spring-data-redis
${spring-data-redis.version}
redis_demo
RedisDemoController.java:
packagecom.redis.controller;importjava.util.HashMap;importjava.util.Map;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.RestController;importcom.redis.service.IRedisService;importcom.redis.service.RedisDemoService;
@RestController
@RequestMapping("/redis")public classRedisDemoController {
@AutowiredprivateRedisDemoService redisDemoService;
@AutowiredprivateIRedisService redisService;
@RequestMapping(value="/demo",method=RequestMethod.GET)publicString demo() {//Map resultMap = new HashMap();// //调用service//String result = redisDemoService.demoService();//if(result != null && "ok".equals(result.toLowerCase())) {//resultMap.put("status", 200);//} else {//resultMap.put("status", 500);//}//return JSONUtil.toJSONString(resultMap);
returnredisDemoService.findAllItems(redisService);
}
}
ItemMapper.java:
packagecom.redis.mapper;importcom.redis.model.Item;importtk.mybatis.mapper.common.Mapper;public interface ItemMapper extends Mapper{
}
Item.java:
packagecom.redis.model;importjava.util.Date;import javax.persistence.*;
@Table(name= "ego_item")public classItem {/*** 商品id,同时也是商品编号*/@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)privateLong id;/*** 商品标题*/
privateString title;/*** 商品卖点*/@Column(name= "sell_point")privateString sellPoint;/*** 商品价格,单位为:分*/
privateLong price;/*** 库存数量*/
privateInteger num;/*** 商品条形码*/
privateString barcode;/*** 商品图片*/
privateString image;/*** 所属类目,叶子类目*/
privateLong cid;/*** 商品状态,1-正常,2-下架,3-删除*/
privateByte status;/*** 创建时间*/
privateDate created;/*** 更新时间*/
privateDate updated;/*** 获取商品id,同时也是商品编号
*
*@returnid - 商品id,同时也是商品编号*/
publicLong getId() {returnid;
}/*** 设置商品id,同时也是商品编号
*
*@paramid 商品id,同时也是商品编号*/
public voidsetId(Long id) {this.id =id;
}/*** 获取商品标题
*
*@returntitle - 商品标题*/
publicString getTitle() {returntitle;
}/*** 设置商品标题
*
*@paramtitle 商品标题*/
public voidsetTitle(String title) {this.title = title == null ? null: title.trim();
}/*** 获取商品卖点
*
*@returnsell_point - 商品卖点*/
publicString getSellPoint() {returnsellPoint;
}/*** 设置商品卖点
*
*@paramsellPoint 商品卖点*/
public voidsetSellPoint(String sellPoint) {this.sellPoint = sellPoint == null ? null: sellPoint.trim();
}/*** 获取商品价格,单位为:分
*
*@returnprice - 商品价格,单位为:分*/
publicLong getPrice() {returnprice;
}/*** 设置商品价格,单位为:分
*
*@paramprice 商品价格,单位为:分*/
public voidsetPrice(Long price) {this.price =price;
}/*** 获取库存数量
*
*@returnnum - 库存数量*/
publicInteger getNum() {returnnum;
}/*** 设置库存数量
*
*@paramnum 库存数量*/
public voidsetNum(Integer num) {this.num =num;
}/*** 获取商品条形码
*
*@returnbarcode - 商品条形码*/
publicString getBarcode() {returnbarcode;
}/*** 设置商品条形码
*
*@parambarcode 商品条形码*/
public voidsetBarcode(String barcode) {this.barcode = barcode == null ? null: barcode.trim();
}/*** 获取商品图片
*
*@returnimage - 商品图片*/
publicString getImage() {returnimage;
}/*** 设置商品图片
*
*@paramimage 商品图片*/
public voidsetImage(String image) {this.image = image == null ? null: image.trim();
}/*** 获取所属类目,叶子类目
*
*@returncid - 所属类目,叶子类目*/
publicLong getCid() {returncid;
}/*** 设置所属类目,叶子类目
*
*@paramcid 所属类目,叶子类目*/
public voidsetCid(Long cid) {this.cid =cid;
}/*** 获取商品状态,1-正常,2-下架,3-删除
*
*@returnstatus - 商品状态,1-正常,2-下架,3-删除*/
publicByte getStatus() {returnstatus;
}/*** 设置商品状态,1-正常,2-下架,3-删除
*
*@paramstatus 商品状态,1-正常,2-下架,3-删除*/
public voidsetStatus(Byte status) {this.status =status;
}/*** 获取创建时间
*
*@returncreated - 创建时间*/
publicDate getCreated() {returncreated;
}/*** 设置创建时间
*
*@paramcreated 创建时间*/
public voidsetCreated(Date created) {this.created =created;
}/*** 获取更新时间
*
*@returnupdated - 更新时间*/
publicDate getUpdated() {returnupdated;
}/*** 设置更新时间
*
*@paramupdated 更新时间*/
public voidsetUpdated(Date updated) {this.updated =updated;
}
}
RedisServiceImpl.java:
packagecom.redis.service.impl;importcom.redis.service.IRedisService;importredis.clients.jedis.JedisCluster;public class RedisServiceImpl implementsIRedisService {privateJedisCluster jedisCluster;public voidsetJedisCluster(JedisCluster jedisCluster) {this.jedisCluster =jedisCluster;
}
@OverridepublicString set(String key, String value) {returnjedisCluster.set(key, value);
}
@OverridepublicString get(String key) {returnjedisCluster.get(key);
}
@OverridepublicLong hset(String key, String field, String value) {return null;
}
@OverridepublicString hget(String key, String field) {return null;
}
@OverridepublicLong del(String... keys) {return null;
}
@OverridepublicLong hdel(String key, String... fields) {return null;
}
@OverridepublicBoolean exists(String key) {return null;
}
@Overridepublic Long expire(String key, intseconds) {return null;
}
@OverridepublicLong ttl(String key) {return null;
}
@OverridepublicLong incr(String key) {return null;
}
}
IRedisService.java:
packagecom.redis.service;public interfaceIRedisService {/*** 设置指定 key 的值
*
*@paramkey
*@paramvalue
*@return
*/String set(String key, String value);/*** 获取指定 key 的值
*
*@paramkey
*@return
*/String get(String key);/*** 设置存储在哈希表中指定字段的值
*
*@paramkey
*@paramfield
*@paramvalue
*@return
*/Long hset(String key, String field, String value);/*** 获取存储在哈希表中指定字段的值
*
*@paramkey
*@paramfield
*@return
*/String hget(String key, String field);/*** 删除 key和对应的值
*
*@paramkeys
*@return
*/Long del(String... keys);/*** 删除key的哈希表字段
*
*@paramkey
*@paramfields
*@return
*/Long hdel(String key, String... fields);/*** 检查给定 key 是否存在
*
*@paramkey
*@return
*/Boolean exists(String key);/*** 为给定 key 设置过期时间
*
*@paramkey
*@paramseconds
*@return
*/Long expire(String key,intseconds);/*** 以秒为单位返回 key 的剩余过期时间
*
*@paramkey
*@return
*/Long ttl(String key);/*** 自增
*@paramkey
*@return
*/Long incr(String key);
}
RedisDemoService.java:
packagecom.redis.service;importjava.util.HashMap;importjava.util.Map;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.stereotype.Service;importcom.redis.mapper.ItemMapper;importcom.redis.utils.JSONUtil;
@Servicepublic classRedisDemoService {
@Value("${ITEM_INFO_KEY}")privateString itemInfoKey;
@AutowiredprivateItemMapper itemMapper;//@Autowired//private IRedisService redisService;
publicString demoService() {
String itemJson=JSONUtil.toJSONString(itemMapper.selectAll());
String result= null;if(itemJson.isEmpty()) {returnresult;
}//result = redisService.set(itemInfoKey, itemJson);//如果存储生成,返回OK
returnresult;
}//findAllItems这个方法需要向前端项目传递商品数据
publicString findAllItems(IRedisService redisService) {//如果缓存中已经有itemInfoKey的数据了,直接就从缓存中取,不再查询数据库,而且也不需要存入redis
Map resultMap = new HashMap();
String itemJsons= "";//从redis数据库中查询数据
itemJsons =redisService.get(itemInfoKey);//如果redis中没有该数据
if(itemJsons.isEmpty()) {//从mysql中查询出需要的数据
itemJsons =JSONUtil.toJSONString(itemMapper.selectAll());//存入redis集群中
String result =redisService.set(itemInfoKey, itemJsons);if("ok".equals(result.toLowerCase())) {
resultMap.put(200, itemJsons);
}else{
resultMap.put(500, null);
}
}else{
resultMap.put(200, itemJsons);
}returnJSONUtil.toJSONString(resultMap);
}
}
JSONUtil.java:
packagecom.redis.utils;importjava.util.List;importcom.fasterxml.jackson.core.JsonProcessingException;importcom.fasterxml.jackson.databind.JavaType;importcom.fasterxml.jackson.databind.ObjectMapper;public classJSONUtil {//定义jackson对象
private static final ObjectMapper mapper = newObjectMapper();/*** 将对象转换成json字符串
*@paramdata
*@return
*/
public staticString toJSONString(Object data) {try{
String string=mapper.writeValueAsString(data);returnstring;
}catch(JsonProcessingException e) {
e.printStackTrace();
}return null;
}/*** 将json结果集转化为对象
*@paramjsonData
*@parambeanType
*@return
*/
public static T parseObject(String jsonData, ClassbeanType) {try{
T t=mapper.readValue(jsonData, beanType);returnt;
}catch(Exception e) {
e.printStackTrace();
}return null;
}/*** 将json数据转换成list
*@paramjsonData
*@parambeanType
*@return
*/
public static List parseArray(String jsonData, ClassbeanType) {
JavaType javaType= mapper.getTypeFactory().constructParametricType(List.class, beanType);try{
List list =mapper.readValue(jsonData, javaType);returnlist;
}catch(Exception e) {
e.printStackTrace();
}return null;
}
}
2.实体类mapper生成器(generator):
generatorConfig.xml:
/p>
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
jdbc.properties:
#\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root
mapper.plugin=tk.mybatis.mapper.generator.MapperPlugin
mapper.Mapper=tk.mybatis.mapper.common.Mapper
pom.xml:
4.0.0
ego-generator
war
0.0.1-SNAPSHOT
ego-generator Maven Webapp
http://maven.apache.org
ego-generator
org.mybatis
mybatis
3.4.4
org.mybatis.generator
mybatis-generator-core
1.3.5
compile
true
tk.mybatis
mapper
3.4.3
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
${basedir}/src/main/resources/generator/generatorConfig.xml
true
true
mysql
mysql-connector-java
5.1.40
tk.mybatis
mapper
3.3.9
右键pom.xml文件,Run as: mybatis-generator.generate,生成对应实体类和mapper