Spring Data的CrudRepository使用及其遇到的问题和解决方法

  • Post author:
  • Post category:其他



Spring Data

项目的目的是为了简化构建基于 Spring 框架应用的数据访问计数,包括非关系数据库、Map-Reduce 框架、云数据服务等等;另外也包含对关系数据库的访问支持。



目的

本篇博文的主要目的是使用Spring Data Redis的

CrudRepository

接口,完成对

Redis

的基本操作,如

保存、删除、查看是否存在、查询全部、分页查询

等。



环境准备



添加依赖

本文采用是Maven工程来构建工程,所包括的依赖有

spring-data-redis

,详细信息如下:

		<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-redis</artifactId>
			<version>1.8.0.RELEASE</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.9.0</version>
		</dependency>

192749_xvSb_2911530.png



Redis配置类

package com.xxx.springdata.redis.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;

@Configuration
@ComponentScan("com.xxx.springdata")
@PropertySource("classpath:redis.properties")
@EnableRedisRepositories("com.xxx.springdata")
public class RedisAppConfig {

	@Value("${redis.host}")
	private String redisHost;

	@Value("${redis.port}")
	private int redisPort;

	@Bean
	public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}

	@Bean
	JedisConnectionFactory jedisConnectionFactory() {
		JedisConnectionFactory factory = new JedisConnectionFactory();
		factory.setHostName(redisHost);
		factory.setPort(redisPort);
		factory.setUsePool(true);
		return factory;
	}

	@Bean
	RedisTemplate<?, ?> redisTemplate() {
		RedisTemplate<byte[], byte[]> template = new RedisTemplate<>();
		template.setConnectionFactory(jedisConnectionFactory());
		return template;
	}

	@Bean
	CacheManager cacheManager() {
		return new RedisCacheManager(redisTemplate());
	}
}

其中

redis.host



redis.port

来源于

src/main/resources

目录下的

redis.properties

文件

redis.host=172.xx.xx.xx
redis.port=6379



Book类

本文使用Book类来完成Spring Data Redis的操作,Book类包含

id, name, isbn 以及authors

, 分别表示Book Id, 书名,ISBN以及书本作者,其它信息如出版信息等都不在这个示例中。

	/** Book ID */
	private Long id;

	/** 书名 */
	private String name;

	/** ISBN */
	private String isbn;

	/** 作者 */
	private List<String> authors;

详细的代码如下:

package com.xxx.springdata.redis.entity;

import java.io.Serializable;
import java.util.List;

import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.index.Indexed;

@RedisHash("books")
public class Book implements Serializable {

	private static final long serialVersionUID = 4665407921597036726L;

	/** Book ID */
	@Id
	private Long id;

	/** 书名 */
	@Indexed
	private String name;

	/** ISBN */
	private String isbn;

	/** 作者 */
	private List<String> authors;

	public Book() {
	}

	public Book(Long id, String name, String isbn, List<String> authors) {
		this.id = id;
		this.name = name;
		this.isbn = isbn;
		this.authors = authors;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the isbn
	 */
	public String getIsbn() {
		return isbn;
	}

	/**
	 * @param isbn
	 *            the isbn to set
	 */
	public void setIsbn(String isbn) {
		this.