选择生成的依赖

选择保存的工程路径

查询已经生成的依赖,并修改mysql的版本

 <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!--修改mysql的驱动版本-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

pom.xml

项目结构如下:

编辑属性文件application.properties

 server.port=8080

 #spring.application.name=springboot-redis

 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/kgc
spring.datasource.username=root
spring.datasource.password=ok #redis
#redis的服务所在的ip地址
spring.redis.host=192.168.117.143
#redis的端口号
spring.redis.port=6379
# redis数据库索引(默认为0) 密码默认是没有的,可以不写
spring.redis.database=0 #打印sql语句,改为自己的mapper包路径
logging.level.cn.kgc.mapper=debug

application.properties

创建持久化类Student.java

 package cn.kgc.vo;

 import java.io.Serializable;

 /**
* Created by Administrator on 2019/7/22.
*/
public class Student implements Serializable{
private Integer sid;
private String sname;
private String password;
private String subject;
private String result;
private Integer cid; public Student() {
} public Student(Integer sid, String sname, String password, String subject, String result, Integer cid) {
this.sid = sid;
this.sname = sname;
this.password = password;
this.subject = subject;
this.result = result;
this.cid = cid;
} public Integer getSid() {
return sid;
} public void setSid(Integer sid) {
this.sid = sid;
} public String getSname() {
return sname;
} public void setSname(String sname) {
this.sname = sname;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getSubject() {
return subject;
} public void setSubject(String subject) {
this.subject = subject;
} public String getResult() {
return result;
} public void setResult(String result) {
this.result = result;
} public Integer getCid() {
return cid;
} public void setCid(Integer cid) {
this.cid = cid;
} @Override
public String toString() {
return "Student{" +
"sid=" + sid +
", sname='" + sname + '\'' +
", password='" + password + '\'' +
", subject='" + subject + '\'' +
", result='" + result + '\'' +
", cid=" + cid +
'}';
}
}

Student.java

创建数据访问层接口StudentMapper.java

 package cn.kgc.mapper;

 import cn.kgc.vo.Student;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update; import java.util.List; /**
* Created by Administrator on 2019/7/22.
*/
public interface StudentMapper {
@Select("select * from student")
List<Student> findAll(); @Select("select * from student where sid=#{sid}")
Student findBySid(Integer sid); @Delete("delete from student where sid=#{sid}")
int del(Integer sid); @Update("update student set sname=#{sname},password=#{password},subject=#{subject},result=#{result},cid=#{cid} where sid=#{sid}")
int modify(Student student); @Insert("insert into student(sname,password,subject,result,cid) values(#{sname},#{password},#{subject},#{result},#{cid})")
int add(Student student); }

StudentMapper.java

创建业务层接口StudentService.java

 package cn.kgc.service;

 import cn.kgc.vo.Student;

 import java.util.List;

 /**
* Created by Administrator on 2019/7/22.
*/
public interface StudentService {
List<Student> findAll(); Student findBySid(Integer sid); int del(Integer sid); int modify(Student student); int add(Student student);
}

StudentService.java

创建业务层接口实现类StudentServiceImpl.java

 package cn.kgc.service;

 import cn.kgc.mapper.StudentMapper;
import cn.kgc.vo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import java.util.List; @Service
@Transactional
public class StudentServiceImpl implements StudentService{
@Autowired
StudentMapper studentMapper; @Autowired
RedisTemplate redisTemplate; @Override
public List<Student> findAll() {
String key="student";
ListOperations<String,Student> operations= redisTemplate.opsForList(); //缓存存在
if (redisTemplate.hasKey(key)){
return operations.range(key,0,-1);
}else{
//得到学生集合
List<Student> list=studentMapper.findAll();
operations.leftPushAll(key,list);
return list;
}
} @Override
public Student findBySid(Integer sid) {
String key = "student_" + sid;
ValueOperations<String, Student> operations = redisTemplate.opsForValue(); //缓存存在
if (redisTemplate.hasKey(key)) {
System.out.println("redis-siddata");
return operations.get(key);
} else {
//得到学生对象
Student student = studentMapper.findBySid(sid);
//添加到缓存
operations.set(key, student);
System.out.println("student-siddata");
return student;
}
} @Override
public int del(Integer sid) {
//删除数据库中的数据
int count = studentMapper.del(sid); //缓存存在
String key = "student_" + sid;
if (redisTemplate.hasKey(key)) {
//删除对应缓存
redisTemplate.delete(key);
}
return count;
} @Override
public int modify(Student student) {
//修改数据库中的数据
int count = studentMapper.modify(student); ValueOperations operations = redisTemplate.opsForValue();
//缓存存在
String key = "student_" + student.getSid();
if (redisTemplate.hasKey(key)) {
//更新缓存
Student stu = studentMapper.findBySid(student.getSid());
operations.set(key, stu);
}
return count;
} @Override
public int add(Student student) {
//添加数据
int count = studentMapper.add(student);
return count;
}
}

StudentServiceImpl.java

创建控制层StudentController.java

 package cn.kgc.controller;

 import cn.kgc.service.StudentService;
import cn.kgc.vo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import java.util.List; @RestController
public class StudentController { @Autowired
StudentService studentService; @GetMapping("/student")
public List<Student> findAll() {
return studentService.findAll();
} /* @GetMapping("/student/{sid}")
public Student findById(@PathVariable("sid") Integer sid) {
return studentService.findBySid(sid);
}
*/
@GetMapping("/findById")
public Student findById(Integer sid) {
return studentService.findBySid(sid);
} @PostMapping("/student/{sid}")
public int del(@PathVariable("sid") Integer sid) {
return studentService.del(sid);
} @PutMapping("/student")
public int modify(Student student) {
return studentService.modify(student);
} }

StudentController.java

启动类

 package cn.kgc;

 import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//指定要扫描的mapper包
@MapperScan("cn.kgc.mapper")
@SpringBootApplication
public class SpringbootMybatisRedisApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisRedisApplication.class, args);
} }

启动类

测试如下:

第一次访问

查看redis

第二次访问

7.springboot+mybatis+redis整合的更多相关文章

  1. SpringBoot + Mybatis + Redis 整合入门项目

    这篇文章我决定一改以往的风格,以幽默风趣的故事博文来介绍如何整合 SpringBoot.Mybatis.Redis. 很久很久以前,森林里有一只可爱的小青蛙,他迈着沉重的步伐走向了找工作的道路,结果发 ...

  2. SpringCloud+MyBatis+Redis整合—— 超详细实例(二)

    2.SpringCloud+MyBatis+Redis redis①是一种nosql数据库,以键值对<key,value>的形式存储数据,其速度相比于MySQL之类的数据库,相当于内存读写 ...

  3. springboot + mybatis + mycat整合

    1.mycat服务 搭建mycat服务并启动,windows安装参照. 系列文章: [Mycat 简介] [Mycat 配置文件server.xml] [Mycat 配置文件schema.xml] [ ...

  4. SpringBoot+Mybatis+MybatisPlus整合实现基本的CRUD操作

    SpringBoot+Mybatis+MybatisPlus整合实现基本的CRUD操作 1> 数据准备 -- 创建测试表 CREATE TABLE `tb_user` ( `id` ) NOT ...

  5. redis(七)---- SpringBoot和redis整合

    SpringBoot和Redis整合非常简单 添加pom依赖 <dependency> <groupId>org.springframework.boot</groupI ...

  6. springboot+mybatis+redis实现分布式缓存

    大家都知道springboot项目都是微服务部署,A服务和B服务分开部署,那么它们如何更新或者获取共有模块的缓存数据,或者给A服务做分布式集群负载,如何确保A服务的所有集群都能同步公共模块的缓存数据, ...

  7. SpringBoot日记——Redis整合

    上一篇文章,简单记录了一下缓存的使用方法,这篇文章将把我们熟悉的redis整合进来. 那么如何去整合呢?首先需要下载和安装,为了使用方便,也可以做环境变量的配置. 下载和安装的方法,之前有介绍,在do ...

  8. springboot和Redis整合

    springboot简化了许多的配置,大大提高了使用效率.下面介绍一下和Redis整合的一些注意事项. 首先介绍单机版的redis整合. 1.第一步当然是导入依赖 <dependency> ...

  9. Springboot Mybatis Redis 实现二级缓存

    前言 什么是mybatis二级缓存? 二级缓存是多个sqlsession共享的,其作用域是mapper的同一个namespace. 即,在不同的sqlsession中,相同的namespace下,相同 ...

随机推荐

  1. JS 变量 相关内容

    JS变量按存储方式区分为哪些类型?: js变量按照存储方式分为两种类型:值类型 和 引用类型 1.值类型(基本类型): 布尔值(boolean) . null .undefined .数值(numbe ...

  2. thinkphp ajax调用demo

    http://files.cnblogs.com/files/jxkshu/tp_ckgd.rar

  3. img标签+map的使用

    img标签+map的使用 img标签含有一个usemap属性,用法相当于锚点的使用,usemap="#useName".然后就是map标签,具体代码: <body> & ...

  4. [nRF51822 AK II 教程]第一课,开发环境的配置及背景介绍【转】

    低功耗蓝牙4.0是全新的技术,并不向下兼容,也就是说它和蓝牙3.0.2.0什么的都不能通信的.另外,蓝牙4.0目前的规范只能做外设和主机(智能手机,电脑等)通讯,也就是说你想用一个单模的蓝牙4.0开发 ...

  5. Codeforces451A-Game With Sticks-思维

    After winning gold and silver in IOI 2014, Akshat and Malvika want to have some fun. Now they are pl ...

  6. 【Stanford Machine Learning Open Course】学习笔记目录

    这里是斯坦福大学机器学习网络课程的学习笔记. 课程地址是:https://class.coursera.org/ml-2012-002/lecture/index 课程资料百度网盘分享链接:https ...

  7. localStorage使用总结(转载)

    localStorage使用总结  本文转载自:https://www.cnblogs.com/st-leslie/p/5617130.html(点击标题可跳转至原文) 一.什么是localStora ...

  8. 你真的懂return吗?

    递归算法中什么时候用return啥时候不用呢? 使用2个例子来说明:快速排序和二分查找 # 二分查找def binarySearch (arr, l, r, x): # 基本判断 if r >= ...

  9. print的简单使用

    import time num=20 for i in range(num): print("#", end="") 结果如下: 加个强制刷新 num=20 f ...

  10. Linux 安装 python3.6

    Linux下安装Python3.6和第三方库   如果本机安装了python2,尽量不要管他,使用python3运行python脚本就好,因为可能有程序依赖目前的python2环境, 比如yum!!! ...