v阅读目录
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
新增AI编程课程,引领技术教育新趋势
v阅读目录
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
激活启动类注解@EnableCaching

2.1 添加service层
package com.demo.service; import com.demo.pojo.UserDetails; /** * Created by toutou on 2019/1/20. */public interface CacheService { UserDetails getUserDetailsByUid(int uid); UserDetails updateUserInfo(UserDetails userDetails); int delUserInfoById(int uid); }
package com.demo.service; import com.demo.dao.UserDetailsMapper; import com.demo.pojo.UserDetails; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; /** * Created by toutou on 2019/1/20. */ @Service public class CacheServiceImpl implements CacheService{ @Autowired UserDetailsMapper userDetailsMapper; @Override @Cacheable(value = "user_details", key = "#uid", unless="#result == null") public UserDetails getUserDetailsByUid(int uid){ System.out.println(" Cacheable 有请求过来了"); UserDetails userDetails = userDetailsMapper.getUserDetailsByUid(uid); return userDetails; } @Override @CachePut(value = "user_details", key = "#user.id") public UserDetails updateUserInfo(UserDetails user){ System.out.println(" CachePut 有请求过来了"); if(userDetailsMapper.updateByPrimaryKeySelective(user) > 0) { // 这里也可以直接在updateByPrimaryKeySelective的方法里,修改后直接查询出该记录返回UserDetails实例,看需求。 user = userDetailsMapper.getUserDetailsByUid(user.getId()); return user; }else{