4.0.0
com.lwl
spring_redis
war
0.0.1-SNAPSHOT
spring_redis Maven Webapp
http://maven.apache.org
UTF-8
4.1.0.RELEASE
org.springframework
spring-aop
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework.data
spring-data-redis
1.5.0.RELEASE
org.codehaus.jackson
jackson-mapper-asl
1.5.0
com.fasterxml.jackson.core
jackson-annotations
2.9.0
com.fasterxml.jackson.core
jackson-core
2.9.0
com.fasterxml.jackson.core
jackson-databind
2.9.0
org.mybatis
mybatis-spring
1.2.3
mysql
mysql-connector-java
5.1.25
org.mybatis
mybatis
3.3.0
com.alibaba
druid
0.2.23
redis.clients
jedis
2.6.2
org.springframework
spring-test
4.1.0.RELEASE
test
javax.servlet
javax.servlet-api
3.0.1
provided
javax.servlet
jstl
1.2
junit
junit
4.9
test
spring_redis
org.mortbay.jetty
jetty-maven-plugin
8.1.8.v20121106
/${project.build.finalName}
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
/${project.build.finalName}
spring-context.xml配置:
spring-mvc.xml配置:
application/json;charset=UTF-8
jdbc.properties配置:
#dataSource configure
connection.url=jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf8
connection.username=root
connection.password=
druid.initialSize=10
druid.minIdle=10
druid.maxActive=50
druid.maxWait=60000
druid.timeBetweenEvictionRunsMillis=60000
druid.minEvictableIdleTimeMillis=300000
druid.validationQuery=SELECT 'x'
druid.testWhileIdle=true
druid.testOnBorrow=false
druid.testOnReturn=false
druid.poolPreparedStatements=true
druid.maxPoolPreparedStatementPerConnectionSize=20
druid.filters=wall,stat
log4j.properties配置:
# Global logging configuration
log4j.rootLogger=WARN,stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c:%L - %m%n
# mybatis log
log4j.logger.com.ssm.dao.UserMapper=DEBUG
# lyz log
log4j.logger.com.ssm.controller=DEBUG
log4j.logger.com.ssm.service=DEBUG
redis.properties配置:
#redis 服务器地址
redis.host=127.0.0.1
#redis 端口号
redis.port=6379
#redis 登录密码
redis.pass=password
#reids 的数据库号
redis.dbIndex=0
redis.expiration=3000
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
web.xml配置:
Archetype Created Web Application
contextConfigLocation
classpath:application.xml
org.springframework.web.context.ContextLoaderListener
springfilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
springfilter
/*
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
true
SpringMVC
/
RedisCacheConfig.java:
@Configuration
@EnableCaching
public class RedisCacheConfig extends CachingConfigurerSupport {
protected final static Logger log = LoggerFactory.getLogger(RedisCacheConfig.class);
private volatile JedisConnectionFactory mJedisConnectionFactory;
private volatile RedisTemplate mRedisTemplate;
private volatile RedisCacheManager mRedisCacheManager;
public RedisCacheConfig() {
super();
}
public RedisCacheConfig(JedisConnectionFactory mJedisConnectionFactory, RedisTemplate mRedisTemplate, RedisCacheManager mRedisCacheManager) {
super();
this.mJedisConnectionFactory = mJedisConnectionFactory;
this.mRedisTemplate = mRedisTemplate;
this.mRedisCacheManager = mRedisCacheManager;
}
public JedisConnectionFactory redisConnectionFactory() {
return mJedisConnectionFactory;
}
public RedisTemplate redisTemplate(RedisConnectionFactory cf) {
return mRedisTemplate;
}
public CacheManager cacheManager(RedisTemplate, ?> redisTemplate) {
return mRedisCacheManager;
}
@Bean
public KeyGenerator keyGenerator() {
return new KeyGenerator() {//匿名内部类
@Override
public Object generate(Object target, Method method, Object... params) {
//规定 本类名+方法名+参数名 为key
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName()+"_");
sb.append(method.getName()+"_");
for (Object obj : params) {
sb.append(obj.toString()+",");
}
return sb.toString();
}
};
}
}
User.java:
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = -5244288298702801619L;
private int id;
private String userName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + "]";
}
}
UserMapper.java:
package com.lwl.spring_redis.mapper;
import java.util.List;
import com.lwl.spring_redis.vo.User;
public interface UserMapper {
public User selectByPrimaryKey(int userId);
public List selectAllUser();
}
UserMapper.xml:
id,userName
UserService.java:
public interface IUserService {
public List getAllUser();
}
UserServiceImpl.java:
@Service
public class UserServiceImpl implements IUserService {
@Resource
private UserMapper userDao;
@Cacheable("getAllUser")
@Override
public List getAllUser() {
return this.userDao.selectAllUser();
}
}
UserController.java:
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private IUserService userService;
/**
* 查询所有User
*
* @param request
* @param model
* @return
*/
@RequestMapping(value = "/showList", method = RequestMethod.GET)
public String showUsers(Model model) {
List userList = new ArrayList();
userList = userService.getAllUser();
model.addAttribute("userList", userList); // 填充数据到model
return "showUser";
}
}
showUser.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false" %>
Insert title here
${userList}
启动tomcat7:run,访问链接 /projectName/user/showUser,显示如下:
查看值:
完成架构。
当停掉mysql 服务时,net stop mysql。访问网页仍然能显示查询数据。