Enterprise Distribution Micro Service Cloud Springcloud springboot MyBatis (23) Integrated Redis

Source: Internet
Author: User

Tag: INF Port this operation Alt. class share CTI File

Introduce dependencies:

To add a redis dependency in the Pom file:

<dependency>            <groupId>org.springframework.boot</groupId>            <artifactId> Spring-boot-starter-data-redis</artifactid></dependency>

  

Configure the data source
Spring.redis.host=localhostspring.redis.port=6379#spring.redis.password=spring.redis.database= 1spring.redis.pool.max-active=8spring.redis.pool.max-wait=-1spring.redis.pool.max-idle= 500spring.redis.pool.min-idle=0spring.redis.timeout=0

  

If your redis has a password, you can configure it. With this two-step operation, you can access the Redis data.

Data Access Layer DAO

Access to Redis via Redistemplate.

@Repositorypublic class Redisdao {    @Autowired    private stringredistemplate template;    Public  void Setkey (String key,string value) {        valueoperations<string, string> ops = Template.opsforvalue ();        Ops.set (key,value,1, timeunit.minutes);//1 minute expires    } public    String GetValue (string key) {        valueoperations <string, string> Ops = This.template.opsForValue ();        return Ops.get (key);}    }

  

Unit Test
@RunWith (springrunner.class) @SpringBootTestpublic class Springbootredisapplicationtests {public    static Logger Logger= Loggerfactory.getlogger (springbootredisapplicationtests.class);    @Test public    void Contextloads () {    }    @Autowired    Redisdao Redisdao;    @Test public    void Testredis () {        redisdao.setkey ("name", "Forezp");        Redisdao.setkey ("Age", "one");        Logger.info (Redisdao.getvalue ("name"));        Logger.info (Redisdao.getvalue ("Age"));}    }

  

To start the unit test, you find that the console is printed: unit test passed;

Forezp11

  

Source Source

Enterprise Distribution Micro Service Cloud Springcloud springboot MyBatis (23) Integrated Redis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.