zoukankan      html  css  js  c++  java
  • xxxx

    xxxx

    pom.xml;


    <project xmlns="" xmlns:xsi=""

    xsi:schemaLocation=" ">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.testboot</groupId>

    <artifactId>Bootest</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <parent>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-parent</artifactId>

    <version>1.5.7.RELEASE</version>

    </parent>

    <dependencies>

    <!-- 开启web -->

    <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-web</artifactId>

    </dependency>

    <!-- thymeleaf模板引擎 -->

    <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-thymeleaf</artifactId>

    </dependency>

    <!-- 用JdbcTemplate与数据库进行连接 -->

    <!-- MYSQL -->

    <dependency>

    <groupId>mysql</groupId>

    <artifactId>mysql-connector-java</artifactId>

    </dependency>

    <!-- Spring Boot JDBC -->

    <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-jdbc</artifactId>

    </dependency>

    <!-- 用JdbcTemplate与数据库进行连接 -->

    <!-- druid -->

    <dependency>

    <groupId>com.alibaba</groupId>

    <artifactId>druid</artifactId>

    <version>1.0.29</version>

    </dependency>

    <!-- redis -->

    <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-data-redis</artifactId>

    </dependency>

    </dependencies>

    <build>

    <plugins>

    <plugin>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-maven-plugin</artifactId>

    </plugin>

    </plugins>

    </build>

    </project>


    application.yml资源文件:


    logging:

    level:

    org.springframework: INFO

    com.example: DEBUG

    spring:

    datasource:

    url : jdbc:mysql://localhost:3306/mysqllocalhost

    username : root

    password : 123456

    driverClassName : com.mysql.jdbc.Driver

    redis:

    host: 127.0.0.1

    password: 123456

    port: 6379

    pool:

    max-idle: 100

    min-idle: 1

    max-active: 1000

    max-wait: -1

    redis 配置类

    package com.boot.cache;

    import org.springframework.beans.factory.annotation.Autowired;

    import org.springframework.boot.autoconfigure.data.redis.RedisProperties;

    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.connection.jedis.JedisConnectionFactory;

    import org.springframework.data.redis.core.RedisTemplate;

    import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;

    import org.springframework.data.redis.serializer.RedisSerializer;

    import org.springframework.data.redis.serializer.StringRedisSerializer;

    /**

    * redis 配置类

    * @author Administrator

    */

    @Configuration

    @EnableCaching// 启用缓存

    public class RedisCacheConfig extends CachingConfigurerSupport{

    @Autowired

    private RedisProperties redisProperties;

    @Bean(name="redisConnectionFactory")

    public JedisConnectionFactory redisConnectionFactory(){

    JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();

    redisConnectionFactory.setHostName(redisProperties.getHost());

    redisConnectionFactory.setPort(redisProperties.getPort());

    redisConnectionFactory.setPassword(redisProperties.getPassword());

    return redisConnectionFactory;

    }

    /***

    * 缓存管理器

    * @param redisTemplate

    */

    @Bean

    public CacheManager cacheManager(RedisTemplate<?,?> redisTemplate){

    CacheManager cacheManager = new RedisCacheManager(redisTemplate);

    return cacheManager;

    }

    /**

    * redis模板操作类,类似于jdbcTemplate的一个类;

    * 防止中文乱码

    */

    @Bean

    public RedisTemplate<String ,String> redisTemplate(RedisConnectionFactory factory){

    RedisTemplate<String,String> redisTemplate = new RedisTemplate<String,String>();

    redisTemplate.setConnectionFactory(factory);

    RedisSerializer<String> redisSerializer = new StringRedisSerializer();//Long类型不可以会出现异常信息;

    redisTemplate.setKeySerializer(redisSerializer);

    redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

    redisTemplate.setHashKeySerializer(redisSerializer);

    return redisTemplate;

    }


    }

    redis增删查方法类

    package com.boot.cache;

    import java.util.concurrent.TimeUnit;

    import org.springframework.beans.factory.annotation.Autowired;

    import org.springframework.data.redis.core.StringRedisTemplate;

    import org.springframework.data.redis.core.ValueOperations;

    import org.springframework.stereotype.Repository;

    @Repository

    public class RedisDao {

    @Autowired

    private StringRedisTemplate template;

    //添加缓存数据

    public void setKey(String key, String value) {

    ValueOperations<String, String> ops = template.opsForValue();

    ops.set(key, value, 10, TimeUnit.MINUTES);// 10分钟后过期

    }

    //查询缓存数据

    public String getValue(String key) {

    ValueOperations<String, String> ops = this.template.opsForValue();

    return ops.get(key);

    }

    //删除缓存数据

    public void removeValue(String key) {

    template.delete(key);

    }

    }

  • 相关阅读:
    02.替换空格 (Java)
    01.二维数组中的查找 (Java)
    css
    CSS Selectors
    Golang Singleton
    TL;DR
    go get
    golang string、int、int64 float 互相转换
    Thrift支持的基本数据类型
    双亲委派模型
  • 原文地址:https://www.cnblogs.com/wdtzms/p/8254168.html
Copyright © 2011-2022 走看看