spring boot整合redis

前序:

    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文件



    
        JIXING
        com.jixing
        1.0-SNAPSHOT
    
    4.0.0

    jixing-consumer-xuanke
    
    
        
            org.springframework.boot
            spring-boot-starter
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
       
        org.springframework.boot
        spring-boot-starter-redis
      
        
        
            mysql
            mysql-connector-java
        
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        
        
            com.h2database
            h2
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

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 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
























 
  

你可能感兴趣的:(Spring,Boot,redis)