4.<cache>标签解析剖析

MyBatis缓存
本文详细解析了MyBatis中的二级缓存机制,包括其构建过程、与Mapper的关系及配置方式。通过源码分析,展示了从SqlSessionFactory创建到MappedStatement组装的全过程。

1.首先进入sqlSessionFactoryBuilder.build()方法

public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
        SqlSessionFactory var5;
        try {
        // 核心配置Configuration的XML解析
            XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
            var5 = this.build(parser.parse());
        } catch (Exception var14) {
            throw ExceptionFactory.wrapException("Error building SqlSession.", var14);
        } finally {
            ErrorContext.instance().reset();

            try {
                inputStream.close();
            } catch (IOException var13) {
            }

        }

2.进入XMLConfigBuilder的parse()方法,再进入parseConfiguration(XNode root)方法,这里利用构建者模式的依次解析核心XML中的各个标签构建Configuration对象

private void parseConfiguration(XNode root) {
        try {
            this.propertiesElement(root.evalNode("properties"));
            Properties settings = this.settingsAsProperties(root.evalNode("settings"));
            this.loadCustomVfs(settings);
            this.typeAliasesElement(root.evalNode("typeAliases"));
            this.pluginElement(root.evalNode("plugins"));
            this.objectFactoryElement(root.evalNode("objectFactory"));
            this.objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
            this.reflectorFactoryElement(root.evalNode("reflectorFactory"));
            this.settingsElement(settings);
            this.environmentsElement(root.evalNode("environments"));
            this.databaseIdProviderElement(root.evalNode("databaseIdProvider"));
            this.typeHandlerElement(root.evalNode("typeHandlers"));
            this.mapperElement(root.evalNode("mappers"));
        } catch (Exception var3) {
            throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + var3, var3);
        }
    }

3.进入mappers解析方法并找到cache标签的解析方法进入即XMLMapperBuilder的cacheElement(XNode context)方法,这里获取cache标签的各个配置属性,并调用builderAssistant.useNewCache()进行构建。

private void cacheElement(XNode context) throws Exception {
        if (context != null) {
            String type = context.getStringAttribute("type", "PERPETUAL");
            Class<? extends Cache> typeClass = this.typeAliasRegistry.resolveAlias(type);
            String eviction = context.getStringAttribute("eviction", "LRU");
            Class<? extends Cache> evictionClass = this.typeAliasRegistry.resolveAlias(eviction);
            Long flushInterval = context.getLongAttribute("flushInterval");
            Integer size = context.getIntAttribute("size");
            boolean readWrite = !context.getBooleanAttribute("readOnly", false);
            boolean blocking = context.getBooleanAttribute("blocking", false);
            Properties props = context.getChildrenAsProperties();
            this.builderAssistant.useNewCache(typeClass, evictionClass, flushInterval, size, readWrite, blocking, props);
        }

4.进入useNewCache()方法发现,创建一个Cache接口的实现类即二级缓存对象,并分别存入核心配置类Configuration对对象,和MapperBuilderAssistant对象中,由于MapperBuilderAssistant属于XMLMapperBuilder,即XMLMapperBuilder中存在一个二级缓存(又源码得知一个mapper标签一个XMLMapperBuilder对象)。

public Cache useNewCache(Class<? extends Cache> typeClass, Class<? extends Cache> evictionClass, Long flushInterval, Integer size, boolean readWrite, boolean blocking, Properties props) {
        Cache cache = (new CacheBuilder(this.currentNamespace)).implementation((Class)this.valueOrDefault(typeClass, PerpetualCache.class)).addDecorator((Class)this.valueOrDefault(evictionClass, LruCache.class)).clearInterval(flushInterval).size(size).readWrite(readWrite).blocking(blocking).properties(props).build();
        this.configuration.addCache(cache);
        this.currentCache = cache;
        return cache;
    }

5.继续回到XMLConfigBuilder解析mapperXML的mapperElement(XNode parent)方法,然后进入解析mapperXML的具体方法即XMLMapperBuilder的parse()方法。

XMLConfigBuilder.class

private void mapperElement(XNode parent) throws Exception {
        if (parent != null) {
            Iterator var2 = parent.getChildren().iterator();

            while(true) {
                while(var2.hasNext()) {
                    XNode child = (XNode)var2.next();
                    String resource;
                    if ("package".equals(child.getName())) {
                        resource = child.getStringAttribute("name");
                        this.configuration.addMappers(resource);
                    } else {
                        resource = child.getStringAttribute("resource");
                        String url = child.getStringAttribute("url");
                        String mapperClass = child.getStringAttribute("class");
                        XMLMapperBuilder mapperParser;
                        InputStream inputStream;
                        if (resource != null && url == null && mapperClass == null) {
                            ErrorContext.instance().resource(resource);
                            inputStream = Resources.getResourceAsStream(resource);
                            mapperParser = new XMLMapperBuilder(inputStream, this.configuration, resource, this.configuration.getSqlFragments());
                            //解析mapperXML的具体方法
                            mapperParser.parse();
                        } else if (resource == null && url != null && mapperClass == null) {
                            ErrorContext.instance().resource(url);
                            inputStream = Resources.getUrlAsStream(url);
                            mapperParser = new XMLMapperBuilder(inputStream, this.configuration, url, this.configuration.getSqlFragments());
                            mapperParser.parse();
                        } else {
                            if (resource != null || url != null || mapperClass == null) {
                                throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one.");
                            }

                            Class<?> mapperInterface = Resources.classForName(mapperClass);
                            this.configuration.addMapper(mapperInterface);
                        }
                    }
                }

                return;
            }
        }
    }
XMLMapperBuilder.class
public void parse() {
        if (!this.configuration.isResourceLoaded(this.resource)) {
            this.configurationElement(this.parser.evalNode("/mapper"));
            this.configuration.addLoadedResource(this.resource);
            this.bindMapperForNamespace();
        }

        this.parsePendingResultMaps();
        this.parsePendingCacheRefs();
        this.parsePendingStatements();
    }

6.找到解析select,insert,update,delete标签的方法,即组装MappedStatement的方法MapperBuilderAssistant的addMappedStatement()方法

public MappedStatement addMappedStatement( String id, ...) { if (unresolvedCacheRef) { throw new IncompleteElementException("Cache-ref not yet resolved"); }id = applyCurrentNamespace(id, false); boolean isSelect = sqlCommandType == SqlCommandType.SELECT; 
//创建MappedStatement对象
 MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, id, sqlSource, sqlCommandType) .flushCacheRequired(valueOrDefault(flushCache, !isSelect)) .useCache(valueOrDefault(useCache, isSelect)) 
 // 在这里将之前放到MapperBuilderAssistant中的二级缓存Cache封装到MappedStatement
 .cache(currentCache);
 

总结:
同一个mapperXML中各个增删改查标签公用一个二级缓存,所以说二级缓存是Mapper级别的。
陶某*____*

<?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> <name>Timo后台管理系统</name> <description>快速搭建后台管理的系统</description> <groupId>com.linln</groupId> <artifactId>timo</artifactId> <version>2.0.3</version> <packaging>pom</packaging> <modules> <module>admin</module> <module>common</module> <module>component</module> <module>devtools</module> <module>modules</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.7</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <!--shiro权限管理框架版本:component.shiro--> <shiro.version>1.9.0</shiro.version> <lombok.version>1.18.24</lombok.version> <ehcache.version>2.10.9.2</ehcache.version> <mysql.connector.version>5.1.46</mysql.connector.version> <jsoup.version>1.14.3</jsoup.version> <!--shiro模板视图权限标签扩展:component.thymeleaf--> <thymeleaf-shiro.version>2.1.0</thymeleaf-shiro.version> <!--excel工具框架版本:component.excel--> <poi.version>4.1.2</poi.version> <!--jwt处理框架版本:component.jwt--> <jwt.version>3.19.1</jwt.version> <!--swagger-knife4j接口文档页面:component.jwt--> <knife4j.version>3.0.3</knife4j.version> <google.findbugs.version>3.0.1</google.findbugs.version> <skipTests>true</skipTests> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <!--spring data jpa持久层框架--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <!--mysql连接驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.connector.version}</version> <scope>runtime</scope> </dependency> <!--lombok语法糖--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> <optional>true</optional> </dependency> <!--ehcache缓存框架--> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>${ehcache.version}</version> </dependency> <!--html解析工具,处理xss攻击--> <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>${jsoup.version}</version> </dependency> <!--knife4j 接口文档--> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>${knife4j.version}</version> </dependency> <!--解决编译时javax.annotation.meta.When不存在问题--> <dependency> <groupId>com.google.code.findbugs</groupId> <artifactId>annotations</artifactId> <version>${google.findbugs.version}</version> </dependency> </dependencies> <!--多环境配置--> <profiles> <!--开发环境--> <profile> <id>dev</id> <activation> <!--默认配置--> <activeByDefault>true</activeByDefault> </activation> <properties> <profile.name>dev</profile.name> </properties> </profile> <!--生产环境--> <profile> <id>prod</id> <properties> <profile.name>prod</profile.name> </properties> </profile> </profiles> </project>
06-28
<think>根据提供的pom.xml文件,这是一个Maven多模块项目,父项目为timo,包含多个子模块:admin,common,component,devtools, modules。父项目使用了Spring Boot2.6.7,并管理了多个依赖和属性。分析需求:用户可能希望了解如何基于这个pom.xml构建项目,或者如何添加新的模块,或者如何配置多环境等。由于问题不明确,我将根据常见的需求提供一些指导。常见操作:1.如何构建整个项目?2.如何添加一个新的模块?3.多环境配置的使用(dev和prod)?4.如何运行项目?由于用户没有提出具体问题,我将根据pom.xml的内容,给出一个标准的构建和运行步骤,并解释多环境配置的使用。步骤1:构建整个项目在项目根目录(即pom.xml所在的目录)执行以下命令:```bashmvn cleaninstall```步骤2:运行项目(假设我们要运行admin模块,因为通常admin是主应用)进入admin模块目录,然后使用SpringBoot Maven插件运行:```bashcdadminmvn spring-boot:run-Pdev#使用dev环境运行```或者使用生产环境配置: ```bashmvnspring-boot:run -Pprod```步骤3:添加新模块假设要添加一个名为`new-module`的模块:1.在项目根目录下创建`new-module`文件夹。2.在`new-module`文件夹中创建`pom.xml`,内容参考其他模块,至少包含:```xml<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.linln</groupId><artifactId>timo</artifactId><version>2.0.3</version></parent><artifactId>new-module</artifactId><packaging>jar</packaging></project>```3.在父项目的pom.xml的`<modules>`部分添加:```xml<module>new-module</module>```步骤4:多环境配置的使用在SpringBoot项目中,多环境配置文件通常命名为`application-{profile}.properties`或`application-{profile}.yml`。在`src/main/resources`目录下,我们可能有:-application-dev.properties(开发环境)- application-prod.properties(生产环境)在运行时,通过`spring.profiles.active`属性指定环境。在Maven命令中,我们使用`-P`参数来激活Maven的profile,然后Maven的profile可以设置`spring.profiles.active`。但注意,在pom.xml中,我们定义的profile是Maven的profile,它可以通过资源过滤来传递属性,或者通过`-D`参数传递。更常见的做法是在SpringBoot的application.properties中设置`spring.profiles.active=@profile.name@`,然后在Maven资源过滤时替换。但是在这个pom.xml中,我们没有看到资源过滤的配置。因此,可能需要额外配置。或者,我们可以在运行应用时直接指定Spring的profile: ```bashmvn spring-boot:run-Dspring-boot.run.profiles=dev```由于pom.xml中已经定义了Maven的profile,并且每个profile有一个`profile.name`属性,我们可以通过资源过滤将`profile.name`的值传递给SpringBoot的配置文件。但是,观察这个pom.xml,并没有配置资源过滤。因此,如果项目中没有配置,那么可能需要手动配置资源过滤,或者使用上述的命令行参数方式。如果需要配置资源过滤,可以在pom.xml的build部分添加:```xml<build><resources><resource><directory>src/main/resources</directory><filtering>true</filtering><includes><include>application.properties</include></includes></resource></resources> </build>```然后在`application.properties`中设置:```spring.profiles.active=@profile.name@```这样,当使用`mvn install-Pdev`时,`@profile.name@`会被替换为`dev`。但是,请注意,这个pom.xml没有配置build部分,因此默认不会进行资源过滤。如果需要,可以添加。由于用户的问题不明确,我将提供一些常见的相关问题和解答。如果用户有具体问题,可以进一步提问。鉴于以上,我将提供如何运行这个项目的示例代码(实际上是命令)和解释。注意:代码部分使用markdown格式包裹,但由于是命令,所以使用bash代码块。示例:使用Maven构建并运行项目(以admin模块为例,使用dev环境)</think>根据提供的 `pom.xml` 文件,这是一个基于 Spring Boot 2.6.7 的多模块 Maven 项目,用于构建 "Timo后台管理系统"。以下是关键分析和优化建议: ### 关键分析 1. **项目结构** ```xml <modules> <module>admin</module> <module>common</module> <module>component</module> <module>devtools</module> <module>modules</module> </modules> ``` - `admin`: 主应用模块 - `common`: 公共工具和基础类 - `component`: 核心组件(如 Shiro、Excel 等) - `devtools`: 开发工具支持 - `modules`: 业务功能模块 2. **依赖管理** - 核心依赖:Spring Boot Web、Data JPA、Validation - 安全框架:Apache Shiro 1.9.0 - 工具集:Lombok、Ehcache、Jsoup (防XSS)、Knife4j (API文档) - 数据库:MySQL Connector 3. **多环境配置** ```xml <profiles> <profile> <id>dev</id> <!-- 默认激活 --> </profile> <profile> <id>prod</id> <!-- 生产环境 --> </profile> </profiles> ``` ### 优化建议与示例代码 #### 1. 添加资源过滤支持多环境 在 `<build>` 中增加资源过滤,使 `application-{profile}.yml` 自动生效: ```xml <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <!-- 启用变量替换 --> <includes> <include>application*.yml</include> </includes> </resource> </resources> </build> ``` #### 2. 统一依赖版本管理 将第三方依赖版本移至 `<dependencyManagement>` 避免冲突: ```xml <dependencyManagement> <dependencies> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>${shiro.version}</version> </dependency> <!-- 其他组件统一管理 --> </dependencies> </dependencyManagement> ``` #### 3. 增强安全配置(XSS过滤示例) 在 `common` 模块添加全局 XSS 过滤器: ```java // XssFilter.java @WebFilter(urlPatterns = "/*") public class XssFilter implements Filter { @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; // 使用Jsoup清理用户输入 String sanitizedInput = Jsoup.clean(request.getParameter("input"), Safelist.basic()); // 将清理后的数据放入请求属性 request.setAttribute("safeInput", sanitizedInput); chain.doFilter(new XssRequestWrapper(request), res); } } // 在启动类添加注解 @ServletComponentScan // 启用Filter扫描 ``` #### 4. 多环境配置示例 `src/main/resources/application-dev.yml`: ```yaml server: port: 8080 spring: datasource: url: jdbc:mysql://localhost:3306/timo_dev username: dev_user password: dev_pass ``` `src/main/resources/application-prod.yml`: ```yaml server: port: 80 spring: datasource: url: jdbc:mysql://prod-db:3306/timo_prod username: prod_user password: ${DB_PASSWORD} # 从环境变量读取 ``` ### 项目启动命令 ```bash # 开发环境 mvn spring-boot:run -P dev # 生产环境 (传递密码参数) mvn spring-boot:run -P prod -Dspring-boot.run.arguments=--DB_PASSWORD=secret123 ``` ### 关键解释 1. **资源过滤 (`<filtering>true</filtering>)** 允许在配置文件中使用 Maven 变量(如 `@profile.name@`),构建时自动替换为实际值。 2. **XSS 防御原理** `Jsoup.clean()` 通过 `Safelist` 限制允许的 HTML 标签,过滤 `<script>` 等危险内容。 3. **多环境切换机制** `-P` 参数激活 Maven Profile,Spring Boot 会自动加载对应的 `application-{profile}.yml` 配置。 4. **依赖管理优势** 子模块无需指定组件版本,避免版本冲突,确保所有模块使用统一依赖版本。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

躺平程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值