`

35.[视频] Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】

阅读更多

 à悟空学院:https://t.cn/Rg3fKJD

学院中有Spring Boot相关的课程!点击「阅读原文」进行查看!

SpringBoot视频:http://t.cn/A6ZagYTi

Spring Cloud视频:http://t.cn/A6ZagxSR

SpringBoot Shiro视频:http://t.cn/A6Zag7IV

SpringBoot交流平台:https://t.cn/R3QDhU0

SpringData和JPA视频:http://t.cn/A6Zad1OH

SpringSecurity5.0视频:http://t.cn/A6ZadMBe

Sharding-JDBC分库分表实战http://t.cn/A6ZarrqS

分布式事务解决方案「手写代码」:http://t.cn/A6ZaBnIr

 

 

【本文章是否对你有用以及是否有好的建议,请留言】

       本文章牵涉到的技术点比较多:Spring Data JPARedisSpring MVC,Spirng Cache,所以在看这篇文章的时候,需要对以上这些技术点有一定的了解或者也可以先看看这篇文章,针对文章中实际的技术点在进一步了解(注意,您需要自己下载Redis Server到您的本地,所以确保您本地的Redis可用,这里还使用了MySql数据库,当然你也可以内存数据库进行测试)。这篇文章会提供对应的Eclipse代码示例,具体大体的分如下几个步骤:

(1)新建Java Maven Project;
(2)在pom.xml中添加相应的依赖包;
(3)编写Spring Boot启动类;
(4)配置application.properties;
(5)编写RedisCacheConfig配置类;
(6)编写DemoInfo测试实体类;
(7)编写DemoInfoRepository持久化类;
(8)编写DemoInfoService类;
(9)编写DemoInfoController类;
(10)测试代码是否正常运行了
(11)自定义缓存key;

 


1)新建Java Maven Project;

       这个步骤就不细说,新建一个spring-boot-redis Java maven project;

 

2)在pom.xml中添加相应的依赖包

Maven中添加相应的依赖包,主要有:spring boot 父节点依赖;spring boot web支持;缓存服务spring-context-support;添加redis支持;JPA操作数据库;mysql 数据库驱动,具体pom.xml文件如下:

 

<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.kfit</groupId>
  <artifactId>spring-boot-redis</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
 
  <name>spring-boot-redis</name>
  <url>http://maven.apache.org</url>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- 配置JDK编译版本. -->
    <java.version>1.8</java.version>
  </properties>
 
  <!-- spring boot 父节点依赖,
       引入这个之后相关的引入就不需要添加version配置,
        spring boot会自动选择最合适的版本进行添加。
    -->
    <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>1.3.3.RELEASE</version>
    </parent>
 
 
  <dependencies>
       
        <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <scope>test</scope>
       </dependency>
 
       <!-- spring boot web支持:mvc,aop... -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
      
       <!--
           包含支持UI模版(Velocity,FreeMarker,JasperReports),
           邮件服务,
           脚本服务(JRuby),
           缓存Cache(EHCache),
           任务计划Scheduling(uartz)。
        -->
       <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context-support</artifactId>
        </dependency>
 
       <!-- 添加redis支持-->
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
        </dependency>
      
       <!-- JPA操作数据库. -->
       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
      
       <!-- mysql 数据库驱动. -->
       <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
        </dependency>
       
        <!-- 单元测试. -->
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
      
  </dependencies>
</project>
 

 

上面是完整的pom.xml文件,每个里面都进行了简单的注释。

 

3)编写Spring Boot启动类(com.kfit.App

 

package com.kfit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * Spring Boot启动类;
 *
 * @author Angel(QQ:412887952)
 * @version v.0.1
 */
 
@SpringBootApplication
public class App {
       /**
        * -javaagent:.\lib\springloaded-1.2.4.RELEASE.jar -noverify
        * @param args
        */
       public static void main(String[] args) {
              SpringApplication.run(App.class, args);
       }
}
 

 

 

4)配置application.properties;

这里主要是配置两个资源,第一就是数据库基本信息;第二就是redis配置;第三就是JPA的配置;

Src/main/resouces/application.properties

########################################################

###datasource  配置MySQL数据源;

########################################################

spring.datasource.url = jdbc:mysql://localhost:3306/test

spring.datasource.username = root

spring.datasource.password = root

spring.datasource.driverClassName = com.mysql.jdbc.Driver

spring.datasource.max-active=20

spring.datasource.max-idle=8

spring.datasource.min-idle=8

spring.datasource.initial-size=10

 

 

 

########################################################

###REDIS (RedisProperties) redis基本配置;

########################################################

# database name

spring.redis.database=0

# server host1

spring.redis.host=127.0.0.1  

# server password

#spring.redis.password=

#connection port

spring.redis.port=6379

# pool settings ...

spring.redis.pool.max-idle=8

spring.redis.pool.min-idle=0

spring.redis.pool.max-active=8

spring.redis.pool.max-wait=-1

# name of Redis server

#spring.redis.sentinel.master=

# comma-separated list of host:port pairs

#spring.redis.sentinel.nodes=


########################################################
### Java Persistence Api 自动进行建表
########################################################
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

 

 

5)编写RedisCacheConfig配置类

       缓存主要有几个要实现的类:其一就是CacheManager缓存管理器;其二就是具体操作实现类;其三就是CacheManager工厂类(这个可以使用配置文件配置的进行注入,也可以通过编码的方式进行实现);其四就是缓存key生产策略(当然Spring自带生成策略,但是在Redis客户端进行查看的话是系列化的key,对于我们肉眼来说就是感觉是乱码了,这里我们先使用自带的缓存策略)。

com.kfit.config/RedisCacheConfig

 

package com.kfit.config;
 
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
 
/**
 * redis 缓存配置;
 *
 * 注意:RedisCacheConfig这里也可以不用继承:CachingConfigurerSupport,也就是直接一个普通的Class就好了;
 *
 * 这里主要我们之后要重新实现 key的生成策略,只要这里修改KeyGenerator,其它位置不用修改就生效了。
 *
 * 普通使用普通类的方式的话,那么在使用@Cacheable的时候还需要指定KeyGenerator的名称;这样编码的时候比较麻烦。
 *
 * @author Angel(QQ:412887952)
 * @version v.0.1
 */
@Configuration
@EnableCaching//启用缓存,这个注解很重要;
public class RedisCacheConfig extends CachingConfigurerSupport {
   
   
    /**
     * 缓存管理器.
     * @param redisTemplate
     * @return
     */
    @Bean
    public CacheManager cacheManager(RedisTemplate<?,?> redisTemplate) {
       CacheManager cacheManager = new RedisCacheManager(redisTemplate);
       return cacheManager;
    }
 
   
    /**
     * redis模板操作类,类似于jdbcTemplate的一个类;
     *
     * 虽然CacheManager也能获取到Cache对象,但是操作起来没有那么灵活;
     *
     * 这里在扩展下:RedisTemplate这个类不见得很好操作,我们可以在进行扩展一个我们
     *
     * 自己的缓存类,比如:RedisStorage类;
     *
     * @param factory : 通过Spring进行注入,参数在application.properties进行配置;
     * @return
     */
    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
       RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
       redisTemplate.setConnectionFactory(factory);
      
       //key序列化方式;(不然会出现乱码;),但是如果方法上有Long等非String类型的话,会报类型转换错误;
       //所以在没有自己定义key生成策略的时候,以下这个代码建议不要这么写,可以不配置或者自己实现ObjectRedisSerializer
       //或者JdkSerializationRedisSerializer序列化方式;
//     RedisSerializer<String> redisSerializer = new StringRedisSerializer();//Long类型不可以会出现异常信息;
//     redisTemplate.setKeySerializer(redisSerializer);
//     redisTemplate.setHashKeySerializer(redisSerializer);
      
       return redisTemplate;
    }
 
}
 

 

在以上代码有很详细的注释,在这里还是在简单的提下:

RedisCacheConfig这里也可以不用继承CachingConfigurerSupport,也就是直接一个普通的Class就好了;这里主要我们之后要重新实现 key的生成策略,只要这里修改KeyGenerator,其它位置不用修改就生效了。普通使用普通类的方式的话,那么在使用@Cacheable的时候还需要指定KeyGenerator的名称;这样编码的时候比较麻烦。

 

 

6)编写DemoInfo测试实体类

       编写一个测试实体类:com.kfit.bean.DemoInfo

package com.kfit.bean;
 
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
 * 测试实体类,这个随便;
 * @author Angel(QQ:412887952)
 * @version v.0.1
 */
@Entity
public class DemoInfo  implements Serializable{
    private static final long serialVersionUID = 1L;
    @Id @GeneratedValue
    private long id;
    private String name;
    private String pwd;
    public long getId() {
       return id;
    }
    public void setId(longid) {
       this.id = id;
    }
    public String getName() {
        return name;
    }
    publicvoid setName(String name) {
       this.name = name;
    }
    public String getPwd() {
       return pwd;
    }
    publicvoid setPwd(String pwd) {
       this.pwd = pwd;
    }
   
    @Override
    public String toString() {
       return "DemoInfo [id=" + id + ", name=" + name + ", pwd=" + pwd + "]";
    }
}

 

 

7)编写DemoInfoRepository持久化类;

       DemoInfoRepository使用Spirng Data JPA实现:

com.kfit.repository.DemoInfoRepository

package com.kfit.repository;
 
import org.springframework.data.repository.CrudRepository;
 
import com.kfit.bean.DemoInfo;
 
/**
 * DemoInfo持久化类
 * @author Angel(QQ:412887952)
 * @version v.0.1
 */
public interface DemoInfoRepository extends CrudRepository<DemoInfo,Long> {
 
}

 

 

8)编写DemoInfoService

       编写DemoInfoService,这里有两个技术方面,第一就是使用Spring @Cacheable注解方式和RedisTemplate对象进行操作,具体代码如下:

com.kfit.service.DemoInfoService:

package com.kfit.service;
 
import com.kfit.bean.DemoInfo;
 
/**
 * demoInfo 服务接口
 * @author Angel(QQ:412887952)
 * @version v.0.1
 */
public interface DemoInfoService {
   
    public DemoInfo findById(long id);
   
    public void deleteFromCache(long id);
 
    void test();
}

 

 

 

com.kfit.service.impl.DemoInfoServiceImpl:

package com.kfit.service.impl;
 
import javax.annotation.Resource;
 
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
 
import com.kfit.bean.DemoInfo;
import com.kfit.repository.DemoInfoRepository;
import com.kfit.service.DemoInfoService;
 
/**
 *
 *DemoInfo数据处理类
 *
 * @author Angel(QQ:412887952)
 * @version v.0.1
 */
@Service
public class DemoInfoServiceImpl implements DemoInfoService {
   
    @Resource
    private DemoInfoRepository demoInfoRepository;
   
    @Resource
    private RedisTemplate<String,String> redisTemplate;
   
    @Override
    public void test(){
       ValueOperations<String,String> valueOperations = redisTemplate.opsForValue();
       valueOperations.set("mykey4", "random1="+Math.random());
       System.out.println(valueOperations.get("mykey4"));
    }
   
    //keyGenerator="myKeyGenerator"
    @Cacheable(value="demoInfo") //缓存,这里没有指定key.
    @Override
    public DemoInfo findById(longid) {
       System.err.println("DemoInfoServiceImpl.findById()=========从数据库中进行获取的....id="+id);
       return demoInfoRepository.findOne(id);
    }
   
    @CacheEvict(value="demoInfo")
    @Override
    public void deleteFromCache(longid) {
       System.out.println("DemoInfoServiceImpl.delete().从缓存中删除.");
    }
   
}

 

 

 

9)编写DemoInfoController

package com.kfit.controller;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import com.kfit.bean.DemoInfo;
import com.kfit.service.DemoInfoService;
 
/**
 * 测试类.
 * @author Angel(QQ:412887952)
 * @version v.0.1
 */
@Controller
public class DemoInfoController {
   
    @Autowired
     DemoInfoService demoInfoService;
     
   
    @RequestMapping("/test")
    public @ResponseBody String test(){
        DemoInfo loaded = demoInfoService.findById(1);
System.out.println("loaded="+loaded);
DemoInfo cached = demoInfoService.findById(1);
        System.out.println("cached="+cached);
        loaded = demoInfoService.findById(2);
        System.out.println("loaded2="+loaded);
        return "ok";
    }
   
   
    @RequestMapping("/delete")
    public @ResponseBody String delete(longid){
        demoInfoService.deleteFromCache(id);
        return"ok";
    }
   
    @RequestMapping("/test1")
    public @ResponseBody String test1(){
        demoInfoService.test();
        System.out.println("DemoInfoController.test1()");
        return "ok";
    }
   
}

 

 

10)测试代码是否正常运行了

 

启动应用程序,访问地址:http://127.0.0.1:8080/test

查看控制台可以查看:

DemoInfoServiceImpl.findById()=========从数据库中进行获取的....id=1

loaded=DemoInfo [id=1, name=张三, pwd=123456]

cached=DemoInfo [id=1, name=张三, pwd=123456]

DemoInfoServiceImpl.findById()=========从数据库中进行获取的....id=2

loaded2=DemoInfo [id=2, name=张三, pwd=123456]

如果你看到以上的打印信息的话,那么说明缓存成功了。

 

访问地址:http://127.0.0.1:8080/test1

random1=0.9985031320746356

DemoInfoController.test1()

 

二次访问:http://127.0.0.1:8080/test

loaded=DemoInfo [id=1, name=张三, pwd=123456]

cached=DemoInfo [id=1, name=张三, pwd=123456]

loaded2=DemoInfo [id=2, name=张三, pwd=123456]

这时候所有的数据都是执行缓存的。

 

这时候执行删除动作:http://127.0.0.1:8080/delete?id=1

然后在访问:http://127.0.0.1:8080/test

DemoInfoServiceImpl.findById()=========从数据库中进行获取的....id=1

loaded=DemoInfo [id=1, name=张三, pwd=123456]

cached=DemoInfo [id=1, name=张三, pwd=123456]

loaded2=DemoInfo [id=2, name=张三, pwd=123456]

 

11)自定义缓存key;

com.kfit.config.RedisCacheConfig类中重写CachingConfigurerSupport中的keyGenerator ,具体实现代码如下:

 

/**
     * 自定义key.
     * 此方法将会根据类名+方法名+所有参数的值生成唯一的一个key,即使@Cacheable中的value属性一样,key也会不一样。
     */
    @Override
    public KeyGenerator keyGenerator() {
       System.out.println("RedisCacheConfig.keyGenerator()");
       return new KeyGenerator() {
           @Override
           public Object generate(Object o, Method method, Object... objects) {
              // This will generate a unique key of the class name, the method name
              //and all method parameters appended.
              StringBuilder sb = new StringBuilder();
              sb.append(o.getClass().getName());
              sb.append(method.getName());
              for (Object obj : objects) {
                  sb.append(obj.toString());
              }
              System.out.println("keyGenerator=" + sb.toString());
              return sb.toString();
           }
       };
    }

 

 

这时候在redis的客户端查看key的话还是序列化的肉眼看到就是乱码了,那么我改变key的序列方式,这个很简单,redis底层已经有具体的实现类了,我们只需要配置下:

//key序列化方式;(不然会出现乱码;),但是如果方法上有Long等非String类型的话,会报类型转换错误;
//所以在没有自己定义key生成策略的时候,以下这个代码建议不要这么写,可以不配置或者自己实现ObjectRedisSerializer
//或者JdkSerializationRedisSerializer序列化方式;
       RedisSerializer<String> redisSerializer = new StringRedisSerializer();//Long类型不可以会出现异常信息;
       redisTemplate.setKeySerializer(redisSerializer);
       redisTemplate.setHashKeySerializer(redisSerializer);

 

 

综上以上分析:RedisCacheConfig类的方法调整为:

package com.kfit.config;
 
import java.lang.reflect.Method;
 
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
 
/**
 * redis 缓存配置;
 *
 * 注意:RedisCacheConfig这里也可以不用继承:CachingConfigurerSupport,也就是直接一个普通的Class就好了;
 *
 * 这里主要我们之后要重新实现 key的生成策略,只要这里修改KeyGenerator,其它位置不用修改就生效了。
 *
 * 普通使用普通类的方式的话,那么在使用@Cacheable的时候还需要指定KeyGenerator的名称;这样编码的时候比较麻烦。
 *
 * @author Angel(QQ:412887952)
 * @version v.0.1
 */
@Configuration
@EnableCaching//启用缓存,这个注解很重要;
public class RedisCacheConfig extends CachingConfigurerSupport {
   
    /**
     * 缓存管理器.
     * @param redisTemplate
     * @return
     */
    @Bean
    public CacheManager cacheManager(RedisTemplate<?,?> redisTemplate) {
       CacheManager cacheManager = new RedisCacheManager(redisTemplate);
       return cacheManager;
    }
 
   
    /**
     * RedisTemplate缓存操作类,类似于jdbcTemplate的一个类;
     *
     * 虽然CacheManager也能获取到Cache对象,但是操作起来没有那么灵活;
     *
     * 这里在扩展下:RedisTemplate这个类不见得很好操作,我们可以在进行扩展一个我们
     *
     * 自己的缓存类,比如:RedisStorage类;
     *
     * @param factory : 通过Spring进行注入,参数在application.properties进行配置;
     * @return
     */
    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
       RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
       redisTemplate.setConnectionFactory(factory);
      
       //key序列化方式;(不然会出现乱码;),但是如果方法上有Long等非String类型的话,会报类型转换错误;
       //所以在没有自己定义key生成策略的时候,以下这个代码建议不要这么写,可以不配置或者自己实现ObjectRedisSerializer
       //或者JdkSerializationRedisSerializer序列化方式;
       RedisSerializer<String> redisSerializer = new StringRedisSerializer();//Long类型不可以会出现异常信息;
       redisTemplate.setKeySerializer(redisSerializer);
       redisTemplate.setHashKeySerializer(redisSerializer);
      
       return redisTemplate;
    }
   
    /**
     * 自定义key.
     * 此方法将会根据类名+方法名+所有参数的值生成唯一的一个key,即使@Cacheable中的value属性一样,key也会不一样。
     */
    @Override
    public KeyGenerator keyGenerator() {
       System.out.println("RedisCacheConfig.keyGenerator()");
       return new KeyGenerator() {
           @Override
           public Object generate(Object o, Method method, Object... objects) {
              // This will generate a unique key of the class name, the method name
              //and all method parameters appended.
              StringBuilder sb = new StringBuilder();
              sb.append(o.getClass().getName());
              sb.append(method.getName());
              for (Object obj : objects) {
                  sb.append(obj.toString());
              }
              System.out.println("keyGenerator=" + sb.toString());
              return sb.toString();
           }
       };
    }
 
}

 

 

这时候在访问地址:http://127.0.0.1:8080/test

这时候看到的Key就是:com.kfit.service.impl.DemoInfoServiceImplfindById1

在控制台打印信息是:

1keyGenerator=com.kfit.service.impl.DemoInfoServiceImplfindById1

2DemoInfoServiceImpl.findById()=========从数据库中进行获取的....id=1

3keyGenerator=com.kfit.service.impl.DemoInfoServiceImplfindById1

4loaded=DemoInfo [id=1, name=张三, pwd=123456]

5keyGenerator=com.kfit.service.impl.DemoInfoServiceImplfindById1

6cached=DemoInfo [id=1, name=张三, pwd=123456]

7keyGenerator=com.kfit.service.impl.DemoInfoServiceImplfindById2

8keyGenerator=com.kfit.service.impl.DemoInfoServiceImplfindById2

10DemoInfoServiceImpl.findById()=========从数据库中进行获取的....id=2

11loaded2=DemoInfo [id=2, name=张三, pwd=123456]

 

其中@Cacheable,@CacheEvict下节进行简单的介绍,这节的东西实在是太多了,到这里就打住吧,剩下的就需要靠你们自己进行扩展了。

 

 Spring Boot 系列博客】

à悟空学院:https://t.cn/Rg3fKJD

学院中有Spring Boot相关的课程!点击「阅读原文」进行查看!

SpringBoot视频:http://t.cn/A6ZagYTi

Spring Cloud视频:http://t.cn/A6ZagxSR

SpringBoot Shiro视频:http://t.cn/A6Zag7IV

SpringBoot交流平台:https://t.cn/R3QDhU0

SpringData和JPA视频:http://t.cn/A6Zad1OH

SpringSecurity5.0视频:http://t.cn/A6ZadMBe

Sharding-JDBC分库分表实战http://t.cn/A6ZarrqS

分布式事务解决方案「手写代码」:http://t.cn/A6ZaBnIr

 

 

历史章节

 

第一章 快速开始

1、Spring Boot之Hello World

2、Spring Boot之Hello World访问404

 

第二章 Spring Boot之JSON

1、spring boot返回json数据

2、Spring Boot完美使用FastJson解析JSON数据

 

第三章 Spring Boot热部署

1、Spring Boot热部署(springloader)

2、springboot + devtools(热部署)

 

第四章 Spring Boot数据库

1、Spring Boot JPA/Hibernate/Spring Data概念

2、Spring Boot JPA-Hibernate

3、Spring Boot Spring Data JPA介绍

4、Spring Boot JdbcTemplate

5、Spring Boot集成MyBatis

 

第五章 web开发

1、全局异常捕捉

2、配置server信息

3、spring boot使用thymeleaf

4、Spring Boot 使用freemarker

5、Spring Boot添加JSP支持

 

第六章 定时任务

1、Spring Boot定时任务

2、Spring Boot 定时任务升级篇(动态修改cron参数)

3、Spring Boot 定时任务升级篇(动态添加修改删除定时任务)

4、Spring Boot 定时任务升级篇(集群/分布式下的定时任务说明)

5、Spring Boot Quartz介绍

6、Spring Boot Quartz在Java Project中使用

7、Spring Boot 集成Quartz普通使用

8、Spring Boot 集成Quartz升级版

9、Spring Boot 集成Quartz二次升级版

10、Spring Boot 集成Quartz-Job如何自动注入Spring容器托管的对象

 

第七章 Spring Boot MyBatis升级篇

1、Spring Boot MyBatis升级篇-注解

2、Spring Boot MyBatis升级篇-注解-自增ID

3、Spring Boot MyBatis升级篇-注解-增删改查

4、Spring Boot MyBatis升级篇-注解-分页查询

5、Spring Boot MyBatis升级篇-注解-分页PageHelper不生效

6、Spring Boot MyBatis升级篇-注解- mybatic insert异常:BindingException: Parameter 'name' not found

7、Spring Boot MyBatis升级篇-注解- #和$符号特别篇

8、Spring Boot MyBatis升级篇-注解-@Result

9、Spring Boot MyBatis升级篇-注解-动态SQL(if test)-方案一:<script>

10、Spring Boot MyBatis升级篇-注解-动态SQL(if test)-方案二:@Provider

11、Spring Boot MyBatis升级篇-注解-动态SQL-参数问题

12、Spring Boot MyBatis升级篇-注解-特别篇:@MapperScan和@Mapper

13、Spring Boot MyBatis升级篇-XML

14、Spring Boot MyBatis升级篇-XML-自增ID

15、Spring Boot MyBatis升级篇-XML-增删改查

16、Spring Boot MyBatis升级篇-XML-分页查询

17、Spring Boot MyBatis升级篇-XML-分页PageHelper不生效

18、Spring Boot MyBatis升级篇-XML-动态SQL(if test)

19、Spring Boot MyBatis升级篇-XML-注解-初尝试

20、Spring Boot MyBatis升级篇- pagehelper替换为pagehelper-spring-boot-starter

 

第八章 Spring Boot 知识点1

1、Spring Boot 拦截器HandlerInterceptor

2、Spring Boot启动加载数据CommandLineRunner

3、Spring Boot环境变量读取和属性对象的绑定

4、Spring Boot使用自定义的properties

5、Spring Boot使用自定义的properties

6、Spring Boot使用@SpringBootApplication

7、Spring Boot 监控和管理生产环境

 

第十章 Spring Boot 打包部署

1、Spring Boot打包部署((提供Linux的sh文件))

 

第十一章 Spring Boot 日志

1、spring boot日志—理论

2、Spring Boot日志-logback

 

3、Spring Boot日志-log4j2

 

 

更多查看博客: http://412887952-qq-com.iteye.com/ye.com/blog/2294942

 

 

 

分享到:
评论
20 楼 林祥纤 2018-03-23  
h448249687 写道
起初用Redis-x64-3.2.100一直报错nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool],后面改成redis2.8.240可以正常使用。可以redis版本有要求加以解释么?新手搞不太清楚这个问题,谢谢  


1、每个版本的连接实现. 你使用的redis-client和reids-server要匹配.
2、连接的配置属性可能还一样.
当然主要是第一点。
19 楼 h448249687 2018-03-21  
起初用Redis-x64-3.2.100一直报错nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool],后面改成redis2.8.240可以正常使用。可以redis版本有要求加以解释么?新手搞不太清楚这个问题,谢谢  
18 楼 林祥纤 2017-08-27  
YQuite 写道
用这个竟然没有RrdisTemplate
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
        </dependency>

去官网的Redis例子看了下,好像是这个
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>



版本问题。。。。。
17 楼 YQuite 2017-08-27  
加上自定义key之后好像要执行redisTemplate.delete才能从redis中删除
16 楼 YQuite 2017-08-27  
用这个竟然没有RrdisTemplate
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
        </dependency>

去官网的Redis例子看了下,好像是这个
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
15 楼 金武飞扬 2017-07-29  
金武飞扬 写道
我在删除1的缓存后,再次执行查询,还是从缓存出来的啊



好像是加上上面的那个自定义key后,在执行delete后就不会删除缓存哦????
是这样吗?
14 楼 金武飞扬 2017-07-28  
我在删除1的缓存后,再次执行查询,还是从缓存出来的啊

13 楼 林祥纤 2017-02-07  
chengbingbbs 写道
执行报:nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool]
自己封装的RedisUtil是可以获取连接的,我本地redis版本是3.0的,是redis版本的原因吗?


可能是,redis版本有要求的。
12 楼 chengbingbbs 2017-02-07  
执行报:nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool]
自己封装的RedisUtil是可以获取连接的,我本地redis版本是3.0的,是redis版本的原因吗?
11 楼 u013448485 2016-11-30  
直接用 StringRedisTemplate 可以替换  RedisTemplate<String,String>,
10 楼 林祥纤 2016-10-11  
程序员小红 写道
能不能在这篇基础上扩展下redis集群。


需要看时间安排,最近个人事情比较多。
9 楼 程序员小红 2016-10-10  
能不能在这篇基础上扩展下redis集群。
8 楼 itlang01 2016-10-07  
你DemoInfoController类的test方法只是你输出是那样而已,实际上还是从数据库获取的,你打开hibernate的show-sql就能看到实际是执行了两次数据库的查询。
itlang01 写道
你DemoInfoController类的test方法只是你输出是那样而已,实际上还是从数据库获取的,你打开hibernate的show-sql就能看到实际是执行了两次数据库的查询。


找到原因了,是@cacheble没有把数据写入Redis导致的。和shiro集成后就无法写入了。用你这篇文章的源码可以写入Redis。
7 楼 林祥纤 2016-10-05  
itlang01 写道
所以你说的RedisConnectionFactory到底有没有配置啊?在你得application.properties中没找到


没有配置的话,就是spring boot底层已经处理了。
6 楼 itlang01 2016-10-05  
你DemoInfoController类的test方法只是你输出是那样而已,实际上还是从数据库获取的,你打开hibernate的show-sql就能看到实际是执行了两次数据库的查询。
5 楼 itlang01 2016-10-05  
itlang01 写道
ok

4 楼 itlang01 2016-10-05  
所以你说的RedisConnectionFactory到底有没有配置啊?在你得application.properties中没找到
3 楼 林祥纤 2016-07-04  
tangduDream 写道
我不知道你有没注意,spring-cache注解竟然没expire这个属性。你说呢?


你可以自己编写一个 @Cacheable 使用aop进行拦截处理,实现自己的定制的缓存管理。
2 楼 林祥纤 2016-07-04  
tangduDream 写道
我不知道你有没注意,spring-cache注解竟然没expire这个属性。你说呢?


还真没注意到。
1 楼 tangduDream 2016-07-03  
我不知道你有没注意,spring-cache注解竟然没expire这个属性。你说呢?

相关推荐

Global site tag (gtag.js) - Google Analytics