一:一级缓存:
一级缓存在mybatis中默认是开启的并且是session级别,它的作用域为一次sqlSession会话。
1 import com.smartdata.pms.PmsApplication; 2 import com.smartdata.pms.entity.PmsProduct; 3 import com.smartdata.pms.mapper.PmsProductMapper; 4 import com.smartdata.pms.service.PmsProductService; 5 import org.junit.Test; 6 import org.junit.runner.RunWith; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.boot.test.context.SpringBootTest; 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 10 import org.springframework.transaction.annotation.Transactional; 11 12 import java.util.concurrent.LinkedBlockingQueue; 13 import java.util.concurrent.ThreadPoolExecutor; 14 import java.util.concurrent.TimeUnit; 15 16 /** 17 * @ProjectName: smartdata 18 * @Package: PACKAGE_NAME 19 * @ClassName: CacheTest 20 * @Author: heluwei 21 * @Description: 缓存测试 22 * @Date: 2020/3/21 18:34 23 * @Version: 1.0 24 */ 25 @RunWith(SpringJUnit4ClassRunner.class) 26 @SpringBootTest(classes = PmsApplication.class) 27 public class CacheTest{ 28 @Autowired 29 PmsProductMapper pmsProductMapper; 30 @Test 31 @Transactional(rollbackFor = Throwable.class) 32 public void testFistCache(){ 33 // 第一次查询,缓存到一级缓存 34 System.out.println(pmsProductMapper.selectById(1)); 35 // 第二次查询,直接读取一级缓存 36 System.out.println(pmsProductMapper.selectById(1)); 37 38 } 39 }