首页>代码>spring+springmvc+mybatis+redis使用注解配置缓存实例>/ssmredis/src/main/java/com/ssm/serviceImpl/UserServiceImpl.java
package com.ssm.serviceImpl;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.ssm.dao.UserMapper;
import com.ssm.pojo.User;
import com.ssm.service.IUserService;

/**
 * userService
 * 
 * 缓存机制说明:所有的查询结果都放进了缓存,也就是把MySQL查询的结果放到了redis中去,
 * 然后第二次发起该条查询时就可以从redis中去读取查询的结果,从而不与MySQL交互,从而达到优化的效果,
 * redis的查询速度之于MySQL的查询速度相当于 内存读写速度 /硬盘读写速度 
 * @Cacheable("a")注解的意义就是把该方法的查询结果放到redis中去,下一次再发起查询就去redis中去取,存在redis中的数据的key就是a;
 * @CacheEvict(value={"a","b"},allEntries=true) 的意思就是执行该方法后要清除redis中key名称为a,b的数据;
 */
@Service("userService")
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)  
public class UserServiceImpl implements IUserService {

    @Resource
    private UserMapper iUserDao;

    @Cacheable("getUserById") //标注该方法查询的结果进入缓存,再次访问时直接读取缓存中的数据
    @Override
    public User getUserById(int userId) {
    
        return this.iUserDao.selectByPrimaryKey(userId);
    }

    @Cacheable("getAllUser")
    @Override
    public List<User> getAllUser() {
    	System.out.println("执行getUserById");
        return this.iUserDao.selectAllUser();
    }

    @CacheEvict(value= {"getAllUser","getUserById","findUsers"},allEntries=true)//清空缓存,allEntries变量表示所有对象的缓存都清除
    @Override
    public void insertUser(User user) {
        this.iUserDao.insertUser(user);
    }

    @CacheEvict(value= {"getAllUser","getUserById","findUsers"},allEntries=true)
    @Override
    public void deleteUser(int id) {
        this.iUserDao.deleteUser(id);
    }

    @Cacheable("findUsers")
    @Override
    public List<User> findUsers(String keyWords) {
        return iUserDao.findUsers(keyWords);
    }

    @CacheEvict(value= {"getAllUser","getUserById","findUsers"},allEntries=true)
    @Override
    public void editUser(User user) {
        this.iUserDao.editUser(user);
    }
}
最近下载更多
xiaot99  LV7 2021年12月22日
huangxiaoke20  LV17 2021年7月7日
peterliu  LV3 2020年12月3日
qimeila  LV5 2020年9月24日
泪染珍珠  LV9 2020年9月7日
Apengwen  LV2 2020年8月14日
521冰521  LV4 2020年7月16日
波波1554733592  LV6 2020年7月2日
huasir2018  LV14 2020年6月12日
huming  LV4 2020年4月21日
最近浏览更多
一瓶儿旺仔  LV1 2023年3月3日
LITIANYU084414  LV11 2023年1月1日
微信网友_6248713511227392  LV11 2022年12月5日
netkill  LV2 2022年10月18日
qq1466625431  LV6 2022年6月28日
and123456  LV11 2022年3月16日
胜过这首歌  LV2 2022年3月10日
吃草的兔子 2022年2月16日
暂无贡献等级
clouduser  LV1 2022年1月3日
xiaot99  LV7 2021年12月22日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友