diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 2c035a1..abe39af 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -7,6 +7,7 @@ + diff --git a/.idea/encodings.xml b/.idea/encodings.xml index b2c2558..d873404 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -11,5 +11,7 @@ + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index fe581c9..b59e23c 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -13,6 +13,7 @@ diff --git a/mybatis519/src/main/java/cn/czyx007/mapper/BrandMapper.java b/mybatis519/src/main/java/cn/czyx007/mapper/BrandMapper.java index 9762e08..69d88cd 100644 --- a/mybatis519/src/main/java/cn/czyx007/mapper/BrandMapper.java +++ b/mybatis519/src/main/java/cn/czyx007/mapper/BrandMapper.java @@ -1,6 +1,7 @@ package cn.czyx007.mapper; import cn.czyx007.bean.Brand; +import org.apache.ibatis.annotations.CacheNamespace; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; @@ -10,6 +11,8 @@ import java.util.List; * @author : 张宇轩 * @createTime : 2023/5/17 - 14:16 */ +//开启二级缓存 +@CacheNamespace public interface BrandMapper { //查询所有品牌 @Select("select * from person_info.tb_brand") diff --git a/mybatis519/src/main/resources/cn/czyx007/mapper/BrandMapper.xml b/mybatis519/src/main/resources/cn/czyx007/mapper/BrandMapper.xml index ab99636..7231ab9 100644 --- a/mybatis519/src/main/resources/cn/czyx007/mapper/BrandMapper.xml +++ b/mybatis519/src/main/resources/cn/czyx007/mapper/BrandMapper.xml @@ -5,4 +5,7 @@ + + + \ No newline at end of file diff --git a/mybatis519/src/main/resources/mybatis-config.xml b/mybatis519/src/main/resources/mybatis-config.xml index 099aee1..233b557 100644 --- a/mybatis519/src/main/resources/mybatis-config.xml +++ b/mybatis519/src/main/resources/mybatis-config.xml @@ -20,6 +20,7 @@ + @@ -28,6 +29,7 @@ + diff --git a/mybatis519/src/test/java/TestComputers.java b/mybatis519/src/test/java/TestComputers.java index 04a7280..8440c32 100644 --- a/mybatis519/src/test/java/TestComputers.java +++ b/mybatis519/src/test/java/TestComputers.java @@ -48,6 +48,7 @@ public class TestComputers { BrandMapper mapper = sqlSession.getMapper(BrandMapper.class); mapper.getBrandAll().forEach(System.out::println); + //一级缓存,SqlSession级别 //更新操作会导致缓存失效 // Brand brand = new Brand(); // brand.setId(1); @@ -60,4 +61,42 @@ public class TestComputers { BrandMapper mapper1 = sqlSession.getMapper(BrandMapper.class); mapper1.getBrandAll().forEach(System.out::println); } + + //如果不是同一个SqlSession,一级缓存失效 + //如果不是同一个SqlSession,二级缓存有效,mapper级别 + @Test + public void test04(){ + //第一次 + SqlSession sqlSession = SqlSessionUtil.getSqlSession(); + BrandMapper mapper = sqlSession.getMapper(BrandMapper.class); + mapper.getBrandAll().forEach(System.out::println); + //SqlSession关闭,缓存数据 + sqlSession.close(); + //第二次 + System.out.println("+==========="); + SqlSession sqlSession1 = SqlSessionUtil.getSqlSession(); + BrandMapper mapper1 = sqlSession1.getMapper(BrandMapper.class); + mapper1.getBrandAll().forEach(System.out::println); + } + + @Test + public void test05(){ + //第一次 + SqlSession sqlSession = SqlSessionUtil.getSqlSession(); + BrandMapper mapper = sqlSession.getMapper(BrandMapper.class); + mapper.getBrandAll().forEach(System.out::println); + //更新操作 + Brand brand = new Brand(); + brand.setId(88); + brand.setBrandName("小花88"); + mapper.updateBrandById(brand); + //SqlSession关闭,缓存数据 + sqlSession.close(); + //第二次 + System.out.println("+==========="); + SqlSession sqlSession1 = SqlSessionUtil.getSqlSession(); + BrandMapper mapper1 = sqlSession1.getMapper(BrandMapper.class); + mapper1.getBrandAll().forEach(System.out::println); + } + } diff --git a/spring519/pom.xml b/spring519/pom.xml new file mode 100644 index 0000000..5ed5ff2 --- /dev/null +++ b/spring519/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + + cn.czyx007 + spring519 + 1.0-SNAPSHOT + + + 11 + 11 + UTF-8 + + + + + org.projectlombok + lombok + 1.18.20 + + + junit + junit + 4.13.2 + test + + + org.springframework + spring-context + 5.3.23 + + + + \ No newline at end of file diff --git a/spring519/src/main/java/cn/czyx007/bean/Dog.java b/spring519/src/main/java/cn/czyx007/bean/Dog.java new file mode 100644 index 0000000..8202373 --- /dev/null +++ b/spring519/src/main/java/cn/czyx007/bean/Dog.java @@ -0,0 +1,22 @@ +package cn.czyx007.bean; + +import lombok.AllArgsConstructor; +import lombok.Data; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/19 - 15:59 + */ +@Data +//@NoArgsConstructor +@AllArgsConstructor +public class Dog { + public Dog() { + System.out.println("狗出生了"); + } + + private String name; + public void eat(){ + System.out.println(name + "的狗吃骨头..."); + } +} diff --git a/spring519/src/main/resources/spring.xml b/spring519/src/main/resources/spring.xml new file mode 100644 index 0000000..62699ba --- /dev/null +++ b/spring519/src/main/resources/spring.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file diff --git a/spring519/src/test/java/TestDemo.java b/spring519/src/test/java/TestDemo.java new file mode 100644 index 0000000..829dd8c --- /dev/null +++ b/spring519/src/test/java/TestDemo.java @@ -0,0 +1,24 @@ +import cn.czyx007.bean.Dog; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/19 - 16:00 + */ +public class TestDemo { + @Test + public void test1(){ + Dog dog = new Dog("xxx"); + dog.eat(); + } + + @Test + public void test2(){ + ApplicationContext app = new ClassPathXmlApplicationContext("spring.xml"); + Dog dog = app.getBean("dog", Dog.class); +// dog.setName("xxx"); + dog.eat(); + } +}