{"id":142,"date":"2020-07-27T01:48:27","date_gmt":"2020-07-26T17:48:27","guid":{"rendered":"http:\/\/121.89.208.222\/?p=142"},"modified":"2020-12-10T14:24:25","modified_gmt":"2020-12-10T06:24:25","slug":"%e8%87%aa%e5%ae%9a%e4%b9%89%e5%ae%9e%e7%8e%b0mybatis%e6%a1%86%e6%9e%b6%e7%9a%84%e7%ae%80%e5%8d%95%e5%ae%9e%e7%8e%b0-%e6%90%ad%e5%bb%ba%e6%b3%a8%e8%a7%a3%e5%bc%80%e5%8f%91","status":"publish","type":"post","link":"http:\/\/moyisuiying.com\/index.php\/mybatis\/142.html","title":{"rendered":"\u81ea\u5b9a\u4e49\u5b9e\u73b0mybatis\u6846\u67b6\u7684\u7b80\u5355\u5b9e\u73b0-\u642d\u5efa\u6ce8\u89e3\u5f00\u53d1"},"content":{"rendered":"\n<p>\u7b14\u8005\u5728\u4e0a\u7bc7\u535a\u5ba2\u4e2d\u5df2\u7ecf\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u7b80\u5355\u5b9e\u73b0mybatis\u6846\u67b6\u57fa\u4e8eXML\u7684\u5b9e\u73b0\uff0c\u5176\u535a\u5ba2\u5730\u5740\u4e3a\uff1a<a href=\"http:\/\/moyisuiying.com\/index.php\/mybatis\/169.html\">\u81ea\u5b9a\u4e49\u5b9e\u73b0mybatis\u6846\u67b6\u7684\u7b80\u5355\u5b9e\u73b0-\u642d\u5efaXML\u5f00\u53d1<\/a><br>\u4e0b\u9762\u7ee7\u7eed\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684mybatis\u642d\u5efa\u6ce8\u89e3\u5f00\u53d1\uff0c\u5728\u539f\u6765XML\u642d\u5efa\u7684\u57fa\u7840\u4e0a\u7ee7\u7eed\u5b9e\u73b0\u3002<br>1.\u5c06mybatis-config.xml\u4e2d\u7684mappers\u6807\u7b7e\u6539\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;mappers>\n        &lt;!--  \u6307\u5b9aUserdao\u7684\u4f4d\u7f6e,\u4f7f\u7528XML\u914d\u7f6e\u65f6\u4f7f\u7528resource\u6807\u7b7e-->\n        &lt;!--\u4f7f\u7528\u6ce8\u89e3\u914d\u7f6e\u7684\u65f6\u5019\u4f7f\u7528class\u6807\u7b7e,\u4f7f\u7528UserDao\u7684\u5168\u9650\u5b9a\u7c7b\u540d   -->\n        &lt;mapper class=\"dao.UserDao\">&lt;\/mapper>\n    &lt;\/mappers><\/code><\/pre>\n\n\n\n<p>2.\u5728\u5305mybatis\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6ce8\u89e3\u63a5\u53e3\u7c7bSelect.java\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package mybatis;\n\nimport java.lang.annotation.ElementType;\nimport java.lang.annotation.Retention;\nimport java.lang.annotation.RetentionPolicy;\nimport java.lang.annotation.Target;\n\n\/**\n * Classname:mybatis-self\n *\n * @description:{description}\n * @author: \u964c\u610f\u968f\u5f71\n * @Date: 2020-07-26 09:59\n *\/\n@Retention(RetentionPolicy.RUNTIME)\n@Target(ElementType.METHOD)\npublic @interface Select {\n    \/**\n     * @Description :\u914d\u7f6eSQL\u8bed\u53e5\n     * @Date 10:06 2020\/7\/26 0026\n     * @Param * @param  \uff1a\n     * @return java.lang.String\n     **\/\n    String value();\n}<\/code><\/pre>\n\n\n\n<p>3.\u7136\u540e\u5728UserDao\u7684\u6bcf\u4e2a\u65b9\u6cd5\u4e0a\u5199\u4e0a\u6ce8\u89e3\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package dao;\n\nimport entity.User;\nimport mybatis.Select;\n\nimport java.util.List;\n\n\/**\n * Classname:mybatisdemo\n * @description:User\u7684dao\u63a5\u53e3\n * @author: \u964c\u610f\u968f\u5f71\n * @Date: 2020-07-24 10:35\n *\/\npublic interface UserDao {\n    \/**\n     * @date: 2020\/7\/24 0024 10:41\n     * @description:\u83b7\u53d6\u6240\u6709\u7684\u7528\u6237\u4fe1\u606f\n     * @return: \u8fd4\u56de\u5305\u542b\u6240\u6709\u7684\u7528\u6237\u7684list\n     *\/\n    @Select(\"select *from user\")\n    List&lt;User> getAll();\n    \/**\n     * @Description :\u6839\u636eid\u83b7\u53d6\u5236\u5b9a\u5bf9\u8c61\n     * @Date 0:32 2020\/7\/26 0026\n     * @Param * @param id \uff1a\n     * @return entity.User\n     **\/\n    @Select(\"select* from user where id=?\")\n   User getOne(int id);\n\n}<\/code><\/pre>\n\n\n\n<p>4.\u5728\u5de5\u5177\u7c7bMybatisUtil.java\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u89e3\u6790dao\u63a5\u53e3\u6ce8\u89e3\u7684\u51fd\u6570annotationLoader(String daoPath) \uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * @Description :\u901a\u8fc7\u53cd\u5c04\u83b7\u53d6dao\u4e2d\u6ce8\u89e3\u7684value\u4ee5\u53ca\u5168\u9650\u5b9a\u65b9\u6cd5\u540d\n * @Date 10:09 2020\/7\/26 0026\n * @Param * @param daoPath \uff1a\n * @return java.util.Map&lt;java.lang.String,mybatis.Mapper>\n **\/\n    private static Map&lt;String, Mapper> annotationLoader(String daoPath) throws Exception {\n        Map&lt;String, Mapper> mapperMap = new HashMap&lt;>();\n        \/\/\u83b7\u53d6dao\u7684\u5b57\u8282\u7801\u6587\u4ef6\n        Class&lt;?> aClass = Class.forName(daoPath);\n        \/\/\u83b7\u53d6\u6240\u6709\u7684\u65b9\u6cd5\n        Method&#91;] methods = aClass.getMethods();\n        \/\/\u904d\u5386\u6bcf\u4e00\u4e2a\u65b9\u6cd5\n        for (Method method : methods){\n            \/\/\u5224\u65ad\u662f\u5426\u662f\u6ce8\u89e3\n            boolean isAnnotation = method.isAnnotationPresent(Select.class);\n            if (isAnnotation){\n                Mapper mapper = new Mapper();\n                \/\/\u662fselect\u6ce8\u89e3\n                Select select = method.getAnnotation(Select.class);\n                \/\/\u83b7\u53d6select\u6ce8\u89e3\u7684\u503c,\u5373\u83b7\u53d6sql\u8bed\u53e5\n                String sql = select.value();\n                \/\/\u83b7\u53d6\u65b9\u6cd5\u540d\n                String methodName = method.getName();\n                \/\/\u7ec4\u5efakey\n                String key = daoPath+\".\"+methodName;\n                \/\/\u83b7\u53d6\u8fd4\u56de\u7c7b\u578b\n                Type returnType = method.getGenericReturnType();\n                \/\/\u5224\u65ad\u662f\u5426\u662f\u53c2\u6570\u5316\u7c7b\u578b\n                if (returnType instanceof ParameterizedType){\n                    \/\/\u5f3a\u8f6c\u6210\u6cdb\u578b\u53c2\u6570\u7c7b\u578b,\u6bd4\u5982 List&lt;User>\n                    ParameterizedType parameterizedType = (ParameterizedType) returnType;\n                    \/\/\u83b7\u53d6\u5b9e\u9645\u53c2\u6570\u7c7b\u578b\u6570\u7ec4\n                    Type&#91;] actualTypeArguments = parameterizedType.getActualTypeArguments();\n                    \/\/\u83b7\u53d6List&lt;User>\u4e2d\u7684User\u6cdb\u578b\u5b9e\u9645\u53c2\u6570\n                   Class actualType = (Class) actualTypeArguments&#91;0];\n                   \/\/\u83b7\u53d6\u5b9e\u9645\u53c2\u6570\u7c7b\u578b\u5168\u9650\u5b9a\u7c7b\u540d\n                    String actualTypeName = actualType.getName();\n                   mapper.setResultTypePath(actualTypeName);\n                }else{\n                    \/\/\u4e0d\u662f\u6cdb\u578b\u5316\u53c2\u6570\uff0c\u662f\u5b9e\u4f53\u7c7b\n                    Class&lt;?> methodReturnType = method.getReturnType();\n                    \/\/\u83b7\u53d6\u8fd4\u56de \u7c7b\u578b\u7684\u5168\u9650\u5b9a\u7c7b\u540d\n                    String methodReturnTypeName = methodReturnType.getName();\n                    mapper.setResultTypePath(methodReturnTypeName);\n                }\n             mapper.setSql(sql);\n             mapper.setSqlType(Mapper.SELECT);\n             mapperMap.put(key,mapper);\n\n            }\n        }\n\n        return mapperMap;\n    }<\/code><\/pre>\n\n\n\n<p>\u5bf9UserDao\u6ce8\u89e3\u8fdb\u884c\u89e3\u6790\u7684\u65f6\u5019\uff0c\u6bd4\u8f83\u96be\u7684\u70b9\u662f\u5982\u4f55\u83b7\u53d6\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7684\u5b9e\u9645\u53c2\u6570\u7c7b\u578b\uff0c\u4e3a\u6b64\u6211\u7279\u610f\u5199\u4e86\u4e00\u7bc7\u6587\u7ae0\uff1a<br><a href=\"http:\/\/121.89.208.222\/javastudy\/135.html\" target=\"_blank\"  rel=\"nofollow\" >Java\u901a\u8fc7\u53cd\u5c04\u83b7\u53d6\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u4ee5\u53ca\u53c2\u6570\u5316\u7684\u5b9e\u9645\u7c7b\u578b\uff08\u4e0a\u7bc7\uff09<\/a><br>MybatisUtil.java\u6587\u4ef6\u66f4\u65b0\u540e\u7684\u5b8c\u6574\u4ee3\u7801\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package utils;\n\nimport mybatis.Configurantion;\nimport mybatis.Mapper;\nimport mybatis.Resources;\nimport mybatis.Select;\nimport org.dom4j.Attribute;\nimport org.dom4j.Document;\nimport org.dom4j.DocumentException;\nimport org.dom4j.Element;\nimport org.dom4j.io.SAXReader;\n\nimport java.io.InputStream;\nimport java.lang.annotation.Annotation;\nimport java.lang.reflect.Method;\nimport java.lang.reflect.ParameterizedType;\nimport java.lang.reflect.Type;\nimport java.util.HashMap;\nimport java.util.List;\nimport java.util.Map;\n\n\/**\n * Classname:MybatisUtil\n *\n * @description:\n * @author: \u964c\u610f\u968f\u5f71\n * @Date: 2020-07-25 15:44\n * @Version: 1.0\n **\/\npublic class MybatisUtil {\n    \/**\n     * @Description :\u901a\u8fc7\u8d44\u6e90\u6587\u4ef6\u6d41\u52a0\u8f7d\u914d\u7f6e\u4fe1\u606f\n     * @Date 15:45 2020\/7\/25 0025\n     * @Param * @param inputStream \uff1a\n     * @return mybatis.Configurantion\n     **\/\n    public static Map&lt;String, Mapper> XMLLoader(String mapperPath) throws DocumentException {\n        InputStream resourceAsStream = Resources.getResourceAsStream(mapperPath);\n        \/\/\u83b7\u53d6\u8bfb\u53d6\u6d41\u5bf9\u8c61\n        SAXReader reader = new SAXReader();\n        \/\/\u83b7\u53d6document\u5bf9\u8c61\n        Document document = reader.read(resourceAsStream);\n        \/\/\u83b7\u53d6\u6839\u8282\u70b9\uff0c\u6839\u8282\u70b9\u5c31\u662fmapper\u8282\u70b9\n        Element rootElement = document.getRootElement();\n        \/\/\u83b7\u53d6mapper\u7684namespace\u7684\u5c5e\u6027\u503c\n        String namespace = rootElement.attributeValue(\"namespace\");\n        \/\/\u83b7\u53d6mapper\u4e0b\u7684\u6bcf\u4e2aselect\u8282\u70b9\n        List nodes = rootElement.selectNodes(\"\/\/select\");\n        Map&lt;String, Mapper> mapperMap = new HashMap&lt;>();\n        if (nodes!=null &amp;&amp; nodes.size() != 0){\n            Map&lt;String, Mapper> mappers = getMapperMap(namespace, nodes,Mapper.SELECT);\n            mapperMap.putAll(mappers);\n        }\n        \/\/\u83b7\u53d6mapper\u4e0b\u7684\u6bcf\u4e2ainsert\u8282\u70b9\n         nodes = rootElement.selectNodes(\"\/\/insert\");\n        if (nodes!=null &amp;&amp; nodes.size() != 0){\n            Map&lt;String, Mapper> mappers = getMapperMap(namespace, nodes,Mapper.INSERT);\n            mapperMap.putAll(mappers);\n        }\n        \/\/\u83b7\u53d6mapper\u4e0b\u7684\u6bcf\u4e2adelete\u8282\u70b9\n         nodes = rootElement.selectNodes(\"\/\/delete\");\n        if (nodes!=null &amp;&amp; nodes.size() != 0){\n            Map&lt;String, Mapper> mappers = getMapperMap(namespace, nodes,Mapper.DELETE);\n            mapperMap.putAll(mappers);\n        }\n        \/\/\u83b7\u53d6mapper\u4e0b\u7684\u6bcf\u4e2aupdate\u8282\u70b9\n        nodes = rootElement.selectNodes(\"\/\/update\");\n        if (nodes!=null &amp;&amp; nodes.size() != 0){\n            Map&lt;String, Mapper> mappers = getMapperMap(namespace, nodes,Mapper.UPDATE);\n            mapperMap.putAll(mappers);\n        }\n\n        return mapperMap;\n    }\n    \/**\n     * @Description :\u6839\u636enamespace\uff0c\u64cd\u4f5c\u7c7b\u578b\u8282\u70b9\u4ee5\u53ca\u64cd\u4f5c\u7c7b\u578b\u83b7\u53d6\u5c01\u88c5\u7684Map\n     * @Date 20:19 2020\/7\/25 0025\n     * @Param * @param namespace\uff1a dao\u63a5\u53e3\u7684\u5168\u9650\u5b9a\u7c7b\u540d\n     * @param nodes \uff1a\u64cd\u4f5c\u7c7b\u578b\u7684\u8282\u70b9\u96c6\u5408\n     * @param sqlType \uff1a\u64cd\u4f5c\u7c7b\u578b\n     * @return java.util.Map&lt;java.lang.String,mybatis.Mapper>\n     **\/\n    private static Map&lt;String, Mapper> getMapperMap(String namespace, List nodes,int sqlType) {\n        Map&lt;String,Mapper> mapperMap = new HashMap&lt;>();\n        for (Object o:nodes){\n            Element element = (Element) o;\n            \/\/\u83b7\u53d6select\u7684id\u5c5e\u6027\u503c\uff0c\u76f8\u5f53\u4e8edao\u63a5\u53e3\u7684\u65b9\u6cd5\u540d\n            String daoMethodName = element.attributeValue(\"id\");\n            \/\/\u83b7\u53d6select\u7684resultType\u7684\u5c5e\u6027\u503c\uff0c\u76f8\u5f53\u4e8edao\u63a5\u53e3\u7684\u8fd4\u56de\u503c\u7c7b\u578b\n            String resultType = element.attributeValue(\"resultType\");\n            \/\/\u7ec4\u5efa\u4e00\u4e2adao\u5168\u9650\u5b9a\u7c7b\u540d\u548c\u65b9\u6cd5\u540d\u7ec4\u6210\u7684key\uff0c\u6bd4\u5982  dao.UserDao.getAll,\u6807\u8bc6dao\u5305\u4e0b\u7684USerDao\u63a5\u53e3\u4e2d\u7684getAll\u65b9\u6cd5\n            String key = namespace+\".\"+daoMethodName;\n            \/\/\u83b7\u53d6select\u6807\u7b7e\u7684sql\u8bed\u53e5\n            String sql = element.getText();\n            Mapper mapper = new Mapper();\n            \/\/\u8bbe\u7f6e\u8fd4\u56de\u503c\u7c7b\u578b\n            mapper.setResultTypePath(resultType);\n            \/\/\u8bbe\u7f6esql\u8bed\u53e5\n            mapper.setSql(sql);\n            \/\/\u5c06mapper\u5b58\u5165Map\u4e2d\n            mapperMap.put(key,mapper);\n            \/\/\u8bbe\u7f6esql\u64cd\u4f5c\u7c7b\u578b\n            mapper.setSqlType(sqlType);\n        }\n        return mapperMap;\n    }\n\n    \/**\n   * @Description :\u52a0\u8f7dXML\u7684\u914d\u7f6e\u4fe1\u606f\n   * @Date 15:45 2020\/7\/25 0025\n   * @Param * @param inputStream \uff1a\u8d44\u6e90\u6587\u4ef6\u6d41\n   * @return mybatis.Configurantion\n   **\/\n    public static Configurantion LoaderConfiguration(InputStream inputStream) throws Exception {\n        \/\/\u83b7\u53d6\u8bfb\u53d6\u6d41\u5bf9\u8c61\n        SAXReader reader = new SAXReader();\n        Configurantion configurantion = new Configurantion();\n        try {\n            \/\/\u6839\u636e\u8d44\u6e90\u6587\u4ef6\u6d41\u83b7\u53d6document\u5bf9\u8c61\n            Document document = reader.read(inputStream);\n            \/\/\u83b7\u53d6\u6587\u6863\u7684\u6839\u8282\u70b9\n            Element rootElm = document.getRootElement();\n            \/\/\u83b7\u53d6\u9ed8\u8ba4\u7684\u6570\u636e\u5e93\u914d\u7f6e\n            List list = rootElm.selectNodes(\"environments\");\n            if (list == null || list.size() ==0){\n                throw  new RuntimeException(\"\u5c1a\u672a\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u73af\u5883\u4fe1\u606f\uff01\");\n            }\n            \/\/\u83b7\u53d6environments\u7684id\n            Element environmentsEle = (Element) list.get(0);\n            String defaultEnvironmentId = environmentsEle.attributeValue(\"default\");\n            \/\/\u83b7\u53d6\u6240\u6709\u7684\u5df2\u7ecf\u914d\u7f6e\u7684\u6570\u636e\u5e93\u73af\u5883\n            List environmentNodes = environmentsEle.elements();\n            if (environmentNodes == null || environmentNodes.size() ==0){\n                throw  new RuntimeException(\"\u5c1a\u672a\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u73af\u5883\u4fe1\u606f\uff01\");\n            }\n            \/\/\u83b7\u53d6\u4e0e\u9ed8\u8ba4\u7684environments\u7684id\u76f8\u540c\u7684environment\u8282\u70b9\n            List propertyList = null;\n            for (Object o: environmentNodes){\n                Element environmentEle = (Element) o;\n                \/\/\u627e\u5230\u9ed8\u8ba4\u7684\u6570\u636e\u5e93\u8fde\u63a5\u73af\u5883\n                if (defaultEnvironmentId.equals(environmentEle.attributeValue(\"id\"))){\n                   \/\/\u83b7\u53d6\u6570\u636e\u5e93\u7684\u94fe\u63a5\u4fe1\u606f\uff0cdriver,username,password,url\n                    \/\/\u83b7\u53d6\u8be5\u73af\u5883\u4e0b\u7684property\u6807\u7b7e\n                     propertyList = environmentEle.selectNodes(\"\/\/property\");\n                     \/\/\u8df3\u51fa\u5faa\u73af\n                    break;\n                }\n            }\n            if(propertyList  == null){\n                throw  new RuntimeException(\"\u5c1a\u672a\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u73af\u5883\u4fe1\u606f\uff01\");\n            }\n            for (Object p:propertyList  ) {\n                Element propertyEle = (Element) p;\n                \/\/\u83b7\u53d6property\u6807\u7b7e\u7684name\u5c5e\u6027\u503c\n                String name = propertyEle.attributeValue(\"name\");\n                if (\"url\".equals(name)){\n                    \/\/\u8bbe\u7f6eURL\u7684\u503c\n                    configurantion.setUrl(propertyEle.attributeValue(\"value\"));\n                }\n                if (\"driver\".equals(name)){\n                    \/\/\u8bbe\u7f6edriver\u7684\u503c\n                    configurantion.setDriver(propertyEle.attributeValue(\"value\"));\n                }\n                if (\"username\".equals(name)){\n                    \/\/\u8bbe\u7f6eusername\u7684\u503c\n                    configurantion.setUsername(propertyEle.attributeValue(\"value\"));\n                }\n                if (\"password\".equals(name)){\n                    \/\/\u8bbe\u7f6epassword\u7684\u503c\n                    configurantion.setPassword(propertyEle.attributeValue(\"value\"));\n                }\n            }\n            \/\/\u5b9a\u4f4d\u5230mybatis\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684 mappers \u4e2d\u7684mapper\u6807\u7b7e\u7684\u6240\u6709\u8282\u70b9\n            List mapperNodes = rootElm.selectNodes(\"mappers\/mapper\");\n            if (mapperNodes == null || mapperNodes.size() == 0){\n                throw  new RuntimeException(\"\u60a8\u672a\u914d\u7f6emapper\u7684\u4fe1\u606f\uff01\");\n            }\n            \/\/\u9010\u4e2a\u904d\u5386mapper\u8282\u70b9\u7136\u540e\u83b7\u53d6\u5176\u4e2dresource\u7684\u503c\n            for (Object e:mapperNodes){\n                Element element = (Element) e;\n                String  daoPath = element.attributeValue(\"resource\");\n                if (daoPath != null){\n                    \/\/\u662fXML\u914d\u7f6e\n                    Map&lt;String, Mapper> mapperMap = XMLLoader(daoPath);\n                    configurantion.setMapperMap(mapperMap);\n                }else{\n\n                    \/\/\u662f\u6ce8\u89e3\u914d\u7f6e\n                    daoPath = element.attributeValue(\"class\");\n                    \/\/\u4ecedao\u63a5\u53e3\u4e2d\u83b7\u53d6\u5bf9\u5e94\u7684\u914d\u7f6e\n                    Map&lt;String,Mapper> mapperMap = annotationLoader(daoPath);\n                    configurantion.setMapperMap(mapperMap);\n                }\n            }\n\n        } catch (DocumentException e) {\n            e.printStackTrace();\n        }\n\n        return  configurantion;\n    }\n\/**\n * @Description :\u901a\u8fc7\u53cd\u5c04\u83b7\u53d6dao\u4e2d\u6ce8\u89e3\u7684value\u4ee5\u53ca\u5168\u9650\u5b9a\u65b9\u6cd5\u540d\n * @Date 10:09 2020\/7\/26 0026\n * @Param * @param daoPath \uff1a\n * @return java.util.Map&lt;java.lang.String,mybatis.Mapper>\n **\/\n    private static Map&lt;String, Mapper> annotationLoader(String daoPath) throws Exception {\n        Map&lt;String, Mapper> mapperMap = new HashMap&lt;>();\n        \/\/\u83b7\u53d6dao\u7684\u5b57\u8282\u7801\u6587\u4ef6\n        Class&lt;?> aClass = Class.forName(daoPath);\n        \/\/\u83b7\u53d6\u6240\u6709\u7684\u65b9\u6cd5\n        Method&#91;] methods = aClass.getMethods();\n        \/\/\u904d\u5386\u6bcf\u4e00\u4e2a\u65b9\u6cd5\n        for (Method method : methods){\n            \/\/\u5224\u65ad\u662f\u5426\u662f\u6ce8\u89e3\n            boolean isAnnotation = method.isAnnotationPresent(Select.class);\n            if (isAnnotation){\n                Mapper mapper = new Mapper();\n                \/\/\u662fselect\u6ce8\u89e3\n                Select select = method.getAnnotation(Select.class);\n                \/\/\u83b7\u53d6select\u6ce8\u89e3\u7684\u503c,\u5373\u83b7\u53d6sql\u8bed\u53e5\n                String sql = select.value();\n                \/\/\u83b7\u53d6\u65b9\u6cd5\u540d\n                String methodName = method.getName();\n                \/\/\u7ec4\u5efakey\n                String key = daoPath+\".\"+methodName;\n                \/\/\u83b7\u53d6\u8fd4\u56de\u7c7b\u578b\n                Type returnType = method.getGenericReturnType();\n                \/\/\u5224\u65ad\u662f\u5426\u662f\u53c2\u6570\u5316\u7c7b\u578b\n                if (returnType instanceof ParameterizedType){\n                    \/\/\u5f3a\u8f6c\u6210\u6cdb\u578b\u53c2\u6570\u7c7b\u578b,\u6bd4\u5982 List&lt;User>\n                    ParameterizedType parameterizedType = (ParameterizedType) returnType;\n                    \/\/\u83b7\u53d6\u5b9e\u9645\u53c2\u6570\u7c7b\u578b\u6570\u7ec4\n                    Type&#91;] actualTypeArguments = parameterizedType.getActualTypeArguments();\n                    \/\/\u83b7\u53d6List&lt;User>\u4e2d\u7684User\u6cdb\u578b\u5b9e\u9645\u53c2\u6570\n                   Class actualType = (Class) actualTypeArguments&#91;0];\n                   \/\/\u83b7\u53d6\u5b9e\u9645\u53c2\u6570\u7c7b\u578b\u5168\u9650\u5b9a\u7c7b\u540d\n                    String actualTypeName = actualType.getName();\n                   mapper.setResultTypePath(actualTypeName);\n                }else{\n                    \/\/\u4e0d\u662f\u6cdb\u578b\u5316\u53c2\u6570\uff0c\u662f\u5b9e\u4f53\u7c7b\n                    Class&lt;?> methodReturnType = method.getReturnType();\n                    \/\/\u83b7\u53d6\u8fd4\u56de \u7c7b\u578b\u7684\u5168\u9650\u5b9a\u7c7b\u540d\n                    String methodReturnTypeName = methodReturnType.getName();\n                    mapper.setResultTypePath(methodReturnTypeName);\n                }\n             mapper.setSql(sql);\n             mapper.setSqlType(Mapper.SELECT);\n             mapperMap.put(key,mapper);\n\n            }\n        }\n\n        return mapperMap;\n    }\n}<\/code><\/pre>\n\n\n\n<p>5.\u5176\u4f59\u4ee3\u7801\u4e0d\u9700\u8981\u6539\u52a8\uff0c\u548cXML\u914d\u7f6e\u7684\u4ee3\u7801\u4e00\u6837\uff0cUserDaoTest.java\u6587\u4ef6\u4e5f\u4e0d\u9700\u8981\u6539\u52a8\uff0c\u76f4\u63a5\u8fd0\u884c\u5f97\u5230\u6d4b\u8bd5\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img   class=\"lazyload\" data-src=\"https:\/\/img-blog.csdnimg.cn\/20200727013837594.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTE4NzAwMjI=,size_16,color_FFFFFF,t_70\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"\u6d4b\u8bd5\u7ed3\u679c\"\/><\/figure >\n<noscript><img src=\"https:\/\/img-blog.csdnimg.cn\/20200727013837594.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTE4NzAwMjI=,size_16,color_FFFFFF,t_70\" alt=\"\u6d4b\u8bd5\u7ed3\u679c\"\/><\/figure><\/noscript>\n\n\n\n<p>\u672c\u6b21\u539f\u7801\u5df2\u7ecf\u6253\u5305\u4e0a\u4f20\u5230\u670d\u52a1\u5668\uff0c\u5982\u6709\u9700\u8981\u8bf7\u81ea\u884c\u4e0b\u8f7d\u540e\u89e3\u538b\u5bfc\u5165Idea\u5373\u53ef\uff1a<a href=\"http:\/\/moyisuiying.com\/wp-content\/uploads\/2020\/07\/mybatis-self_2.rar\">http:\/\/moyisuiying.com\/wp-content\/uploads\/2020\/07\/mybatis-self_2.rar<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u81ea\u5b9a\u4e49\u5b9e\u73b0mybatis\u6846\u67b6\u7684\u7b80\u5355\u5b9e\u73b0-\u642d\u5efa\u6ce8\u89e3\u5f00\u53d1<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[20,13,25],"_links":{"self":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts\/142"}],"collection":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/comments?post=142"}],"version-history":[{"count":1,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts\/142\/revisions"}],"predecessor-version":[{"id":216,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts\/142\/revisions\/216"}],"wp:attachment":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/media?parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/categories?post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/tags?post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}