前序:
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