SpringBoot搭建SSM整合Redis缓存数据(菜鸟学习)

前言

自学多时,写一个demo做个总结。
从建项目开始,比较简单的实现springboot+redis缓存,供初学者参考,更快的理解redis缓存。

环境

win10
Idea2018.1.2x64
jdk1.8.0_131
mysql5.7.21
Redis3.2.100
RedisDesktopManager0.8.8.384(redis桌面管理工具)
mysql,redis都是用本地环境

第一步:搭建ssm框架连接mysql

新建项目

https://start.spring.io/
在这里插入图片描述
将项目导入idea

项目结构

创建各种包和类如下图
在这里插入图片描述

pojo类

这东西到底叫pojo,bean,domain,entity还是vo,我也分不清反正就是实体类
要实现toString方法

package com.sunc.springbootredis.pojo;

public class Hello {
    private Integer id;
    private String text;

    @Override
    public String toString() {
        return "Hello{" +
                "id=" + id +
                ", text='" + text + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

service类

package com.sunc.springbootredis.service;

import com.sunc.springbootredis.pojo.Hello;

public interface HelloService {
    public Hello sayHello(Hello hello);
}

service实现类

package com.sunc.springbootredis.service.Impl;

import com.sunc.springbootredis.mapper.HelloMapper;
import com.sunc.springbootredis.pojo.Hello;
import com.sunc.springbootredis.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HelloServiceImpl implements HelloService {
    @Autowired
    private HelloMapper helloMapper;

    @Override
    public Hello sayHello(Hello hello) {
        return helloMapper.sayHello(hello);
    }
}

mapper

package com.sunc.springbootredis.mapper;

import com.sunc.springbootredis.pojo.Hello;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface HelloMapper {

    @Select("SELECT * FROM hello WHERE id = #{id}")
    public Hello sayHello(Hello hello);
}

controller

package com.sunc.springbootredis.controller;

import com.sunc.springbootredis.pojo.Hello;
import com.sunc.springbootredis.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {
    @Autowired
    private HelloService helloService;

    @RequestMapping("/sayHello")
    public Hello sayHello(){
        Hello hello = new Hello();
        hello.setId(1);
        return helloService.sayHello(hello);
    }
}

Application.properties配置

这里添加debug,方便看mapper中执行sql情况

spring.datasource.url=jdbc:mysql://localhost:3306/sunc?Unicode=true&characterEncoding=UTF8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123321

logging.level.com.sunc.springbootredis.mapper=debug
spring.redis.host=localhost

建表并添加数据

在这里插入图片描述
在这里插入图片描述

如果使用mapper.xml写SQL

启动类加注解

@MapperScan("com.suncspringbootredis.mapper")

配置文件扫描静态文件)
(注意:mapper.xml放在resources静态文件夹下)

mybatis.mapper-locations=classpath:mapper/*.xml

请求controller

浏览器中请求controller可以看到返回数据,说明已经连接成功
http://localhost:8080/hello/sayHello

连续请求两次,log显示查询两次mysql。框架搭建成功
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190212103432904.png

第二步:整合redis

pojo实体类实现Serializable

public class Hello implements Serializable{

启动类新增注解@EnableCaching

package com.sunc.springbootredis;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@MapperScan("com.suncspringbootredis.mapper")
@EnableCaching
public class SpringbootRedisApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootRedisApplication.class, args);
	}
}

Service实现类新增注解@Cacheable(cacheNames = {“hello”})

@Service
public class HelloServiceImpl implements HelloService {
    @Autowired
    private HelloMapper helloMapper;

    @Cacheable(cacheNames = {"hello"})
    @Override
    public Hello sayHello(Hello hello) {
        return helloMapper.sayHello(hello);
    }
}

这时连续请求两次controller

在这里插入图片描述
可以看到Log日志只打印一次sql,说明第二次是从redis缓存中查询的,并没有请求mysql
查看RedisDesktopManager可以发现redis已经有hello了
在这里插入图片描述
这里text为啥是null我也不知道了,反正是好用了

成功

但是数据看出并没有做序列化,使用的是jdk自带的序列化。后续学习序列化
如有偏颇敬请斧正,本厮邮箱:suncch@163.com

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Boot是一个用于简化Spring应用程序开发的框架。它提供了一套开箱即用的配置,可以快速搭建基于Spring的应用程序。SSM框架是指Spring + SpringMVC + MyBatis组合使用的一种开发框架。下面是关于如何使用Spring Boot搭建SSM框架的步骤: 1. 首先,我们需要创建一个Maven项目。可以通过在IDE中选择创建Maven项目的方式来创建一个新的Maven项目。 2. 在pom.xml文件中添加Spring Boot和SSM的相关依赖。在这里,我们需要引入spring-boot-starter-web,spring-boot-starter-data-jpa,mybatis-spring-boot-starter等依赖。 3. 创建一个Spring Boot的主类,使用@SpringBootApplication注解进行标记。这个类将作为整个应用的入口。 4. 创建一个控制器类,使用@RestController注解进行标记,并编写一些API接口方法。 5. 创建一个实体类,用于映射数据库中的表结构。 6. 创建一个DAO接口,使用@Mapper注解进行标记,并在方法上使用@Select、@Insert、@Update等注解编写SQL语句。 7. 创建一个Service类,使用@Service注解进行标记,并在类中注入DAO接口的实现。 8. 创建一个配置类,用于配置数据库连接等信息。 9. 在配置文件application.properties或application.yml中配置数据库连接信息。 10. 运行Spring Boot应用程序,可以通过访问定义的API接口来进行测试。 通过以上步骤,我们就可以使用Spring Boot搭建SSM框架。Spring Boot提供了自动配置的功能,可以帮助我们快速集成SSM框架所需的组件,并且简化了开发过程。使用Spring Boot搭建SSM框架可以提高开发效率和代码质量,同时还能够实现更好的性能和可扩展性。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值