Spring-Data-Redis之实例Demo

        了解完Spring Data Redis基本功能之后,根据快速开始的项目自己动手做了一个小Demo。使用的框架是Spring+Spring MVC+Mybatis;开发环境是Eclipse Kepler+JDK1.8+Maven3.2.3。

 

1、首先创建Maven工程

 

        在pom.xml文件中引入需要用到的jar包,这里是引入了所有的jar包,可根据自己的情况添加或删除jar包。

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.tgb</groupId>
  <artifactId>aop-redis-demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <properties>
  	<spring.version>4.3.8.RELEASE</spring.version>
  	<mybatis.version>3.2.8</mybatis.version>
	<mybatis.spring.version>1.2.2</mybatis.spring.version>
	<mysql.version>5.1.32</mysql.version>
	<jackson.version>2.4.2</jackson.version>
	<druid.version>1.0.9</druid.version>
  </properties>
  
  <dependencies>
  	<dependency>
		<groupId>org.apache.commons</groupId>
		<artifactId>commons-lang3</artifactId>
		<version>3.5</version>
	</dependency>
	<dependency>
		<groupId>commons-logging</groupId>
		<artifactId>commons-logging</artifactId>
		<version>1.2</version>
	</dependency>
  	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
		<version>${spring.version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context-support</artifactId>
		<version>${spring.version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-beans</artifactId>
		<version>${spring.version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>${spring.version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-jdbc</artifactId>
		<version>${spring.version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-aspects</artifactId>
		<version>${spring.version}</version>
	</dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>    
	    <groupId>org.springframework.data</groupId>    
	    <artifactId>spring-data-commons</artifactId>    
	    <version>1.12.4.RELEASE</version>    
	</dependency>
    <dependency>    
	    <groupId>org.springframework.data</groupId>    
	    <artifactId>spring-data-redis</artifactId>    
	    <version>1.8.0.RELEASE</version>    
	</dependency>
	<dependency>    
	    <groupId>org.springframework.data</groupId>    
	    <artifactId>spring-data-keyvalue</artifactId>    
	    <version>1.1.4.RELEASE</version>    
	</dependency>   
	<dependency>   
	    <groupId>redis.clients</groupId>   
	    <artifactId>jedis</artifactId>   
	    <version>2.9.0</version>   
	</dependency> 
	<dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis</artifactId>
		<version>${mybatis.version}</version>
	</dependency>
	<dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis-spring</artifactId>
		<version>${mybatis.spring.version}</version>
	</dependency>
	<!-- MySql -->
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>${mysql.version}</version>
	</dependency>
	<!-- 连接池 -->
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>druid</artifactId>
		<version>${druid.version}</version>
	</dependency>
	<dependency>
		<groupId>com.fasterxml.jackson.core</groupId>
		<artifactId>jackson-databind</artifactId>
		<version>${jackson.version}</version>
	</dependency>
  </dependencies>
</project>

 

 

2、逆向工程生成Mapper

 

        用逆向工程的方式,根据建好的数据库生成相应的Mapper。创建自己的数据库,建立相应的表。下面给出我建表的执行脚本:

 

DROP TABLE IF EXISTS `demo`;
CREATE TABLE `demo` (
  `name` char(50) DEFAULT NULL,
  `id` char(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

 

        逆向工程配置相应的数据库地址和信息,即可生成Mapper和POJO对象,将这些对象拷贝到自己的项目相应包下就可以了。

 

3、创建Spring配置文件

 

        在classpath下创建Spring配置文件applicationContext.xml,并在相应位置创建db.properties文件,配置数据库信息。

 

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	
	<context:component-scan base-package="com"/>

    <aop:aspectj-autoproxy proxy-target-class="true"/>
    
	<context:property-placeholder location="classpath:properties/*.properties"/>
	
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		destroy-method="close">
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="maxActive" value="10" />
		<property name="minIdle" value="5" />
	</bean>
	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 数据库连接池 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 加载mybatis的全局配置文件 -->
		<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
	</bean>
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.aop.redis.mapper" />
	</bean> 
	
	<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
            <property name="minIdle" value="${redis.minIdle}" />  
            <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> 
	
	<bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
	    <!--配置集群-->
            <constructor-arg name="clusterConfig" ref="redisClusterConfig" />
        </bean> 
	     
	<!-- redis template definition -->  
	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
	    <property name="connectionFactory" ref="jedisConnFactory" />
	    <property name="keySerializer">  
	        <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
	    </property>     
	    <property name="valueSerializer">  
	        <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />  
	    </property>  
	    <property name="hashKeySerializer">    
	       <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>    
	    </property>  
	    <property name="hashValueSerializer">  
	       <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>    
	    </property>  
	</bean> 
	
	<bean id="redisClusterConfig" class="org.springframework.data.redis.connection.RedisClusterConfiguration">  
            <property name="maxRedirects" value="3" />  
            <property name="clusterNodes">  
                <set>  
	           <bean class="org.springframework.data.redis.connection.RedisNode">  
	               <constructor-arg name="host" value="192.168.*.*"></constructor-arg>   
	               <constructor-arg name="port" value="6379"></constructor-arg>  
	           </bean>  
                   <bean class="org.springframework.data.redis.connection.RedisNode">  
                       <constructor-arg name="host" value="192.168.*.*"></constructor-arg>   
                       <constructor-arg name="port" value="6380"></constructor-arg>  
                   </bean>  
                   <bean class="org.springframework.data.redis.connection.RedisNode">  
                       <constructor-arg name="host" value="192.168.*.*"></constructor-arg>   
                       <constructor-arg name="port" value="6381"></constructor-arg>  
                   </bean>  
               </set>  
           </property>  
       </bean>  
</beans>
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/aop-redis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456

 

 

4、编写Service代码

 

@Service
@Transactional
public class RedisTemplateServiceImpl implements RedisTemplateService {

	@Autowired
	private DemoMapper demoMapper;

	@Autowired
	private RedisTemplate redisTemplate;

	public List<Demo> getByName(String name) {
		DemoExample example = new DemoExample();
		Criteria criteria = example.createCriteria();
		criteria.andNameEqualTo(name);
		return demoMapper.selectByExample(example);
	}

	public List<Demo> findAll() {
		DemoExample example = new DemoExample();
		List<Demo> demos = new ArrayList<Demo>();
		demos = demoMapper.selectByExample(example);
		// ListOperations<String, Object> list = redisTemplate.opsForList();
		// RedisSerializer<String> serializer =
		// redisTemplate.getStringSerializer();
		// serializer.serialize("allDemoList");

		// list.rightPush("allDemoList", demos);
		// redisTemplate.expire("allDemoList", 30, TimeUnit.SECONDS);
		BoundListOperations operations = redisTemplate
		        .boundListOps("allDemoList");
		operations.rightPush(demos);
		operations.expire(30, TimeUnit.SECONDS);
		return demos;
	}

	public Demo getById(String id) {
		DemoExample example = new DemoExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(id);
		ValueOperations<String, Object> value = redisTemplate.opsForValue();
		value.set("demoId", id);
		return demoMapper.selectByExample(example).get(0);
	}

}

从启动类启动后,调用该Service的方法,即可存储数据到Redis缓存中去。

 

  

        完整项目请点击链接下载:http://download.csdn.net/detail/u013038861/9850543。RedisTemplate具体的使用API下篇博客再介绍。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值