Framework Implementation

In the current project, create under 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>

This code is a MyBatis configuration file, mainly completing the following tasks:

  • Configure database connection properties, such as driver class, JDBC URL, username and password
  • Import MyBatis mapping file for defining SQL statements and mapping relationships

mapper.xml

Implement single query and list query:

<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": Defines the namespace of the mapping file, used to identify the current Mapper file

Single record query (selectOne):

  • select id="selectOne" defines the method name, corresponding to the method name in UserInfoMapper interface
  • parameterType specifies the parameter type of the method
  • resultType specifies the return result type

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;
    }

}

This code is a Java class for parsing XML configuration files, whose main role is to read XML configuration file content and initialize application configuration information.

Class Definition

  • Class name XMLConfigerBuilder: Indicates this is an XML configuration parser
  • Member variable configuration: Saves application configuration information

parseConfiguration Method This method is responsible for parsing XML files and initializing Configuration objects, mainly including the following steps:

  1. Parse XML document: Use SAXReader to read input stream, generate XML document object
  2. Read database configuration: Find property nodes in XML, parse name and value attributes
  3. Parse Mapper files: Find mapper nodes in XML, extract resource attribute value of each node

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);
        }
    }

}