框架实现

在当前项目中,在 resources 下新建:

  • sqlMapConfig.xml
  • mapper.xml

sqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
    <property name="jdbcUrl" value="jdbc:mysql://172.16.1.130:3306/wzk-mybatis?characterEncoding=utf-8"></property>
    <property name="user" value="hive"></property>
    <property name="password" value="hive@wzk.icu"></property>
    <mapper resource="mapper.xml"></mapper>
</configuration>

这段代码是一个 MyBatis 的配置文件,主要完成以下任务:

  • 配置数据库连接属性,例如驱动类、JDBC URL、用户名和密码
  • 引入 MyBatis 的映射文件,用于定义 SQL 语句和映射关系

mapper.xml

实现一个单查询和列表的查询:

<mapper namespace="icu.wzk.dao.UserInfoMapper">
    <select id="selectOne" parameterType="icu.wzk.model.UserInfo" resultType="icu.wzk.model.UserInfo">
        SELECT
            *
        FROM
            user_info
        WHERE
            username=#{username}
    </select>
    <select id="selectList" parameterType="icu.wzk.model.UserInfo" resultType="icu.wzk.model.UserInfo">
        SELECT
            *
        FROM
            user_info
    </select>
</mapper>

命名空间 (namespace):

  • mapper namespace="icu.wzk.dao.UserInfoMapper":定义了映射文件的命名空间,用于标识当前的 Mapper 文件

单条记录查询 (selectOne):

  • select id="selectOne" 定义了方法名称,与 UserInfoMapper 接口中的方法名对应
  • parameterType 指定了方法的参数类型
  • resultType 指定了返回结果的类型

实体相关

model

package icu.wzk.model;

import lombok.Data;

@Data
public class UserInfo {

    private Long id;

    private String username;

    private String password;

    private Integer age;

}

资源相关

Configuration

package icu.wzk.bean;

import lombok.Data;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Data
public class Configuration {

    private DataSource dataSource;

    private Map<String, MappedStatement> mappedStatementMap = new HashMap<>();

}

MappedStatement

package icu.wzk.bean;

import lombok.Data;

@Data
public class MappedStatement {

    private String id;

    private String sql;

    private String parameterType;

    private String resultType;

}

Resources

package icu.wzk.bean;

import java.io.InputStream;

public class Resources {

    public static InputStream getResourceAsStream(String path) {
        return Resources.class.getClassLoader().getResourceAsStream(path);
    }

}

解析相关

XMLConfigerBuilder

package icu.wzk.bean;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

public class XMLConfigerBuilder {

    private Configuration configuration;

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

    public Configuration parseConfiguration(InputStream inputStream) throws DocumentException, PropertyVetoException, ClassNotFoundException {
        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();
        List<Element> propertyElements = rootElement.selectNodes("//property");
        Properties properties = new Properties();
        for (Element element : propertyElements) {
            String name = element.attributeValue("name");
            String value = element.attributeValue("value");
            properties.setProperty(name, value);
        }
        ComboPooledDataSource comboSource = new ComboPooledDataSource();
        comboSource.setDriverClass(properties.getProperty("driverClass"));
        comboSource.setJdbcUrl(properties.getProperty("jdbcUrl"));
        comboSource.setUser(properties.getProperty("user"));
        comboSource.setPassword(properties.getProperty("password"));
        configuration.setDataSource(comboSource);

        List<Element> mappedElements = rootElement.selectNodes("//mapper");
        XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(configuration);
        for (Element element : mappedElements) {
            String mapperPath = element.attributeValue("resource");
            InputStream resourceAsStream = Resources.getResourceAsStream(mapperPath);
            xmlMapperBuilder.parse(resourceAsStream);
        }
        return configuration;
    }

}

这段代码是一个用于解析 XML 配置文件的 Java 类,主要作用是读取 XML 配置文件内容,初始化应用程序所需的配置信息。

类的定义

  • 类名 XMLConfigerBuilder:表示这是一个 XML 配置解析器
  • 成员变量 configuration:保存应用的配置信息

parseConfiguration 方法 该方法负责解析 XML 文件并初始化 Configuration 对象,主要包括以下几个步骤:

  1. 解析 XML 文档:使用 SAXReader 读取输入流,生成 XML 文档对象
  2. 读取数据库配置:查找 XML 中的 property 节点,解析出 name 和 value 属性
  3. 解析 Mapper 文件:查找 XML 中的 mapper 节点,提取每个节点的 resource 属性值

XMLMapperBuilder

package icu.wzk.bean;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

public class XMLMapperBuilder {

    private Configuration configuration;

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

    public void parse(InputStream inputStream) throws DocumentException, ClassNotFoundException {
        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();
        String namespace = rootElement.attributeValue("namespace");
        List<Element> selectList = rootElement.selectNodes("select");
        for (Element element : selectList) {
            String id = element.attributeValue("id");
            String parameterType = element.attributeValue("parameterType");
            String resultType = element.attributeValue("resultType");
            String key = namespace + "." + id;
            String textTrim = element.getTextTrim();

            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParameterType(parameterType);
            mappedStatement.setResultType(resultType);
            mappedStatement.setSql(textTrim);
            configuration.getMappedStatementMap().put(key, mappedStatement);
        }
    }

}