当前位置: 首页 > news >正文

04-08核心配置和映射配置文件解析

学习资料视频来源:https://www.bilibili.com/video/BV1R14y1W7yS

文章目录

  • 1. 项目介绍
    • 1.1 项目结构
    • 1.2 项目依赖
      • (1) ipersistent依赖
      • (2) ipersistent-test依赖
    • 1.3 配置文件
      • (1) 核心配置文件
      • (2) 映射配置文件
  • 2. 核心源码类
    • 2.1 ipersistent
      • Resources
      • Configuration
      • MappedStatement
      • XMLConfigBuilder
      • XMLMapperBuilder
      • SqlSessionFactoryBuilder
      • SqlSessionFactory
      • DefaultSqlSessionFactory
    • 2.2 ipersistent-test
      • 测试类

1. 项目介绍

1.1 项目结构

项目分为2个模块,ipersistent 和ipersistent-test。ipersistent为框架,ipersistent-test为框架使用端,ipersistent-test依赖ipersistent。视频里是使用maven管理项目,我这里是使用gradle管理项目。目录结构如下:
在这里插入图片描述

1.2 项目依赖

(1) ipersistent依赖

plugins {
    id 'java-library'
}

group = 'com.mql'

repositories {
    maven {
        url 'https://maven.aliyun.com/nexus/content/groups/public/'
    }
    mavenCentral()
}

dependencies {
    testImplementation platform('org.junit:junit-bom:5.10.0')
    testImplementation 'org.junit.jupiter:junit-jupiter'
    // 引入dom4j依赖
    api 'dom4j:dom4j:1.6.1'
    runtimeOnly 'mysql:mysql-connector-java:5.1.40'
    implementation('com.alibaba:druid:1.2.8')
    implementation 'jaxen:jaxen:1.2.0' // 
}

(2) ipersistent-test依赖

plugins {
    id 'java'
}

group = 'com.mql'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation platform('org.junit:junit-bom:5.10.0')
    testImplementation 'org.junit.jupiter:junit-jupiter'
    // 引用ipersistent
        implementation project(':ipersistent')
    implementation 'org.apache.logging.log4j:log4j-core:2.14.1'
    implementation 'org.apache.logging.log4j:log4j-api:2.14.1'
}

test {
    useJUnitPlatform()
}

1.3 配置文件

核心配置文件映射配置文件,都是在ipersistent-test提供。

(1) 核心配置文件

<configuration>
    <!-- 数据源配置 -->
    <datasource>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/test"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </datasource>
    <mappers>
        <mapper resource="mapper/UserMapper.xml"/>
    </mappers>
</configuration>

(2) 映射配置文件

<mapper namespace="user">

    <!--    唯一标识: namespace.id-->
    <!-- 查询所有用户 -->
    <select id="selectList" resultType="com.mqlyes.User">
        select * from user
    </select>

    <!-- 按条件查询用户 -->
    <!--    -->
    <select id="selectOne" resultType="com.mqlyes.User" parameterType="com.mqlyes.User">
        select * from user where id = #{id} and username = #{username}
    </select>
</mapper>

2. 核心源码类

2.1 ipersistent

Resources

作用:根据路径,加载配置文件,并转换成字节流,比较简单。

public class Resources {

    /**
     * 根据配置文件的路径加载配置文件存到内存中
     * @param path
     * @return
     */
    public static InputStream getResource(String path) {
        return Resources.class.getClassLoader().getResourceAsStream(path);
    }
}

Configuration

作用:Configuration存储核心配置和映射配置文件解析出的配置信息。分别对应dataSource和mappedStatementMap。


/**
 * 存数据库配置信息 从sqlMapper.xml中解析出的东西
 */
public class Configuration {
    // 数据源对象
    private DataSource dataSource;
    // key为 statementID
    private Map<String, MappedStatement> mappedStatementMap = new HashMap<>();

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public Map<String, MappedStatement> getMappedStatementMap() {
        return mappedStatementMap;
    }

    public void setMappedStatementMap(Map<String, MappedStatement> mappedStatementMap) {
        this.mappedStatementMap = mappedStatementMap;
    }
}

MappedStatement

作用:存储映射配置文件中解析出信息,一个MappedStatement对应一个xxxmapper.xml文件

public class MappedStatement {
    // 唯一标识
    private String StatementID;

    private String resultType;

    private String parameterType;

    private String sql;

    public String getStatementID() {
        return StatementID;
    }

    public void setStatementID(String statementID) {
        StatementID = statementID;
    }

    public String getResultType() {
        return resultType;
    }

    public void setResultType(String resultType) {
        this.resultType = resultType;
    }

    public String getParameterType() {
        return parameterType;
    }

    public void setParameterType(String parameterType) {
        this.parameterType = parameterType;
    }

    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }
}

XMLConfigBuilder

作用:先解析核心配置文件中数据库配置信息,封装成Datasource。再解析mapper文件所有路径
调用XMLMapperBuilder解析mapper文件,并封装进MappedStatement。解析二者的过程中,把Datasource和MappedStatement封装进Configuration。

public class XMLConfigBuilder {

    private Configuration configuration;

    public XMLConfigBuilder() {
        this.configuration = new Configuration();
    }

    /**
     * 解析配置文件 封装到Configuration中
     *
     * @param inputStream
     * @return
     */
    public Configuration parse(InputStream inputStream) throws DocumentException {
        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();
        List<Element> list = rootElement.selectNodes("//property");
        Properties properties = new Properties();
        for (Element element : list) {
            String name = element.attributeValue("name");
            String value = element.attributeValue("value");
            properties.put(name, value);
        }
        // 创建数据源对象
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(properties.getProperty("driver"));
        druidDataSource.setUrl(properties.getProperty("url"));
        druidDataSource.setUsername(properties.getProperty("username"));
        druidDataSource.setPassword(properties.getProperty("password"));
        configuration.setDataSource(druidDataSource);
        // 获取映射配置文件路径
        List<Element> mapperList = rootElement.selectNodes("//mapper");
        for (Element element : mapperList) {
            String mapperPath = element.attributeValue("resource");
            InputStream resource = Resources.getResource(mapperPath);
            XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(configuration);
            xmlMapperBuilder.parse(resource);
        }
        return configuration;
    }
}

XMLMapperBuilder


public class XMLMapperBuilder {

    private Configuration configuration;

    public XMLMapperBuilder(Configuration configuration) {
        this.configuration = configuration;
    }

  public   void parse(InputStream inputStream) throws DocumentException {
        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();
        String namespace = rootElement.attributeValue("namespace");
        List<Element> elements = rootElement.selectNodes("//select");
        for (Element element : elements) {
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            String parameterType = element.attributeValue("parameterType");
            String sql = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setStatementID(namespace + "." + id);
            mappedStatement.setSql(sql);
            mappedStatement.setResultType(resultType);
            mappedStatement.setParameterType(parameterType);
            configuration.getMappedStatementMap().put(mappedStatement.getStatementID(), mappedStatement);
        }

    }
}

SqlSessionFactoryBuilder

public class SqlSessionFactoryBuilder {

    public SqlSessionFactory build(InputStream inputStream) throws DocumentException {
        XMLConfigBuilder xmlConfigBuilder = new XMLConfigBuilder();
        Configuration configuration = xmlConfigBuilder.parse(inputStream);
        // 创建SqlSessionFactory工厂对想
        DefaultSqlSessionFactory defaultSqlSessionFactory = new DefaultSqlSessionFactory(configuration);
        return defaultSqlSessionFactory;
    }

}

SqlSessionFactory

public interface SqlSessionFactory {
}

DefaultSqlSessionFactory


public class DefaultSqlSessionFactory implements SqlSessionFactory {
    private Configuration configuration;

    public DefaultSqlSessionFactory(Configuration configuration) {
        this.configuration = configuration;
    }
}

2.2 ipersistent-test

测试类

使用端目前只有一个测试类,看返回结果中的configuration对象信息是否完整。

public class IpersistentTest {

    @Test
    public void test1() throws Exception {
        InputStream resource = Resources.getResource("sqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resource);
        System.out.println(build);
    }
}

http://www.dtcms.com/a/106511.html

相关文章:

  • 14.主从Reactor+线程池模式,Connection对象引用计数的深入分析
  • 【UE5】发现意外的文件尾解决方法
  • 国产系统服务器识别不到stata盘
  • C语言--统计输入字符串中的单词个数
  • 前端新增数据,但数据库里没有新增的数据
  • k8s 污点常用操作
  • Qt进阶开发:QFileSystemModel的使用
  • Rust 开发提效神器:lombok-macros 宏库
  • 吴恩达深度学习复盘(5)神经网络的前向传播TesorFlow与NumPy实现比对
  • 【高并发内存池】第八弹---脱离new的定长内存池与多线程malloc测试
  • 【redis】redis实现红锁
  • 关于文化和软件技术的传承
  • resttemplate设置params
  • C++与Python初始化最小和最大整数
  • 多传感器融合SLAM中如何检验编写的Factor是否有效
  • 黑马点评。1 导入黑马点评项目
  • C++ 中的 **CRTP
  • 鸿蒙定位开发服务
  • 论文浅尝 | Interactive-KBQA:基于大语言模型的多轮交互KBQA(ACL2024)
  • HTML 媒体(Media)学习笔记
  • 使用Apache HttpClient编写Java爬虫
  • Python | 第十一章 | 模块和包 | 面向对象编程_基础部分
  • Java安全基础-反射机制
  • 《AI大模型应知应会100篇》第2篇:大模型核心术语解析:参数、Token、推理与训练
  • 基于微信小程序的智慧乡村旅游服务平台【附源码】
  • 聊聊Spring AI的EmbeddingModel
  • 好文和技术网站记录
  • Java虚拟机面试题:引言
  • 【Zabbix技术系列文章】第⑥篇——Zabbix 高级运维与优化
  • leetcode118.杨辉三角