7.springboot+mybatis+redis整合

 

選擇生成的依賴java

 

 

選擇保存的工程路徑mysql

 

 

查詢已經生成的依賴,並修改mysql的版本web

 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>
pom.xml

 

項目結構以下:redis

 

 

 

編輯屬性文件application.propertiesspring

 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
application.properties

 

 

建立持久化類Student.javasql

 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 }
Student.java

 

建立數據訪問層接口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 }
StudentMapper.java

 

建立業務層接口StudentService.javaapache

 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 }
StudentService.java

 

建立業務層接口實現類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 }
StudentServiceImpl.java

 

 

建立控制層StudentController.javaspringboot

 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 }
StudentController.java

 

啓動類

 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

 

 

 

 第二次訪問

 

 

相關文章
相關標籤/搜索