引入这三个依赖器创建项目,在项目pom.xml文件会出现以下依赖:
2、编写数据库表对应的实体类,并使用JPA相关注解配置映射关系
复制代码
package com.hardy.springbootdatacache.entity;
import org.springframework.data.annotation.Id;
import javax.persistence.*;
/**
-
@Author: HardyYao
-
@Date: 2021/6/19
*/
@Entity(name = “t_comment”) // 设置ORM实体类,并指定映射的表名
public class Comment {@Id // 映射对应的主键id
@GeneratedValue(strategy = GenerationType.IDENTITY) // 设置主键自增策略
private Integer id;private String content;
private String author;
@Column(name = “a_id”) // 指定映射的表字段名
private Integer aId;public Integer getId() {
return id;
}public void setId(Integer id) {
this.id = id;
}public String getContent() {
return content;
}public void setContent(String content) {
this.content = content;
}public String getAuthor() {
return author;
}public void setAuthor(String author) {
this.author = author;
}public Integer getaId() {
return aId;
}public void setaId(Integer aId) {
this.aId = aId;
}@Override
public String toString() {
return “Comment{” +
“id=” + id +
“, content=’” + content + ‘’’ +
“, author=’” + author + ‘’’ +
“, aId=” + aId +
‘}’;
}
}
复制代码
3、编写数据库操作的Repository接口文件
复制代码
package com.hardy.springbootdatacache.repository;
import com.hardy.springbootdatacache.entity.Comment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;
/**
-
@Author: HardyYao
-
@Date: 2021/6/19
*/
public interface CommentRepository extends JpaRepository<Comment, Integer> {/**
- 根据评论id修改评论作者author
- @param author
- @param id
- @return
*/
@Transactional
@Modifying
@Query(“update t_comment c set c.author = ?1 where c.id=?2”)
int updateComment(String author,Integer id);
}
复制代码
4、编写service层
复制代码
package com.hardy.springbootdatacache.service;
import com.hardy.springbootdatacache.entity.Comment;
import com.hardy.springbootdatacache.repository.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.Optional;
/**
-
@Author: HardyYao
-
@Date: 2021/6/19
*/
@Service
public class CommentService {@Autowired
private CommentRepository commentRepository;/**
- 根据评论id查询评论
- @Cacheable:将该方法的查询结果comment存放在SpringBoot默认缓存中
- cacheNames:起一个缓存命名空间,对应缓存唯一标识
- @param id
- @return
*/
@Cacheable(cacheNames = “comment”)
public Comment findCommentById(Integer id){
Optional comment = commentRepository.findById(id);
if(comment.isPresent()){
Comment comment1 = comment.get();
return comment1;
}
return null;
}
}
复制代码
5、编写controller层
复制代码
package com.hardy.springbootdatacache.controller;
import com.hardy.springbootdatacache.entity.Comment;
import com.hardy.springbootdatacache.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
-
@Author: HardyYao
-
@Date: 2021/6/19
*/
@RestController
public class CommentController {@Autowired
private CommentService commentService;@RequestMapping(value = “/findCommentById”)
public Comment findCommentById(Integer id){
Comment comment = commentService.findCommentById(id);
return comment;
}
}
复制代码
6、编写配置文件
在全局配置文件application.properties中编写对应的数据库连接配置
复制代码
MySQL数据库连接配置
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdata?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
显示使用JPA进行数据库查询的SQL语句
spring.jpa.show-sql=true
开启驼峰命名匹配映射
mybatis.configuration.map-underscore-to-camel-case=true
解决中文乱码问题
spring.http.encoding.force-response=true
复制代码
7、测试
在浏览器中输入:http://localhost:8080/findCommentById?id=1 进行访问(连续访问三次):
在上图中,因为没有在SpringBoot项目中开启缓存管理,故虽然数据表中的数据没有任何变化,但是每执行一次查询操作,即便执行的是相同的SQL语句,都还是会访问一次数据库。
2、默认缓存使用
在前面搭建的Web应用的基础上,开启SpringBoot默认支持的缓存,以使用SpringBoot默认缓存。
1、在项目启动类的类名上方使用@EnableCaching注解开启基于注解的缓存支持
复制代码
package com.hardy.springbootdatacache;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@EnableCaching // 开启SpringBoot基于注解的缓存管理支持
@SpringBootApplication
public class SpringbootdataCacheApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootdataCacheApplication.class, args);
}
}
复制代码
2、使用@Cacheable注解对数据操作方法进行缓存管理
将@Cacheable注解标注在Service类的查询方法上,对查询结果进行缓存:
复制代码
package com.hardy.springbootdatacache.service;
import com.hardy.springbootdatacache.entity.Comment;
import com.hardy.springbootdatacache.repository.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.Optional;
/**
-
@Author: HardyYao
-
@Date: 2021/6/19
*/
@Service
public class CommentService {@Autowired
private CommentRepository commentRepository;/**
- 根据评论id查询评论
- @param id
- @return
*/
@Cacheable(cacheNames = “comment”)
public Comment findCommentById(Integer id){
Optional comment = commentRepository.findById(id);
if(comment.isPresent()){
Comment comment1 = comment.get();
return comment1;
}
return null;
}
}
USB Microphone https://www.soft-voice.com/
Wooden Speakers https://www.zeshuiplatform.com/
亚马逊测评 www.yisuping.cn
深圳网站建设www.sz886.com