为了避免麻烦,先在win10下的redis服务端进行整合,一切完成之后再进行Linux多机多节点整合;
参照了网上很多内容后开始整合,大概结构如下:
- 配置spring-mybatis,内容如下,可照搬:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
">
<!-- 注入mybatis -->
<!-- 加载数据源 ,写在redis的配置文件中,只能写一个
<bean id="propertuConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"
value="classpath:jdbc.properties"></property>
</bean>-->
<!-- 通过属性文件获取连接 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<property name="initialSize" value="${initialSize}"/>
<property name="maxActive" value="${maxActive}"/>
<property name="maxIdle" value="${maxIdle}"/>
<property name="minIdle" value="${minIdle}"></property>
<property name="maxWait" value="${maxWait}"></property>
</bean>
<!-- 加载xml文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatisconfig.xml"></property>
<!-- 自动扫描mapping.xml文件,这一句很重要 -->
<property name="mapperLocations" value="classpath:com/CaoPorn/dao/*.xml"></property>
</bean>
<!-- 指明所在包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.CaoPorn.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
</beans>
2.配置spring-redis:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
">
<context:property-placeholder location="classpath:*.properties"/>
<!-- 引入数据库配置文件 -->
<!-- redis数据源 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxActive}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<!--链接redis-->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"></property>
<property name="port" value="${redis.port}"></property>
<property name="password" value="${redis.password}"></property>
<property name="poolConfig" ref="poolConfig"></property>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="connectionFactory" >
<!--以下针对各种数据进行序列化方式的选择-->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<!--<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>-->
</bean>
</beans>
3、mybatisconfig:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<!-- 自动扫描Alias注解,在user.xml中可以直接使用 -->
<package name="com.CaoPorn.bean"/>
</typeAliases>
</configuration>
4、redis.properties:
redis.host=127.0.0.1
redis.port=6379
redis.password=199633
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
5、所需包:
6、接下来写测试文件:
dao层:
package com.CaoPorn.dao;
import java.util.List;
public interface RedisBaiseTakes<H,K,V> {
//增
public void add(K key,String value);
public void addObj(H objectKey,K key,V object);
//删
public void delete(K key);
public void delete(List<K> listKeys);
public void deletObj(H objecyKey,K key);
//改
public void update(K key,String value);
public void updateObj(H objectKey,K key,V object);
//查
public String get(K key);
public V getObj(H objectKey,K key);
}
bean层
package com.CaoPorn.bean;
import java.io.Serializable;
public class SeeUser implements Serializable{
private String id;
private String ip;//用户的id
private String seeTime;//用户访问的时间
private int seeCount;//用户访问的次数
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getSeeTime() {
return seeTime;
}
public void setSeeTime(String seeTime) {
this.seeTime = seeTime;
}
public int getSeeCount() {
return seeCount;
}
public void setSeeCount(int seeCount) {
this.seeCount = seeCount;
}
}
controller层:
package com.CaoPorn.controller;
import com.CaoPorn.bean.SeeUser;
import com.CaoPorn.dao.RedisBaiseTakes;
import com.CaoPorn.tools.StringFile;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping("test")
public class TestController {
@Resource(name="seeUserRedisTakes")
private RedisBaiseTakes seeUserRedisTakes;
@RequestMapping("/hello.do")
public ModelAndView hello(){
ModelAndView mv = new ModelAndView();
System.out.println("hello see");
seeUserRedisTakes.add("hello1","zxm");
mv.setViewName("../hello");
return mv;
}
@RequestMapping("/hello2.do")
public ModelAndView hello2(HttpServletRequest request){
ModelAndView mv = new ModelAndView();
SeeUser seeUser = new SeeUser();
seeUser.setId("1");
seeUser.setIp(StringFile.getIpAddr(request));
seeUser.setSeeTime(StringFile.getNoewTime("yyyy-MM-dd HH:mm:ss"));
seeUser.setSeeCount(1);
seeUserRedisTakes.addObj("seeUser",seeUser.getId(),seeUser);
request.setAttribute("user",seeUser);
mv.setViewName("../hello");
return mv;
}
@RequestMapping("/hello3.do")
public ModelAndView hello3(HttpServletRequest request){
ModelAndView mv = new ModelAndView();
SeeUser seeUser = (SeeUser) seeUserRedisTakes.getObj("seeUser","1");
if(seeUser!=null){
System.out.println(seeUser.getId()+"======="+seeUser.getIp()+"======"+seeUser.getSeeTime()+"======="+seeUser.getSeeCount());
}
mv.setViewName("hello");
return mv;
}
}
service层:
package com.CaoPorn.service;
import java.util.List;
import java.util.logging.Logger;
import javax.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import com.CaoPorn.bean.SeeUser;
import com.CaoPorn.bean.User;
import com.CaoPorn.dao.RedisBaiseTakes;
@Component("seeUserRedisTakes")
public class SeeUserRedisTakes implements RedisBaiseTakes<String,String,SeeUser>{
@Resource(name="redisTemplate")
private RedisTemplate redisTemplate;
private Logger logger = Logger.getLogger(String.valueOf(SeeUserRedisTakes.class));
public void add(String key, String value) {
if(redisTemplate==null){
logger.warning("redisTemplate 实例化失败");
return;
}else{
redisTemplate.opsForValue().set(key,value);
}
}
public void delete(String key) {
// TODO Auto-generated method stub
}
public void delete(List<String> listKeys) {
// TODO Auto-generated method stub
}
public void deletObj(String objecyKey, String key) {
// TODO Auto-generated method stub
}
public void update(String key, String value) {
// TODO Auto-generated method stub
}
public void updateObj(String objectKey, String key, User object) {
// TODO Auto-generated method stub
}
public String get(String key) {
String value = (String) redisTemplate.opsForValue().get(key);
return value;
}
public SeeUser getObj(String objectKey, String key) {
SeeUser seeUser = (SeeUser) redisTemplate.opsForHash().get(objectKey,key);
return seeUser;
}
public void addObj(String objectKey, String key, SeeUser object) {
if(redisTemplate==null){
logger.warning("redisTemplate 实例化失败");
return;
}else{
redisTemplate.opsForHash().put(objectKey,key,object);
}
}
public void updateObj(String objectKey, String key, SeeUser object) {
// TODO Auto-generated method stub
}
}
再写一个jsp,测试之后打开redis客户端查看