redis安装
可参考redis菜鸟教程
1.解压后的目录
2.打开cmd窗口,切换到安装目录,输入:
redis-server.exe redis.windows.conf
启动redis
springboot整合redis
1.pom.xml文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 模板引擎 Thymeleaf 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Spring Data JPA 依赖 :: 数据持久层框架 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- h2 数据源依赖 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Cache 缓存依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Redis 数据源依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
2.application.properties文件
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=-1ms
其他参考设置
## Redis 配置
## Redis数据库索引(默认为0)
spring.redis.database=0
## Redis服务器地址
spring.redis.host=127.0.0.1
## Redis服务器连接端口
spring.redis.port=6379
## Redis服务器连接密码(默认为空)
spring.redis.password=
## 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
## 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
## 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
## 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
## 连接超时时间(毫秒)
spring.redis.timeout=0
3.项目详情
各个包详情
domain实体层:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.io.Serializable;
@Entity
public class Book implements Serializable{//注意序列化
@Id
@GeneratedValue
private Long id;
private String name;
private String writer;
private String introduction;
忽略getter和setter
}
Jpa持久化接口:
import org.springframework.data.jpa.repository.JpaRepository;
import cn.kingstar.ch4redis.domain.Book;
/**
* @author wujinxing
* @date: 2018/11/16 11:24
* @description:
*/
public interface BookRepository extends JpaRepository<Book,Long> {
}
Service层:
import cn.kingstar.ch4redis.domain.Book;
import java.util.List;
/**
* @author: wujinxing
* @date: 2018/11/16 11:28
* @description:
*/
public interface BookService {
/**
* 获取所有book
* @return
*/
List<Book> findAll();
/**
* 新增book
* @param book
* @return
*/
Book insertByBook(Book book);
/**
* 更新book
* @param book
* @return
*/
Book update(Book book);
/**
* 删除book
* @param id
* @return
*/
Book delete(Long id);
/**
* 获取book
* @param id
* @return
*/
Book findById(Long id);
}
实现:
import cn.kingstar.ch4redis.Service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import cn.kingstar.ch4redis.domain.Book;
import java.util.List;
import cn.kingstar.ch4redis.domain.BookRepository;
/**
* @auther: wujinxing
* @date: 2018/11/16 11:29
* @description:
*/
@Service
@CacheConfig(cacheNames = "books") //缓存注解,稍后解释
public class BookServiceImpl implements BookService {
@Autowired
BookRepository bookRepository;
@Override
public List<Book> findAll() {
return bookRepository.findAll();
}
@Override
public Book insertByBook(Book book) {
return bookRepository.save(book);
}
@CachePut(key = "#p0.id")
@Override
public Book update(Book book) {
System.out.println("call update method");
return bookRepository.save(book);
}
@CacheEvict(key = "#p0")
@Override
public Book delete(Long id) {
System.out.println("call delete method");
Book book = bookRepository.findById(id).get();
bookRepository.delete(book);
return book;
}
@Cacheable(key = "#p0")
@Override
public Book findById(Long id) {
System.out.println("call findById method");
return bookRepository.findById(id).get();
}
}
Controller层:
import cn.kingstar.ch4redis.Service.BookService;
import cn.kingstar.ch4redis.domain.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* @auther: wujinxing
* @date: 2018/11/16 11:55
* @description:
*/
@Controller
@RequestMapping(value = "/book")
public class BookController {
private static final String BOOK_FROM_PATH_NAME = "bookForm";
private static final String BOOK_LIST_PATH_NAME = "bookList";
private static final String REDIRECT_TO_BOOK_URL = "redirect:/book";
@Autowired
BookService bookService;
/**
* 获取Book列表
* 处理"/book"的GET请求,获取Book列表
* @param map
* @return
*/
@RequestMapping(method = RequestMethod.GET)
public String getBookList(ModelMap map){
map.addAttribute("bookList",bookService.findAll());
return BOOK_LIST_PATH_NAME;
}
/**
* 获取创建Book表单
* @param map
* @return
*/
@RequestMapping(value = "/create",method = RequestMethod.GET)
public String createBookForm(ModelMap map){
map.addAttribute("book",new Book());
map.addAttribute("action","create");
return BOOK_FROM_PATH_NAME;
}
/**
* 创建Book
* 处理 "/book/create" 的 POST 请求,用来新建 Book 信息
* 通过 @ModelAttribute 绑定表单实体参数,也通过 @RequestParam 传递参数
* @param book
* @return
*/
@RequestMapping(value = "/create",method = RequestMethod.POST)
public String postBook(@ModelAttribute Book book){
bookService.insertByBook(book);
return REDIRECT_TO_BOOK_URL;
}
/**
* 获取更新 Book 表单
* 处理 "/book/update/{id}" 的 GET 请求,通过 URL 中的 id 值获取 Book 信息
* URL 中的 id ,通过 @PathVariable 绑定参数
*/
@RequestMapping(value = "/update/{id}",method = RequestMethod.GET)
public String getUser(@PathVariable Long id,ModelMap map){
map.addAttribute("book",bookService.findById(id));
map.addAttribute("action","update");
return BOOK_FROM_PATH_NAME;
}
/**
* 更新 Book
* 处理 "/update" 的 PUT 请求,用来更新 Book 信息
*/
@RequestMapping(value = "/update",method = RequestMethod.POST)
public String putBook(@ModelAttribute Book book){
bookService.update(book);
return REDIRECT_TO_BOOK_URL;
}
/**
* 删除 Book
* 处理 "/book/{id}" 的 GET 请求,用来删除 Book 信息
*/
@RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
public String deleteBook(@PathVariable Long id){
bookService.delete(id);
return REDIRECT_TO_BOOK_URL;
}
}
resources中template文件夹的静态文件:
bookList:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<script type="text/javascript" th:src="@{https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js}"></script>
<link th:href="@{https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css}" rel="stylesheet"/>
<link th:href="@{/css/default.css}" rel="stylesheet"/>
<link rel="icon" th:href="@{/images/favicon.ico}" type="image/x-icon"/>
<meta charset="UTF-8"/>
<title>书籍列表</title>
</head>
<body>
<div class="contentDiv">
<h5>《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 5 章《数据存储》Demo </h5>
<table class="table table-hover table-condensed">
<legend>
<strong>书籍列表</strong>
</legend>
<thead>
<tr>
<th>书籍编号</th>
<th>书名</th>
<th>作者</th>
<th>简介</th>
<th>管理</th>
</tr>
</thead>
<tbody>
<tr th:each="book : ${bookList}">
<th scope="row" th:text="${book.id}"></th>
<td><a th:href="@{/book/update/{bookId}(bookId=${book.id})}" th:text="${book.name}"></a></td>
<td th:text="${book.writer}"></td>
<td th:text="${book.introduction}"></td>
<td><a class="btn btn-danger" th:href="@{/book/delete/{bookId}(bookId=${book.id})}">删除</a></td>
</tr>
</tbody>
</table>
<div><a class="btn btn-primary" href="/book/create" role="button">新增书籍</a></div>
</div>
</body>
</html>
bookForm:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<script type="text/javascript" th:src="@{https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js}"></script>
<link th:href="@{https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css}" rel="stylesheet"/>
<link th:href="@{/css/default.css}" rel="stylesheet"/>
<link rel="icon" th:href="@{/images/favicon.ico}" type="image/x-icon"/>
<meta charset="UTF-8"/>
<title>书籍管理</title>
</head>
<body>
<div class="contentDiv">
<h5>《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 5 章《数据存储》Demo </h5>
<legend>
<strong>书籍管理</strong>
</legend>
<form th:action="@{/book/{action}(action=${action})}" method="post" class="form-horizontal">
<input type="hidden" name="id" th:value="${book.id}"/>
<div class="form-group">
<label for="book_name" class="col-sm-2 control-label">书名:</label>
<div class="col-xs-4">
<input type="text" class="form-control" id="book_name" name="name" th:value="${book.name}"
th:field="*{book.name}"/>
</div>
</div>
<div class="form-group">
<label for="book_writer" class="col-sm-2 control-label">作者:</label>
<div class="col-xs-4">
<input type="text" class="form-control" id="book_writer" name="writer" th:value="${book.writer}"
th:field="*{book.writer}"/>
</div>
</div>
<div class="form-group">
<label for="book_introduction" class="col-sm-2 control-label">简介:</label>
<div class="col-xs-4">
<textarea class="form-control" id="book_introduction" rows="3" name="introduction"
th:value="${book.introduction}" th:field="*{book.introduction}"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input class="btn btn-primary" type="submit" value="提交"/>
<input class="btn" type="button" value="返回" onclick="history.back()"/>
</div>
</div>
</form>
</div>
</body>
</html>