步骤:
1.读配置文件
2.读xml文件
3.查找要执行的方法
4.执行sql
5.结果赋值
核心代码:
//1.解析mapperXml配置文件 MapperBean mapperBean = XmlParseUtils.loadXml(path); if (!method.getDeclaringClass().getName().equals(mapperBean.getInterfaceName())) { return null; } //2.查找要执行的方法 List<InterfaceMethodInfo> interfaceMethodInfoList = mapperBean.getInterfaceMethodInfoList(); InterfaceMethodInfo currentMethodInfo = getInterfaceMethodInfo(method, interfaceMethodInfoList); if (currentMethodInfo == null) { return null; } //3.执行sql List<Object> paramsList = new ArrayList<>(); paramsList.add(args[0]); ResultSet resultSet = JDBCUtils.query(currentMethodInfo.getSql(), paramsList); //4.结果赋值 Object returnTypeObj = currentMethodInfo.getResultType(); setReturnTypeObj(returnTypeObj, resultSet);
代码地址:https://github.com/zhugezifang/mybatis-xml