自定义ORM(mybatis)源码(二)-解析mapper.xml

自定义ORM(mybatis)源码(二)-解析mapper.xml

模仿mybatis

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <datasource>
        <property key="driverName" value="com.mysql.cj.jdbc.Driver"></property>
        <property key="url" value="jdbc:mysql://localhost:3306"></property>
        <property key="username" value="test"></property>
        <property key="password" value="test"></property>
    </datasource>

    <mappers>
        <mapper resource="mapper/UserMapper.xml"/>
    </mappers>
</config>

这里解析 mappers 节点下的UserMapper.xml

XmlMapperParser

继承 BaseXmlParser

public class XmlMapperParser extends BaseXmlParser {
    private Document document;

    public XmlMapperParser(InputStream inputStream, Properties properties, Configuration configuration) {
        super(configuration);
        this.document = createDocument(new InputSource(inputStream));
    }

    @Override
    public Configuration parse() {
        try {
            Configuration configuration = getConfiguration();
            XNodeParser xNodeParser = new XNodeParser(document);
            //解析 namespace
            Node mapper = xNodeParser.getNodeObject("mapper");
            String namespace = XNodeParser.getAttributeValue(mapper, "namespace");
            Class<?> mapperClz = Class.forName(namespace);

            //解析 sql-map
            extractSelectNode(configuration, xNodeParser, namespace);
            //增加mapper
            configuration.addMapper(mapperClz);
            return getConfiguration();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static void extractSelectNode(Configuration configuration, XNodeParser xNodeParser, String namespace) throws XPathExpressionException {
        NodeList nodeList = xNodeParser.getNodeList("mapper//select");
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node select = nodeList.item(i);
            MappedStatement stmt = new MappedStatement();
            stmt.setNamespace(namespace);
            stmt.setCommand("select");
            //解析 resultType
            extractResultMapping(namespace, select, stmt);
            //解析sql
            stmt.addBoundSql(new BoundSql(select.getTextContent()));
            configuration.addMappedStatement(stmt);
        }
    }

    private static void extractResultMapping(String namespace, Node select, MappedStatement stmt) {
        String id = XNodeParser.getAttributeValue(select, "id");
        stmt.setFullId(namespace.concat(".").concat(id));
        String resultType = XNodeParser.getAttributeValue(select, "resultType");
        Class<?> aClass;
        try {
            aClass = Class.forName(resultType);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        stmt.addResultMapping(new ResultMapping(id,aClass));
    }
}

将解析的结果放在 Configuration中, 并且在 MapperRegistry 中创建 代理对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值