目的:提高查询效率,减少数据库压力
test:
//根据gid查询goods @Test public void testSelGoodsByGid(){ GoodsDao mapper = session.getMapper(GoodsDao.class); Goods goods = mapper.selGoodsByGid(4); System.out.println(goods); System.out.println("----------------"); //再次根据同一个条件查询 Goods goods1 = mapper.selGoodsByGid(4); //不会再次调用sql语句,因为mybatis默认开启了一级缓存 System.out.println(goods1); System.out.println("================"); //清除缓存 session.clearCache(); //再次根据同一个条件查询 Goods goods2 = mapper.selGoodsByGid(4); //需要调用sql语句 System.out.println(goods2); }
log4j:
DEBUG [main] - ==> Preparing: select * from t_goods WHERE gid = ? DEBUG [main] - ==> Parameters: 4(Integer) DEBUG [main] - <== Total: 1 Goods{gid=4, gname='冰箱', gprice=3155.55, gcount=30, gdate=Sat Aug 15 00:00:00 CST 2020} ---------------- Goods{gid=4, gname='冰箱', gprice=3155.55, gcount=30, gdate=Sat Aug 15 00:00:00 CST 2020} ================ DEBUG [main] - ==> Preparing: select * from t_goods WHERE gid = ? DEBUG [main] - ==> Parameters: 4(Integer) DEBUG [main] - <== Total: 1 Goods{gid=4, gname='冰箱', gprice=3155.55, gcount=30, gdate=Sat Aug 15 00:00:00 CST 2020}