环境:项目采用springMvc+spring4.25+hibernate5.08+Mysql
目的:将Redis作为缓存数据库,具体Redis的优势网上都有,就不在赘述了。
commons-pool2-2.5.0.jar redis配置线程池
springMVC.xml
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
springCore2.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/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/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
redis.properties
redis.hostName=127.0.0.1
redis.port=6379
redis.password=
redis.timeout=10000
redis.maxIdle=300
redis.maxTotal=1000
redis.maxWaitMillis=1000
redis.minEvictableIdleTimeMillis=300000
redis.numTestsPerEvictionRun=1024
redis.timeBetweenEvictionRunsMillis=30000
redis.testOnBorrow=true
redis.testWhileIdle=true
RedisCacheUser.java 实体类 对应Mysql字段
@Entity
@Table(name="rediscacheuser")
public class RedisCacheUser {
//用户ID
private String redisuserid;
//名字
private String redisusername;
//密码
private String redispassword;
//ID
private String redisddid;
//用户电话
private String redismobile;
public RedisCacheUser(){
}
@Id
public String getUserid() {
return redisuserid;
}
public void setUserid(String userid) {
this. redisuserid = userid;
}
@Column(name="USERNAME",nullable=false,length=32)
public String getUsername() {
return redisusername;
}
public void setUsername(String username) {
this. redisusername = username;
}
@Column(name="PASSWORD",nullable=false,length=64)
public String getPassword() {
return redispassword;
}
public void setPassword(String password) {
this. redispassword = password;
}
@Column(name="DDID",nullable=false,length=128)
public String getDdid() {
return redisddid;
}
public void setDdid(String ddid) {
this.redisddid = ddid;
}
@Column(name="MOBILE",length=32)
public String getMobile() {
return redismobile;
}
public void setMobile(String mobile) {
this.redismobile = mobile;
}
@Override
public String toString() {
return "RedisCacheUser [redisuserid=" + redisuserid + ", redisusername=" + redisusername + ", redispassword="
+ redispassword + ", redisddid=" + redisddid + ", redismobile=" + redismobile + ", getUserid()="
+ getUserid() + ", getUsername()=" + getUsername() + ", getPassword()=" + getPassword() + ", getDdid()="
+ getDdid() + ", getMobile()=" + getMobile() + ", getClass()=" + getClass() + ", hashCode()="
+ hashCode() + ", toString()=" + super.toString() + "]";
}
}
RedisCacheTestService.java BaseTxService 里面封装了hibernate操作 可以通过hibernateTemplate操作Mysql
@Service
public class RedisCacheTestService extends BaseTxService {
@CachePut(key="#redisCacheUser.getUserid()",value="RedisCacheUser")
public String addRedisCacheTestUser(RedisCacheUser redisCacheUser) {
this.hibernateTemplate.save(redisCacheUser);
return JSONObject.fromObject(redisCacheUser).toString();
}
@CachePut(key="#redisCacheUser.getUserid()",value="RedisCacheUser")
public RedisCacheUser addRedisCacheTestUser1(RedisCacheUser redisCacheUser) {
this.hibernateTemplate.save(redisCacheUser);
return redisCacheUser;
}
/**
* 根据userId查询对象 先从缓存 没有则再数据库
* @param userId
* @return
*/
@Cacheable(key="#userId",value="RedisCacheUser")
@SuppressWarnings("unchecked")
public String getRedisCacheUserByuserId(String userId) {
String string = getRedisCacheUserByuserIdDB(userId);
return string;
}
public String getRedisCacheUserByuserIdDB(String userId) {
List
RedisCacheUser redisCacheUser = new RedisCacheUser() ;
redisCacheUsers = (List
if(redisCacheUsers.size()>0) {
redisCacheUser = redisCacheUsers.get(0);
return JSONObject.fromObject(redisCacheUser).toString();
}
return null;
}
/**
* 根据userName模糊查询多个对象 先从缓存 没有则再数据库
* @param userId
* @return
*/
/*@Cacheable(key="#username",value="RedisCacheUser")
@SuppressWarnings("unchecked")*/
public String getRedisCacheUsers(String username) {
String string = getRedisCacheUsersDB(username);
return string;
}
public String getRedisCacheUsersDB(String username) {
List
RedisCacheUser redisCacheUser = new RedisCacheUser() ;
redisCacheUsers = (List
if(redisCacheUsers.size()>0) {
System.out.println("redisCacheUsers:"+redisCacheUsers);
return JSONObject.fromObject(redisCacheUsers).toString();
}
return null;
}
/**
* 根据userName模糊查询多个对象 先从缓存 没有则再数据库
* @param username
* @return List
*/
/*@Cacheable(key="#username",value="RedisCacheUser")
@SuppressWarnings("unchecked")*/
public List
List
redisCacheUsers = (List
return redisCacheUsers;
}
/**
* 根据userId删除对象
* @param userId
* @return
*/
@CacheEvict(key="#redisCacheUser.getUserid()",value="RedisCacheUser")
public Boolean deleteRedisCacheUserByuserId(RedisCacheUser redisCacheUser) {
this.hibernateTemplate.delete(redisCacheUser);
// TODO Auto-generated method stub
return true;
}
/**
* 清空所有缓存 只是清除了Redis中的RedisCacheUser 但要删除数据库的需自己实现
* @return
*/
@CacheEvict(value="RedisCacheUser",allEntries=true)
public void deleteRedisCacheUsers() {
// TODO Auto-generated method stub
}
}
RedisCacheTest.java 使用Junit4测试
/**