SpringBoot study notes 6_ integrate Redis

Thirty-four SpringBoot integrate Redis (combined SpringBoot integration Mybatis )

34.0 The case is to achieve a single version of Redis integration

 34.1 related to dependence

 1           <dependency>
 2           <groupId>org.springframework.boot</groupId>
 3           <artifactId>spring-boot-starter-web</artifactId>  
 4        </dependency>
 5   
 6            <!-- mybatis(注解方式) -->
 7        <dependency>
 8                <groupId>org.mybatis.spring.boot</groupId>
 9                <artifactId>mybatis-spring-boot-starter</artifactId>
10                <version>1.3.1</version>
11        </dependency>
12        
13        <!-- Mysql -->
14        <dependency>
15                <groupId>mysql</groupId>
16                <artifactId>mysql-connector-java</artifactId>
17        </dependency>
18        
19        <!-- redis依赖 -->
<20        dependency>  
21             <groupId>org.springframework.boot</groupId>  
22             <artifactId>spring-boot-starter-data-redis</artifactId>  
23        </dependency>
Its dependencies

34.2 mysql configuration and redis

 1 #数据库配置
 2 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 3 spring.datasource.username=root
 4 spring.datasource.password=
 5 spring.datasource.url=jdbc:mysql://localhost:3306/springboot
 6 
 7 #redis单服务器配置
 8 spring.redis.database=0
 9 spring.redis.host=127.0.0.1
10 spring.redis.port=6379
11 spring.redis.pool.max-active=8
12 spring.reids.pool.max-wait=-1
13 spring.redis.pool.max-idle=8
14 spring.redis.pool.min-idle=0
15 spring.redis.timeout=0
16     
application.properties

34.3 open the cache

Added to the startup class @EnableCaching  comment 

 1 package com.wu.app;
 2 
 3 import org.mybatis.spring.annotation.MapperScan;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 import org.springframework.cache.annotation.EnableCaching;
 7 
 8 
 9 @SpringBootApplication(scanBasePackages={"com.wu.controller","com.wu.service"})
10 @MapperScan("com.wu.mapper")//需要单独扫描
11 @EnableCaching//开启缓存
12 public class SpringApplications {
13         //程序启动入口
14         public static void main(String []args){
15             SpringApplication.run(SpringApplications.class, args);
16         }
17 }
Startup class

Add in the service layer method needs to be cached in @Cacheable comment

 1 package com.wu.service;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.cache.annotation.Cacheable;
 5 import org.springframework.stereotype.Service;
 6 
 7 import com.wu.mapper.UsersMapper;
 8 import com.wu.pojo.Users;
 9 @Service
10 public class UsersServiceImp implements UsersService {
11     @Autowired
12     private UsersMapper mapper;
13     
14     @Cacheable (value = "User") // set the key 
15      @Override
 16      public the Users selectByName (String name) {
 . 17          System.out.println ( "Find from the database" );
 18 is          return mapper.selectByName (name);
 19      }
 20 is      
21 is      
22 is }
UsersServiceImp.java

Entity need to achieve serial interfaces  implements Serializable

 1 public class Users implements Serializable{
 2     private Integer id;
 3 
 4     private String name;
 5 
 6     private String password;
 7 
 8     private String email;
 9 
10     private Date birthday;
11 
12     public Integer getId() {
13         return id;
14     }
15 
16     public void setId(Integer id) {
17         this.id = id;
18     }
19 
20     public String getName() {
21         return name;
22     }
23 
24     public void setName(String name) {
25         this.name = name == null ? null : name.trim();
26     }
27 
28     public String getPassword() {
29         return password;
30     }
31 
32     public void setPassword(String password) {
33         this.password = password == null ? null : password.trim();
34     }
35 
36     public String getEmail() {
37         return email;
38     }
39 
40     public void setEmail(String email) {
41         this.email = email == null ? null : email.trim();
42     }
43 
44     public Date getBirthday() {
45         return birthday;
46     }
47 
48     public void setBirthday(Date birthday) {
49         this.birthday = birthday;
50     }
Entity class

 34.4 results

That is the first time such a query from a database query, the query that is loaded from the cache again

Guess you like

Origin www.cnblogs.com/wuba/p/11256111.html