前序:
redis是一个缓存,消息代理和功能丰富的键值存储系统。Spring Boot为Spring Data Redis提供的Jedis和Lettuce客户端库和抽象提供了基本的自动配置。有一个spring-boot-starter-data-redis“Starter”用于以默认方式使用Jedis的方便方式收集依赖关系。
正题之mysql数据库建表
数据库名:ms
1.建user表
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(40) DEFAULT NULL,
`password` varchar(40) DEFAULT NULL,
`sex` varchar(5) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;2.向user表里插入一条数据
INSERT INTO USER (id,username,PASSWORD,sex) VALUES ('2','李四','123456','男');
正题之建立maven项目
1 .pom文件
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>JIXING</artifactId>
<groupId>com.jixing</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jixing-consumer-xuanke</artifactId>
<dependencies>
<!-- 配置快速启动 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--整合web (springmvc) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot整合redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
<!-- MYSQL依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId><!--mysql驱动包 -->
</dependency>
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId><!--boot整合mybatis的关键包annotation -->
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.application.xml
server.port=7902
logging.level.com.xuanke.dao= trace
logging.level.org.springframework.web = info
logging.level.com.xuanke=debug
#JPA configure
spring.datasource.url = jdbc:mysql://localhost:3306/msm
spring.datasource.username = root
spring.datasource.password = 172.16.77.181
spring.datasource.driverClassName = com.mysql.jdbc.Driver
#redis 配置
#redis 配置数据库序列号(0~15)
spring.redis.database=0
#redis 配置主机名
spring.redis.host=192.168.0.98
#redis 配置主机名
spring.redis.port=6379
#redis 配置连接密码(默认为空,根据自己情况而定)
spring.redis.password=
#redis 配置连接池的最大数量(使用负值表示没有限制)
spring.redis.pool.max-active=24
#redis 配置连接池最大空闲连接数
spring.redis.pool.max-idle=8
#redis 配置连接池的最大等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
#redis 配置连接池最小空间数
spring.redis.pool.min-idle=0
#redis 配置连接超时时间 单位为毫秒
spring.redis.timeout=6000
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# DDL mode. This is actually a shortcut for the "hibernate.hbm2ddl.auto" property. Default to "create-drop" when using an embedded database, "none" otherwise.
spring.jpa.hibernate.ddl-auto = update
# Hibernate 4 naming strategy fully qualified name. Not supported with Hibernate 5.
spring.jpa.hibernate.naming.strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.application.name=jixing-consumer-xuanke
3.项目架构
4.UserServieImpl代码
@Repository
public class UserServiceImpl implements UserService{
private static final Logger LOGGER=LoggerFactory.getLogger(UserServiceImpl.class);
@Autowired
private UserDao userDao;
@Autowired
private RedisTemplate redisTemplate;
@Override
public User findUserById(int id) {
//从redis 缓存里获取城市信息
String key="user_"+id;
ValueOperations<String, User> opsForValue = redisTemplate.opsForValue();
//缓存是否存在
boolean haskey=redisTemplate.hasKey(key);
User user;
if(haskey){
user=opsForValue.get(key);
}else
{
//否则从db中取
user = userDao.findUser(id);
}
//插入到缓存里
opsForValue.set(key, user,10,TimeUnit.SECONDS);
LOGGER.info("UserServiceImpl.findUserById():用户插入缓存",user.toString());
return user;
}
}5.UserController
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping(value="/test")
public User test(){
return userService.findUserById(1);
}
}
至于Dao代码在这里就不再赘述,若有需要请看下面的网址:https://github.com/241600489/jixing-consumer-xuanke

本文介绍了一个使用SpringBoot结合Redis缓存和MySQL数据库的示例项目,包括搭建环境、配置文件详解及核心代码实现。
1925

被折叠的 条评论
为什么被折叠?



