SpringBoot与Redis的整合
一、导入坐标
<!--这里与数据库进行了结合所以引入了数据库相关坐标-->
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis坐标-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
二、编写application.yaml文件
# 应用名称
spring.application.name=demo1
# 应用服务 WEB 访问端口
server.port=9090
# 指定mapper.xml的位置
mybatis.mapper-locations=classpath*:com/redisdemo/demo1/mapper/xml
三、编写测试demo
package com.redisdemo.demo1.controller;
import com.redisdemo.demo1.mapper.UserMapper;
import com.redisdemo.demo1.pojo.User;
import com.redisdemo.demo1.service.UserService;
import com.redisdemo.demo1.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.Random;
@RestController
@Api(tags = "用户管理")
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@ApiOperation("数据库初始化100条数据")
@RequestMapping(value = "/init",method = RequestMethod.GET)
public void init(){
for (int i = 0; i < 100; i++) {
Random rand = new Random();
User user = new User();
String temp = "un" + i;
user.setUsername(temp);
user.setPassword(temp);
int n = rand.nextInt(2);
user.setSex(n);
userService.createUser(user);
}
}
@ApiOperation("单个用户查询,按照userId进行查询")
@RequestMapping(value = "/findById/{id}",method = RequestMethod.GET)
public UserVo findById(@PathVariable Integer id){
User user = userService.findUserById(id);
UserVo userVo = new UserVo();
BeanUtils.copyProperties(user,userVo);
return userVo;
}
@ApiOperation("修改某条数据")
@RequestMapping(value = "/updateUser",method = RequestMethod.POST)
public void updateUser(@RequestBody UserVo userVo){
User user = new User();
BeanUtils.copyProperties(userVo,user);
userService.updateUser(user);
}
}
=================================================
public interface UserService {
void createUser(User obj);
void updateUser(User obj);
User findUserById(Integer userid);
}
=================================================
@Service
public class UserServiceImpl implements UserService {
private static final Logger logger= LoggerFactory.getLogger(UserServiceImpl.class);
private static final String CACHE_KEY_USER="user:";
@Autowired
private UserMapper userMapper;
@Autowired
private RedisTemplate redisTemplate;
@Override
public void createUser(User obj) {
this.userMapper.insert(obj);
String key=CACHE_KEY_USER+obj.getId();
User user = this.userMapper.selectByPrimaryKey(obj.getId());
redisTemplate.opsForValue().set(key,user);
}
@Override
public void updateUser(User obj) {
userMapper.updateByPrimaryKey(obj);
User user = userMapper.selectByPrimaryKey(obj.getId());
String key=CACHE_KEY_USER+obj.getId();
redisTemplate.opsForValue().set(key,user);
}
@Override
public User findUserById(Integer userid) {
ValueOperations<String,User> valueOperations = redisTemplate.opsForValue();
User user = valueOperations.get(CACHE_KEY_USER+userid);
if(Objects.nonNull(user)){
return user;
}
User user1 = userMapper.selectByPrimaryKey(userid);
String key=CACHE_KEY_USER+user1.getId();
valueOperations.set(key,user1);
return user1;
}
}
==============================================
@ApiModel(value = "用户信息类")
public class UserVo {
@ApiModelProperty(value = "用户id")
private Integer id;
@ApiModelProperty(value = "用户姓名")
private String username;
@ApiModelProperty(value = "用户密码")
private String password;
@ApiModelProperty(value = "用户性别")
private Integer sex;
@ApiModelProperty(value = "是否删除:0:未删除 1:已删除")
private Integer deleted;
@ApiModelProperty(value = "更新时间")
private Date updateTime;
@ApiModelProperty(value = "创建时间")
private Date createTime;
}
=============================================
package com.redisdemo.demo1.pojo;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;
@Table(name = "users")
public class User implements Serializable {
private static final Long UUID=1L;
@Id
@GeneratedValue(generator = "JDBC")
private Integer id;
private String username;
private String password;
private Integer sex;
private Integer deleted;
@Column(name = "update_time")
private Date updateTime;
@Column(name = "create_time")
private Date createTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public Integer getDeleted() {
return deleted;
}
public void setDeleted(Integer deleted) {
this.deleted = deleted;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", sex=" + sex +
", deleted=" + deleted +
", updateTime=" + updateTime +
", createTime=" + createTime +
'}';
}
}
四、启动后出现的bug
1.由于User类要放到redis缓存中,User类要实现Serializable接口,否则会报错
2.Redis中的配置文件Redis.conf中将这里修改为bind 0.0.0.0,否则会报
java.net.ConnectException: Connection refused: no further information错误
3.在这里注意一点数据库中表字段名字不能叫delete,这个是一个关键字不能当做字段进行使用。
|