springboot-mybatits数据库,ecache缓存操作,批量操作,事物操作

springboot-mybatits数据库,ecache缓存操作,批量操作,事物操作


代码下载: https://github.com/2010yhh/springBoot-demos/tree/master/springboot-static
环境

idea2018,jdk1.8,

springboot版本:1.5.9.RELEASE

1.mybatits,数据库ecache缓存操作

ecache配置:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" name="shiroCache">
	<diskStore path="java.io.tmpdir"/>
	<defaultCache
			maxElementsInMemory="10000"
			eternal="false"
			timeToIdleSeconds="120"
			timeToLiveSeconds="120"
			overflowToDisk="false"
			diskPersistent="false"
			diskExpiryThreadIntervalSeconds="120"
	/>
	<!-- 表示此缓存最多可以存活2分钟,如果期间超过1分钟未访问 那么此缓存失效-->
	<cache name="user"
		   maxEntriesLocalHeap="2000"
		   eternal="false"
		   timeToIdleSeconds="60"
		   timeToLiveSeconds="120"
		   overflowToDisk="false"
		   statistics="true">
	</cache>
</ehcache>

使用:

@Service
@CacheConfig(cacheNames = {"user"})
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Cacheable(key = "#userName")
    @Override
    public List<User> findByUserName(String userName, String passWord) {
        UserExample example = new UserExample();
        example.createCriteria().andUserNameEqualTo(userName).andPassWordEqualTo(passWord);
        return userMapper.selectByExample(example);
    }

    @CachePut(key = "#user.userId")
    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void addUser(User user) {
        //测试事物特性
        userMapper.insertSelective(user);
         //userMapper.insertSelective(user);
    }

}

测试时,设置缓存30s,(timeToIdleSeconds=“30”
timeToLiveSeconds=“30”)在缓存有效期内查询:从日志可看出,只查询数据库一次。

在这里插入图片描述

2.mybatits,数据库批量操作:增删查改

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值