6.springboot+mybatis+redis整合
Posted 红酒人生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了6.springboot+mybatis+redis整合相关的知识,希望对你有一定的参考价值。
选择生成的依赖
选择保存的工程路径
查询已经生成的依赖,并修改mysql的版本
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-data-redis</artifactId> 5 </dependency> 6 <dependency> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-jdbc</artifactId> 9 </dependency> 10 <dependency> 11 <groupId>org.springframework.boot</groupId> 12 <artifactId>spring-boot-starter-web</artifactId> 13 </dependency> 14 <dependency> 15 <groupId>org.mybatis.spring.boot</groupId> 16 <artifactId>mybatis-spring-boot-starter</artifactId> 17 <version>2.1.0</version> 18 </dependency> 19 <!--修改mysql的驱动版本--> 20 <dependency> 21 <groupId>mysql</groupId> 22 <artifactId>mysql-connector-java</artifactId> 23 <version>5.1.38</version> 24 </dependency> 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-starter-test</artifactId> 28 <scope>test</scope> 29 </dependency> 30 </dependencies>
项目结构如下:
编辑属性文件application.properties
1 server.port=8080 2 3 #spring.application.name=springboot-redis 4 5 spring.datasource.driver-class-name=com.mysql.jdbc.Driver 6 spring.datasource.url=jdbc:mysql://localhost:3306/kgc 7 spring.datasource.username=root 8 spring.datasource.password=ok 9 10 11 #redis 12 #redis的服务所在的ip地址 13 spring.redis.host=192.168.117.143 14 #redis的端口号 15 spring.redis.port=6379 16 # redis数据库索引(默认为0) 密码默认是没有的,可以不写 17 spring.redis.database=0 18 19 #打印sql语句,改为自己的mapper包路径 20 logging.level.cn.kgc.mapper=debug
创建持久化类Student.java
1 package cn.kgc.vo; 2 3 import java.io.Serializable; 4 5 /** 6 * Created by Administrator on 2019/7/22. 7 */ 8 public class Student implements Serializable{ 9 private Integer sid; 10 private String sname; 11 private String password; 12 private String subject; 13 private String result; 14 private Integer cid; 15 16 public Student() { 17 } 18 19 public Student(Integer sid, String sname, String password, String subject, String result, Integer cid) { 20 this.sid = sid; 21 this.sname = sname; 22 this.password = password; 23 this.subject = subject; 24 this.result = result; 25 this.cid = cid; 26 } 27 28 public Integer getSid() { 29 return sid; 30 } 31 32 public void setSid(Integer sid) { 33 this.sid = sid; 34 } 35 36 public String getSname() { 37 return sname; 38 } 39 40 public void setSname(String sname) { 41 this.sname = sname; 42 } 43 44 public String getPassword() { 45 return password; 46 } 47 48 public void setPassword(String password) { 49 this.password = password; 50 } 51 52 public String getSubject() { 53 return subject; 54 } 55 56 public void setSubject(String subject) { 57 this.subject = subject; 58 } 59 60 public String getResult() { 61 return result; 62 } 63 64 public void setResult(String result) { 65 this.result = result; 66 } 67 68 public Integer getCid() { 69 return cid; 70 } 71 72 public void setCid(Integer cid) { 73 this.cid = cid; 74 } 75 76 @Override 77 public String toString() { 78 return "Student{" + 79 "sid=" + sid + 80 ", sname=\'" + sname + \'\\\'\' + 81 ", password=\'" + password + \'\\\'\' + 82 ", subject=\'" + subject + \'\\\'\' + 83 ", result=\'" + result + \'\\\'\' + 84 ", cid=" + cid + 85 \'}\'; 86 } 87 }
创建数据访问层接口StudentMapper.java
1 package cn.kgc.mapper; 2 3 import cn.kgc.vo.Student; 4 import org.apache.ibatis.annotations.Delete; 5 import org.apache.ibatis.annotations.Insert; 6 import org.apache.ibatis.annotations.Select; 7 import org.apache.ibatis.annotations.Update; 8 9 import java.util.List; 10 11 /** 12 * Created by Administrator on 2019/7/22. 13 */ 14 public interface StudentMapper { 15 @Select("select * from student") 16 List<Student> findAll(); 17 18 @Select("select * from student where sid=#{sid}") 19 Student findBySid(Integer sid); 20 21 @Delete("delete from student where sid=#{sid}") 22 int del(Integer sid); 23 24 @Update("update student set sname=#{sname},password=#{password},subject=#{subject},result=#{result},cid=#{cid} where sid=#{sid}") 25 int modify(Student student); 26 27 @Insert("insert into student(sname,password,subject,result,cid) values(#{sname},#{password},#{subject},#{result},#{cid})") 28 int add(Student student); 29 30 }
创建业务层接口StudentService.java
1 package cn.kgc.service; 2 3 import cn.kgc.vo.Student; 4 5 import java.util.List; 6 7 /** 8 * Created by Administrator on 2019/7/22. 9 */ 10 public interface StudentService { 11 List<Student> findAll(); 12 13 Student findBySid(Integer sid); 14 15 int del(Integer sid); 16 17 int modify(Student student); 18 19 int add(Student student); 20 }
创建业务层接口实现类StudentServiceImpl.java
1 package cn.kgc.service; 2 3 import cn.kgc.mapper.StudentMapper; 4 import cn.kgc.vo.Student; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.data.redis.core.ListOperations; 7 import org.springframework.data.redis.core.RedisTemplate; 8 import org.springframework.data.redis.core.ValueOperations; 9 import org.springframework.stereotype.Service; 10 import org.springframework.transaction.annotation.Transactional; 11 12 import java.util.List; 13 14 @Service 15 @Transactional 16 public class StudentServiceImpl implements StudentService{ 17 @Autowired 18 StudentMapper studentMapper; 19 20 @Autowired 21 RedisTemplate redisTemplate; 22 23 @Override 24 public List<Student> findAll() { 25 String key="student"; 26 ListOperations<String,Student> operations= redisTemplate.opsForList(); 27 28 //缓存存在 29 if (redisTemplate.hasKey(key)){ 30 return operations.range(key,0,-1); 31 }else{ 32 //得到学生集合 33 List<Student> list=studentMapper.findAll(); 34 operations.leftPushAll(key,list); 35 return list; 36 } 37 } 38 39 @Override 40 public Student findBySid(Integer sid) { 41 String key = "student_" + sid; 42 ValueOperations<String, Student> operations = redisTemplate.opsForValue(); 43 44 //缓存存在 45 if (redisTemplate.hasKey(key)) { 46 System.out.println("redis-siddata"); 47 return operations.get(key); 48 } else { 49 //得到学生对象 50 Student student = studentMapper.findBySid(sid); 51 //添加到缓存 52 operations.set(key, student); 53 System.out.println("student-siddata"); 54 return student; 55 } 56 } 57 58 @Override 59 public int del(Integer sid) { 60 //删除数据库中的数据 61 int count = studentMapper.del(sid); 62 63 //缓存存在 64 String key = "student_" + sid; 65 if (redisTemplate.hasKey(key)) { 66 //删除对应缓存 67 redisTemplate.delete(key); 68 } 69 return count; 70 } 71 72 @Override 73 public int modify(Student student) { 74 //修改数据库中的数据 75 int count = studentMapper.modify(student); 76 77 ValueOperations operations = redisTemplate.opsForValue(); 78 //缓存存在 79 String key = "student_" + student.getSid(); 80 if (redisTemplate.hasKey(key)) { 81 //更新缓存 82 Student stu = studentMapper.findBySid(student.getSid()); 83 operations.set(key, stu); 84 } 85 return count; 86 } 87 88 @Override 89 public int add(Student student) { 90 //添加数据 91 int count = studentMapper.add(student); 92 return count; 93 } 94 }
创建控制层StudentController.java
1 package cn.kgc.controller; 2 3 import cn.kgc.service.StudentService; 4 import cn.kgc.vo.Student; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.web.bind.annotation.*; 7 8 import java.util.List; 9 10 @RestController 11 public class StudentController { 12 13 @Autowired 14 StudentService studentService; 15 16 @GetMapping("/student") 17 public List<Student> findAll() { 18 return studentService.findAll(); 19 } 20 21 /* @GetMapping("/student/{sid}") 22 public Student findById(@PathVariable("sid") Integer sid) { 23 return studentService.findBySid(sid); 24 } 25 */ 26 @GetMapping("/findById") 27 public Student findById(Integer sid) { 28 return studentService.findBySid(sid); 29 } 30 31 @PostMapping("/student/{sid}") 32 public int del(@PathVariable("sid") Integer sid) { 33 return studentService.del(sid); 34 } 35 36 @PutMapping("/student") 37 public int modify(Student student) { 38 return studentService.modify(student); 39 } 40 41 }
启动类
1 package cn.kgc; 2 3 import org.mybatis.spring.annotation.MapperScan; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 //指定要扫描的mapper包 7 @MapperScan("cn.kgc.mapper") 8 @SpringBootApplication 9 public class SpringbootMybatisRedisApplication { 10 11 public static void main(String[] args) { 12 SpringApplication.run(SpringbootMybatisRedisApplication.class, args); 13 } 14 15 }
测试如下:
第一次访问
查看redis
第二次访问
以上是关于6.springboot+mybatis+redis整合的主要内容,如果未能解决你的问题,请参考以下文章
黑马Java笔记+踩坑汇总JavaSE+JavaWeb+SSM+SpringBoot+瑞吉外卖+SpringCloud/SpringCloudAlibaba+黑马旅游+谷粒商城