5/19 19:08
This commit is contained in:
parent
2d8d14a7f0
commit
68a8a9f33a
1
.idea/compiler.xml
generated
1
.idea/compiler.xml
generated
@ -7,6 +7,7 @@
|
||||
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||
<outputRelativeToContentRoot value="true" />
|
||||
<module name="spring519" />
|
||||
<module name="mybatis516" />
|
||||
<module name="demo516" />
|
||||
<module name="demo515" />
|
||||
|
2
.idea/encodings.xml
generated
2
.idea/encodings.xml
generated
@ -11,5 +11,7 @@
|
||||
<file url="file://$PROJECT_DIR$/mybatis517-518/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/mybatis519/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/mybatis519/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/spring519/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/spring519/src/main/resources" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
1
.idea/misc.xml
generated
1
.idea/misc.xml
generated
@ -13,6 +13,7 @@
|
||||
<option value="$PROJECT_DIR$/mybatis517/pom.xml" />
|
||||
<option value="$PROJECT_DIR$/mybatis517-518/pom.xml" />
|
||||
<option value="$PROJECT_DIR$/mybatis519/pom.xml" />
|
||||
<option value="$PROJECT_DIR$/spring519/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
|
@ -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")
|
||||
|
@ -5,4 +5,7 @@
|
||||
|
||||
<mapper namespace="cn.czyx007.mapper.BrandMapper">
|
||||
|
||||
<!-- 开启二级缓存,注解与此处二选一 -->
|
||||
<!-- <cache/>-->
|
||||
|
||||
</mapper>
|
@ -20,6 +20,7 @@
|
||||
<!--将下划线映射为驼峰-->
|
||||
<setting name="mapUnderscoreToCamelCase" value="true"/>
|
||||
<setting name="lazyLoadingEnabled" value="true"/>
|
||||
<setting name="cacheEnabled" value="true"/>
|
||||
</settings>
|
||||
|
||||
<typeAliases>
|
||||
@ -28,6 +29,7 @@
|
||||
|
||||
<plugins>
|
||||
<plugin interceptor="com.github.pagehelper.PageInterceptor">
|
||||
<!-- 启用合理化,防止第一页再点击上一页,最后一页再点击下一页 -->
|
||||
<property name="reasonable" value="true"/>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
36
spring519/pom.xml
Normal file
36
spring519/pom.xml
Normal file
@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>cn.czyx007</groupId>
|
||||
<artifactId>spring519</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.20</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>5.3.23</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
22
spring519/src/main/java/cn/czyx007/bean/Dog.java
Normal file
22
spring519/src/main/java/cn/czyx007/bean/Dog.java
Normal file
@ -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 + "的狗吃骨头...");
|
||||
}
|
||||
}
|
10
spring519/src/main/resources/spring.xml
Normal file
10
spring519/src/main/resources/spring.xml
Normal file
@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
<bean id="dog" class="cn.czyx007.bean.Dog">
|
||||
<!-- 依赖注入,对 对象属性赋值 -->
|
||||
<property name="name" value="xxx"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
24
spring519/src/test/java/TestDemo.java
Normal file
24
spring519/src/test/java/TestDemo.java
Normal file
@ -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();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user