先下载redis的数据库
github下载redis
再下载desktop的操作界面
Gitee下载Redis的操作界面
启动服务
第一次访问先将数据从数据库拿出来,再放入Redis中,下次再拿同样的数据可以直接先在Redis中寻找
下面是进行的一些测试代码
package com.xuda.model;
public class Books {
private Integer bookid;
private String bookname;
private Integer bookcounts;
private String detail;
public Integer getBookid() {
return bookid;
}
public void setBookid(Integer bookid) {
this.bookid = bookid;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public Integer getBookcounts() {
return bookcounts;
}
public void setBookcounts(Integer bookcounts) {
this.bookcounts = bookcounts;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
}
package com.xuda.mapper;
import com.xuda.model.Books;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface BooksMapper {
int insert(Books record);
int insertSelective(Books record);
int select();
}
<mapper namespace="com.xuda.mapper.BooksMapper">
<resultMap id="BaseResultMap" type="com.xuda.model.Books">
<result column="bookID" jdbcType="INTEGER" property="bookid" />
<result column="bookName" jdbcType="VARCHAR" property="bookname" />
<result column="bookCounts" jdbcType="INTEGER" property="bookcounts" />
<result column="detail" jdbcType="VARCHAR" property="detail" />
resultMap>
<insert id="insert" parameterType="com.xuda.model.Books">
insert into books (bookID, bookName, bookCounts,
detail)
values (#{bookid,jdbcType=INTEGER}, #{bookname,jdbcType=VARCHAR}, #{bookcounts,jdbcType=INTEGER},
#{detail,jdbcType=VARCHAR})
insert>
<insert id="insertSelective" parameterType="com.xuda.model.Books">
insert into books
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="bookid != null">
bookID,
if>
<if test="bookname != null">
bookName,
if>
<if test="bookcounts != null">
bookCounts,
if>
<if test="detail != null">
detail,
if>
trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="bookid != null">
#{bookid,jdbcType=INTEGER},
if>
<if test="bookname != null">
#{bookname,jdbcType=VARCHAR},
if>
<if test="bookcounts != null">
#{bookcounts,jdbcType=INTEGER},
if>
<if test="detail != null">
#{detail,jdbcType=VARCHAR},
if>
trim>
insert>
<select id="select" resultType="java.lang.Integer">
select count(*) from books
select>
mapper>
package com.xuda.Service;
import com.xuda.model.Books;
/**
* @author :程序员徐大大
* @description:TODO
* @date :2022-03-19 15:39
*/
public interface BookService {
int insert(Books record);
int insertSelective(Books record);
int select();
}
package com.xuda.Service.impl.Impl;
import com.xuda.Service.BookService;
import com.xuda.mapper.BooksMapper;
import com.xuda.model.Books;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
/**
* @author :程序员徐大大
* @description:TODO
* @date :2022-03-19 15:42
*/
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BooksMapper booksMapper;
@Autowired
private RedisTemplate<Object,Object> redisTemplate;
@Override
public int insert(Books books) {
return booksMapper.insert(books);
}
@Override
public int insertSelective(Books books) {
return booksMapper.insertSelective(books);
}
//需要注入redis模板
@Override
public int select() {
//更新redistemplate 对象中spring容器中key的序列化方式
redisTemplate.setKeySerializer(new StringRedisSerializer());
//1. 先去redis缓存查看
/*Integer allStudentCount = (Integer) redisTemplate.opsForValue().get("allStudentCount");
//判断是否有值
if (allStudentCount == null ) {
//如果没有值就去数据库查询
System.out.println("从数据库查询");
allStudentCount = booksMapper.select();
//将查询的结果放入redis缓存中
//其中每隔十五秒清一次redis缓存
redisTemplate.opsForValue().set("allStudentCount",allStudentCount,15, TimeUnit.SECONDS);
} else {
System.out.println("从redis里面取出");
}*/
//上面的代码中多线程和高并发情况下会出现一种现象:缓存穿透
//需要通过双重检测+同步代码块来解决以上出现的问题
//先判断redis缓存取学生人数
Integer allStudentCount = (Integer) redisTemplate.opsForValue().get("allStudentCount");
if (allStudentCount == null) {
//设置同步代码快
synchronized (this){
//再次从redis中获取学生人数
allStudentCount = (Integer) redisTemplate.opsForValue().get("allStudentCount");
//再次判断是否为空
if (allStudentCount == null) {
System.out.println("从数据库进行查询 进入到mysql");
//从数据库进行查询
allStudentCount = booksMapper.select();
//放到redis缓存中
//设置缓存15后清除
redisTemplate.opsForValue().set("allStudentCount",allStudentCount,15, TimeUnit.SECONDS);
} else {
System.out.println("从redis中查询到数据没查到");
}
}
} else {
System.out.println("从redis查询");
}
//有就直接返回
return allStudentCount;
}
}
package com.xuda.Controller;
import com.xuda.Service.BookService;
import com.xuda.model.Books;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author :程序员徐大大
* @description:TODO
* @date :2022-03-19 15:49
*/
@Controller
@RequestMapping(value = "books")
public class ControllerBook {
@Autowired
private BookService bookService;
@RequestMapping(value = "findbook")
public @ResponseBody Object findbook(){
LinkedList<Object> objects = new LinkedList<>();
Books books = new Books();
books.setBookname("徐大大");
books.setBookcounts(200);
books.setDetail("nihao");
int i = bookService.insertSelective(books);
return i;
}
@RequestMapping(value = "/index")
public @ResponseBody Object index() {
return "hello spring easy to use!";
}
@RequestMapping(value = "/count")
public @ResponseBody Object studentCount(){
//固定一个线程池
ExecutorService executorService = Executors.newFixedThreadPool(100);
for (int i = 0; i < 10; i++) {
//开启一个线程
executorService.submit(new Runnable() {
@Override
public void run() {
Integer allStuentCount =bookService.select();
}
});
}
executorService.shutdown();
Integer allStudentCount = bookService.select();
return "学生总人数为" + allStudentCount;
}
}
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssmbuild?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=
#&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.6.4version>
<relativePath/>
parent>
<groupId>com.xudagroupId>
<artifactId>003-springboot-mybatisartifactId>
<version>1.0.0version>
<name>003-springboot-mybatisname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.48version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.2.2version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
includes>
resource>
resources>
<plugins>
<plugin>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-maven-pluginartifactId>
<version>1.3.6version>
<configuration>
<configurationFile>GeneratorMapper.xmlconfigurationFile>
<verbose>trueverbose>
<overwrite>trueoverwrite>
configuration>
plugin>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>