springboot结合mysql和redis数据库进行数据读取和缓存的demo
结尾有项目代码的gitee下载路径。
依赖的导入,本案例需要的依赖在下方
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
数据表sql
/*
Navicat MySQL Data Transfer
Source Server : MySQL80
Source Server Version : 80026
Source Host : 127.0.0.1:3306
Source Database : cheshi
Target Server Type : MYSQL
Target Server Version : 80026
File Encoding : 65001
Date: 2022-05-05 20:46:24
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for users
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`user` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`jurisdiction` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
-- ----------------------------
-- Records of users
-- ----------------------------
INSERT INTO `users` VALUES ('admin', '1234', 'admin11');
INSERT INTO `users` VALUES ('hhh', 'hhh', 'hhh');
INSERT INTO `users` VALUES ('admins', 'hhhj', 'hhhjjj');
application.properties文件需要添加的内容
spring.datasource.url=jdbc:mysql://localhost:3306/cheshi?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.schema= classpath:sql/cheshi.sql
#spring.datasource.initialization-mode=always
spring.redis.host= localhost
spring.redis.port= 6379
spring.redis.password=123456
spring.cache.type=redis
spring.redis.timeout=10000
mybatis.mapper-locations= classpath:mybatis/*.xml
mybatis文件代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hsq.hdfs.dao.UserDao">
<select id="get" resultType="com.hsq.hdfs.domain.userDo">
select `user`,`password`,`jurisdiction` from users where user = #{user}
</select>
<select id="list" resultType="com.hsq.hdfs.domain.userDo">
select
`user`,`password`,`jurisdiction`
from users
</select>
<insert id="save" parameterType="com.hsq.hdfs.domain.userDo" useGeneratedKeys="true" keyProperty="user">
insert into users
(
`user`,`password`,`jurisdiction`
)
values
(
#{user},#{password},#{jurisdiction}
)
</insert>
<delete id="remove">
delete from users where user = #{user}
</delete>
<update id="update">
update users set password = #{password} where user = #{user}
</update>
</mapper>
dao文件夹编写mapper映射代码(文件名UserDao)
@Mapper
public interface UserDao {
userDo get(String user);
List<userDo> list();
int save(userDo GoodsContent);
void update(@Param("user") String user, @Param("password") String password);
int remove(String user);
}
domain文件夹代码(文件名userDo)
public class userDo implements Serializable {
private String user;
private String password;
private String jurisdiction;
public void setUser(String user) {
this.user = user;
}
public void setPassword(String password) {
this.password = password;
}
public void setJurisdiction(String jurisdiction) {
this.jurisdiction = jurisdiction;
}
public String getUser() {
return user;
}
public String getPassword() {
return password;
}
public String getJurisdiction() {
return jurisdiction;
}
}
服务层代码
@Service
public class userService {
@Autowired
private UserDao UserDao;
// @Cacheable( value="userDo",unless = "#result==null",key="#hhh")
@Cacheable(cacheNames = "userDo", unless = "#result==null")
public userDo get(String user){
System.out.println("get");
if(UserDao.get(user)!=null){
return UserDao.get(user);
}
return null;
}
@CacheEvict(cacheNames = "userDo", beforeInvocation=true)
public int remove(String user){
System.out.println("remove");
return UserDao.remove(user);
}
@Cacheable(cacheNames = "userDo", key = "#GoodsContent.user")
// @Cacheable( value="userDo",unless = "#result==null",key="#GoodsContent.user")
public userDo save(userDo GoodsContent){
UserDao.save(GoodsContent);
System.out.println("save");
return UserDao.get(GoodsContent.getUser());
}
@CachePut(cacheNames = "userDo", key = "#user")
public userDo update(@Param("user") String user, @Param("password") String password){
UserDao.update(user,password);
System.out.println("update");
return UserDao.get(user);
}
}
控制层代码
@RestController
public class Controller {
@Autowired
private com.hsq.hdfs.server.userService userService;
@GetMapping("/get")
public userDo get(@RequestParam("user")String user) {
return userService.get(user);
}
@PostMapping("/del")
public int del(@RequestParam("user")String user) {
return userService.remove(user);
}
@PostMapping("/edit")
public userDo edit(@RequestBody userDo userDo){
return userService.save(userDo);
}
@PostMapping("/up")
public userDo up(@RequestParam("user")String user,@RequestParam("password")String password) {
return userService.update(user,password);
}
}
运行结果
代码补充说明,服务层函数内的print输出目的是让用户明白缓存的作用,当redis内读取到数据时,函数是不会运行的,也就不会有print的打印,同时redis数据的更新使用的是@CachePut。缓存的数据为函数的返回结果。具体用法可以自行百度学习。本文只是提供一个小小的demo,为大家提供一个代码案例,方便大家的学习。
项目gitee下载路径:https://gitee.com/hsq111/redisdemo.git
|