详解详解SSH框架和框架和Redis的整合的整合
本篇文章主要介绍了SSH框架和Redis的整合,详细的介绍了Struts+Spring+Hibernate和Redis整合,有兴趣的可以了解一下。
一个已有的Struts+Spring+Hibernate项目,以前使用MySQL数据库,现在想把Redis也整合进去。
1. 相关相关Jar文件文件
下载并导入以下3个Jar文件:
commons-pool2-2.4.2.jar、jedis-2.3.1.jar、spring-data-redis-1.3.4.RELEASE.jar。
2. Redis配置文件
配置文件
在src文件夹下面新建一个redis.properties文件,设置连接Redis的一些属性。
redis.host=127.0.0.1
redis.port=6379
redis.default.db=1
redis.timeout=100000
redis.maxActive=300
redis.maxIdle=100
redis.maxWait=1000
redis.testOnBorrow=true
再新建一个redis.xml文件。
classpath:redis.properties
3. Redis类类
新建一个com.school.redisclient包,结构如下:
接口IRedisService:
public interface IRedisService
{
public void set(K key, V value, long expiredTime);
public V get(K key);
public Object getHash(K key, String name);
public void del(K key);
}
抽象类AbstractRedisService,主要是对RedisTemplate进行操作:
public abstract class AbstractRedisService implements IRedisService {
@Autowired
private RedisTemplate redisTemplate;
public RedisTemplate getRedisTemplate() {
return redisTemplate;
}
public void setRedisTemplate(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
@Override
public void set(final K key, final V value, final long expiredTime) {
BoundValueOperations valueOper = redisTemplate.boundValueOps(key);
if (expiredTime <= 0) {
valueOper.set(value);
} else {
valueOper.set(value, expiredTime, TimeUnit.MILLISECONDS);
}
}
@Override
public V get(final K key) {
BoundValueOperations valueOper = redisTemplate.boundValueOps(key);
return valueOper.get();
}
@Override
public Object getHash(K key, String name){
Object res = redisTemplate.boundHashOps(key).get(name);
return res;
}
@Override
public void del(K key) {
if (redisTemplate.hasKey(key)) {
redisTemplate.delete(key);
}
}
}
实现类RedisService:
@Service("redisService")
public class RedisService extends AbstractRedisService
{
}
工具类RedisTool:
public class RedisTool {
private static ApplicationContext factory;
private static RedisService redisService;
public static ApplicationContext getFactory(){
if (factory == null){
factory = new ClassPathXmlApplicationContext("classpath:redis.xml");
}
return factory;
}
public static RedisService getRedisService(){
if (redisService == null){
redisService = (RedisService) getFactory().getBean("redisService");
}
return redisService;
}
}
4. 查询功能的实现
查询功能的实现
新建一个Action:RClasQueryAction,返回Redis里面所有的课程数据。
@SuppressWarnings("serial")
public class RClasQueryAction extends ActionSupport {
RedisService rs = RedisTool.getRedisService();
List claslist = new ArrayList();
Clas c;
public String execute(){
if (rs != null){
System.out.println("RedisService : " + rs);
getAllClas();
}
ServletActionContext.getRequest().setAttribute("claslist", claslist);
return SUCCESS;
}
private void getAllClas(){
claslist = new ArrayList();
int num = Integer.parseInt(rs.get("clas:count"));
for (int i=0; ipublic class RClasAction extends ActionSupport {
@Autowired
private ClasService clasService;
RedisService rs = RedisTool.getRedisService();
List claslist = new ArrayList();
private Clas clas;
public Clas getClas() {
return clas;
}
public void setClas(Clas Clas) {
this.clas = Clas;
}
public String execute(){
saveClas(clas);
return SUCCESS;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private void saveClas(Clas c){
List ids = rs.getList("clas:id");
// clas:id
int num = ids.size();
int id = Integer.parseInt(ids.get(num-1)) + 1;
rs.rightPushList("clas:id", String.valueOf(id));
// clas:count
int count = Integer.parseInt(rs.get("clas:count"));
rs.set("clas:count", String.valueOf(count+1), -1);
// 增加
HashMap hashmap = new HashMap();
hashmap.put("ID", String.valueOf(id));
hashmap.put("NAME", clas.getName());
hashmap.put("COMMENT", clas.getComment());
rs.addHash("clas:" + id, hashmap);
// 同步到MySQL
clasService.saveClas(clas);
}
}
clas:id是一个List类型的Key-Value,记录了所有的课程ID,取出最后一个ID,再+1,作为增加的课程的ID,同时clas:count的值也要+1。使用addHash()方法向Redis添加了一个Hash类型的Key-Value(也
就是一门课程):
@SuppressWarnings({ "unchecked", "rawtypes" })
public synchronized void addHash(K key, HashMap map){
redisTemplate.opsForHash().putAll(key, map);
}
同时将该门课程增加到MySQL。
6. 删除功能的实现
删除功能的实现
新建一个Action:RClasDeleteAction,实现删除Redis的课程数据,并同步到MySQL。
package com.school.action;
import org.springframework.beans.factory.annotation.Autowired;
import com.opensymphony.xwork2.ActionSupport;
import com.school.redisclient.RedisService;
import com.school.redisclient.RedisTool;
import com.school.service.ClasService;
@SuppressWarnings("serial")
public class RClasDeleteAction extends ActionSupport {
@Autowired
private ClasService clasService;
RedisService rs = RedisTool.getRedisService()
private int id;
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
public String execute(){
deleteClas(id);
// 同步到MySQL
clasService.deleteClas(id);
return SUCCESS;
}
private void deleteClas(int id){
// 删除
rs.del("clas:" + id);
// clas:count
int count = Integer.parseInt(rs.get("clas:count"));
rs.set("clas:count", String.valueOf(count-1), -1);
// clas:id
rs.delListItem("clas:id", String.valueOf(id));
}
}
直接删除clas:id,再将clas:count的值-1,这两步比较简单,复杂的是从clas:id中删除该课程的ID,使用了delListItem()方法来实现:
@Override
public synchronized void delListItem(K key, V value){
redisTemplate.opsForList().remove(key, 1, value);
}
redisTemplate.opsForList().remove()方法类似于LREM命令。最后在MySQL中也删除相同的课程。
7. 修改功能的实现
修改功能的实现
新建一个Action:RClasUpdateAction,实现删除Redis的课程数据,并同步到MySQL。
package com.school.action;
import java.util.HashMap;
import org.springframework.beans.factory.annotation.Autowired;
import com.opensymphony.xwork2.ActionSupport;
import com.school.entity.Clas;
import com.school.redisclient.RedisService;
import com.school.redisclient.RedisTool;
import com.school.service.ClasService;
@SuppressWarnings("serial")
public class RClasUpdateAction extends ActionSupport{
@Autowired
private ClasService clasService;
RedisService rs = RedisTool.getRedisService();
private Clas clas;
public Clas getClas() {
return clas;
}
public void setClas(Clas clas) {
this.clas = clas;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public String execute(){
HashMap hashmap = new HashMap();
hashmap.put("ID", String.valueOf(clas.getId()));
hashmap.put("NAME", clas.getName());
hashmap.put("COMMENT", clas.getComment());
rs.putHash("clas:" + clas.getId(), hashmap);
// 同步到MySQL
clasService.updateClas(clas);
return SUCCESS;
}
}
使用了putHash()方法来更新:
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public synchronized void putHash(K key, HashMap map){
redisTemplate.boundHashOps(key).putAll(map);
}
同时在MySQL做相同的更新操作。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。