{"id":169,"date":"2020-07-26T02:45:07","date_gmt":"2020-07-25T18:45:07","guid":{"rendered":"http:\/\/121.89.208.222\/?p=131"},"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%baxml%e5%bc%80%e5%8f%91","status":"publish","type":"post","link":"http:\/\/moyisuiying.com\/index.php\/mybatis\/169.html","title":{"rendered":"\u81ea\u5b9a\u4e49\u5b9e\u73b0mybatis\u6846\u67b6\u7684\u7b80\u5355\u5b9e\u73b0-\u642d\u5efaXML\u5f00\u53d1"},"content":{"rendered":"\n<p>mybatis\u6846\u67b6\u4f5c\u4e3a\u4e00\u4e2a\u6d41\u884c\u7684\u548c\u6570\u636e\u5e93\u6253\u4ea4\u9053\u7684\u6301\u4e45\u5c42\u6846\u67b6\uff0c\u5728\u6211\u4eec\u5b9e\u9645\u5f00\u53d1\u4e2d\u8fd0\u7528\u5e7f\u6cdb\uff0c\u5176\u6846\u67b6\u7684\u8bbe\u8ba1\u601d\u60f3\u503c\u5f97\u6211\u4eec\u8fd9\u4e9b\u65b0\u624b\u5b66\u4e60\u3002\u7b14\u8005\u8fd1\u51e0\u5929\u5f00\u59cb\u5b66\u4e60mybatis\u6846\u67b6\uff0c\u4fbf\u81ea\u5df1\u53c2\u7167mybatis\u6846\u67b6\u7684\u6e90\u7801\u4ee5\u53ca\u5176\u5b83\u4e00\u4e9b\u76f8\u5173\u6587\u732e\uff0c\u81ea\u5b9a\u4e49\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684mybatis\u6846\u67b6\u3002\u4e0b\u9762\u4fbf\u5f00\u59cb\u8fdb\u5165\u6b63\u9898\uff1a<br>1.\u6211\u4eec\u5728\u4f7f\u7528mybatis\u6846\u67b6\u7684\u65f6\u5019\uff0c\u4e00\u822c\u4f7f\u7528\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>            \/\/\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\n            InputStream inputStream = Resources.getResourceAsStream(\"mybatis-config.xml\");\n            \/\/\u5b9e\u4f8b\u5316SqlSessionFactoryBuilder\n            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();\n          \/\/\u6784\u5efaSqlSessionFactory\n            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);\n           \/\/\u4ea7\u751f\u4e00\u4e2aSqlSession\n            SqlSession sqlSession = sqlSessionFactory.openSession();\n            \/\/\u5229\u7528SqlSession\u521b\u5efaUserDAao\u7684\u4ee3\u7406\u5bf9\u8c61\n            UserDao userDao = sqlSession.getMapper(UserDao.class);\n            \/\/\u5229\u7528\u4ee3\u7406\u5bf9\u8c61\u6267\u884c\u65b9\u6cd5\n            List&lt;User> userList = userDao.getAll();\n            \/\/\u5173\u95ed\u8d44\u6e90\n            sqlSession.close();<\/code><\/pre>\n\n\n\n<p>\u6b64\u6b21\u5c31\u53c2\u8003mybatis\u7684\u4f7f\u7528\u6b65\u9aa4\uff0c\u6211\u4eec\u4e0d\u5bfc\u5165mybatis\u7684\u4f9d\u8d56\u5305\uff0c\u9010\u4e2a\u81ea\u5b9a\u4e49\u5b9e\u73b0\u8be5\u529f\u80fd\u6240\u9700\u8981\u7684\u7c7b\u3002<br>2.\u521a\u5f00\u59cb\u5982\u56fe\u6240\u793a\uff1a<br><img  src=\"https:\/\/img-blog.csdnimg.cn\/20200726012002686.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTE4NzAwMjI=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><br>\u8fd9\u4e9b\u5fc5\u8981\u7684\u7c7b\u62a5\u9519\uff0c\u6211\u4eec\u9700\u8981\u81ea\u5df1\u624b\u52a8\u5b9a\u4e49\u521b\u5efa\u3002<br>2.1\u9996\u5148\u521b\u5efaResources\u7c7b\u3002\u5e76\u4e14\u521b\u5efa\u4e00\u4e2astatic\u7684getResourceAsStream\uff08String filePath\uff09\u65b9\u6cd5\u3002<br>2.2\u5176\u6b21\u521b\u5efaSqlSessionFactoryBuilder\u7c7b\uff0c\u5e76\u4e14\u521b\u5efa\u4e00\u4e2abuild\uff08InputStream inputStream\uff09\u65b9\u6cd5<br>2.3\u518d\u521b\u5efaSqlSessionFactory\u63a5\u53e3\uff08\u6839\u636emybatis\u7684\u6e90\u7801\u53ef\u4ee5\u77e5\u9053\u8fd9\u4e2a\u662f\u63a5\u53e3\uff09\uff0c\u7136\u540e\u6dfb\u52a0\u4e00\u4e2aopenSession\uff08\uff09\u65b9\u6cd5\u3002<br>2.4\u6700\u540e\u521b\u5efaSqlSession\u63a5\u53e3\uff08\u6839\u636emybatis\u7684\u6e90\u7801\u53ef\u4ee5\u77e5\u9053\u8fd9\u4e2a\u662f\u63a5\u53e3\uff09\u3002\u7136\u540e\u521b\u5efa\u4e00\u4e2agetMapper\uff08Class daoClass\uff09\u548cclose\uff08\uff09\u65b9\u6cd5\u3002<br><img  class=\"lazyload\" data-src=\"https:\/\/img-blog.csdnimg.cn\/20200726012858168.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=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><br>\u867d\u7136\u8fd9\u4e9b\u7c7b\u548c\u65b9\u6cd5\u8fd8\u6ca1\u6709\u771f\u6b63\u5b9e\u73b0\uff0c\u4f46\u662f\u6b64\u65f6\u53d1\u73b0\u5df2\u7ecf\u4e0d\u62a5\u9519\u4e86\u3002<br>3.\u5728resources\u8d44\u6e90\u6839\u76ee\u5f55\u4e2d\u521b\u5efa\u914d\u7f6e\u6587\u4ef6mybatis-config.xml<\/p >\n<noscript><img src=\"https:\/\/img-blog.csdnimg.cn\/20200726012002686.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTE4NzAwMjI=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><br>\u8fd9\u4e9b\u5fc5\u8981\u7684\u7c7b\u62a5\u9519\uff0c\u6211\u4eec\u9700\u8981\u81ea\u5df1\u624b\u52a8\u5b9a\u4e49\u521b\u5efa\u3002<br>2.1\u9996\u5148\u521b\u5efaResources\u7c7b\u3002\u5e76\u4e14\u521b\u5efa\u4e00\u4e2astatic\u7684getResourceAsStream\uff08String filePath\uff09\u65b9\u6cd5\u3002<br>2.2\u5176\u6b21\u521b\u5efaSqlSessionFactoryBuilder\u7c7b\uff0c\u5e76\u4e14\u521b\u5efa\u4e00\u4e2abuild\uff08InputStream inputStream\uff09\u65b9\u6cd5<br>2.3\u518d\u521b\u5efaSqlSessionFactory\u63a5\u53e3\uff08\u6839\u636emybatis\u7684\u6e90\u7801\u53ef\u4ee5\u77e5\u9053\u8fd9\u4e2a\u662f\u63a5\u53e3\uff09\uff0c\u7136\u540e\u6dfb\u52a0\u4e00\u4e2aopenSession\uff08\uff09\u65b9\u6cd5\u3002<br>2.4\u6700\u540e\u521b\u5efaSqlSession\u63a5\u53e3\uff08\u6839\u636emybatis\u7684\u6e90\u7801\u53ef\u4ee5\u77e5\u9053\u8fd9\u4e2a\u662f\u63a5\u53e3\uff09\u3002\u7136\u540e\u521b\u5efa\u4e00\u4e2agetMapper\uff08Class daoClass\uff09\u548cclose\uff08\uff09\u65b9\u6cd5\u3002<br><img src=\"https:\/\/img-blog.csdnimg.cn\/20200726012858168.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTE4NzAwMjI=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><br>\u867d\u7136\u8fd9\u4e9b\u7c7b\u548c\u65b9\u6cd5\u8fd8\u6ca1\u6709\u771f\u6b63\u5b9e\u73b0\uff0c\u4f46\u662f\u6b64\u65f6\u53d1\u73b0\u5df2\u7ecf\u4e0d\u62a5\u9519\u4e86\u3002<br>3.\u5728resources\u8d44\u6e90\u6839\u76ee\u5f55\u4e2d\u521b\u5efa\u914d\u7f6e\u6587\u4ef6mybatis-config.xml<\/p><\/noscript>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n&lt;!--mybatis\u7684\u4e3b\u914d\u7f6e\u6587\u4ef6-->\n&lt;configuration>\n    &lt;environments default=\"development\">\n        &lt;environment id=\"development\">\n            &lt;!-- \u914d\u7f6e\u4e8b\u52a1\u7c7b\u578b  -->\n            &lt;transactionManager type=\"JDBC\"\/>\n            &lt;!--   \u914d\u7f6e\u8fde\u63a5\u4fe1\u606f         -->\n            &lt;dataSource type=\"POOLED\">\n                &lt;!--       \u914d\u7f6e\u52a0\u8f7dMySQL\u7684\u9a71\u52a8         -->\n                &lt;property name=\"driver\" value=\"com.mysql.cj.jdbc.Driver\"\/>\n                &lt;!--      MySQL\u8fde\u63a5\u7684\u6570\u636e\u5e93\u5730\u5740\uff0c\u7b14\u8005\u8fd9\u91cc\u662f\u5728\u672c\u5730localhost\u4e0a\u6d4b\u8bd5\uff0c\u5e76\u4e14\u6570\u636e\u5e93\u7684\u540d\u79f0\u4e3amybatisdemo     -->\n                &lt;property name=\"url\" value=\"jdbc:mysql:\/\/localhost:3306\/mybatisdemo?serverTimezone=UTC\"\/>\n                &lt;!--      MySQL\u8fde\u63a5\u7684\u7528\u6237\u540d         -->\n                &lt;property name=\"username\" value=\"root\"\/>\n                &lt;!--      MySQL\u7684\u8fde\u63a5\u5bc6\u7801        -->\n                &lt;property name=\"password\" value=\"root\"\/>\n            &lt;\/dataSource>\n        &lt;\/environment>\n    &lt;\/environments>\n    &lt;!--   \u6307\u5b9a\u914d\u7f6e\u6587\u4ef6\u7684\u7684\u4f4d\u7f6e\uff0c\u6620\u5c04\u6587\u4ef6\u6307\u7684\u662f\u6bcf\u4e2adao\u7684\u4f4d\u7f6e-->\n    &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 resource=\"dao\/UserDao.xml\">&lt;\/mapper>\n    &lt;\/mappers>\n&lt;\/configuration><\/code><\/pre>\n\n\n\n<p>4.\u9996\u5148\u5b9e\u73b0Resources\u7c7b\u7684getResourceAsStream\u65b9\u6cd5\uff0c\u5176\u5177\u4f53\u4ee3\u7801\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> \/**\n     * @Description :\u6839\u636e\u4f20\u5165\u7684\u8d44\u6e90\u6587\u4ef6\u540d\u83b7\u53d6\u5bf9\u5e94\u7684\u8f93\u5165\u6d41\n     * @Date 15:17 2020\/7\/25 0025\n     * @Param * @param filePath \uff1a\u8d44\u6e90\u6587\u4ef6\u540d\uff0c\u6bd4\u5982 demo.txt\n     * @return java.io.InputStream\n     **\/\n    public static InputStream getResourceAsStream(String filePath) {\n        return Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);\n    }<\/code><\/pre>\n\n\n\n<p>5.\u63a5\u7740\u5b9e\u73b0SqlSessionFactoryBuilder\u7684build\u65b9\u6cd5\uff0c\u5176\u5177\u4f53\u4ee3\u7801\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  \/**\n     * @Description :\u6839\u636e\u914d\u7f6e\u6587\u4ef6\u8f93\u5165\u6d41\u52a0\u8f7d\u914d\u7f6e\u4fe1\u606f\u5e76\u5c01\u88c5\u5230\u4e00\u4e2a\u5bf9\u8c61\u4e2d\u53bb\n     * @Date 1:36 2020\/7\/26 0026\n     * @Param * @param inputStream \uff1a\u914d\u7f6e\u6587\u4ef6\u8f93\u5165\u6d41\n     * @return mybatis.SqlSessionFactory\n     **\/\n    public SqlSessionFactory build(InputStream inputStream) {\n        \/\/\u8c03\u7528\u5de5\u5177\u7c7b\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\u5e76\u5c06\u914d\u7f6e\u4fe1\u606f\u5c01\u88c5\u5230\u4e00\u4e2aConfiguration\u7c7b\u4e2d\n        Configurantion configurantion = MybatisUtil.LoaderConfiguration(inputStream);\n        \/\/\u5229\u7528SqlSessionFactory\u63a5\u53e3\u7684\u4e00\u4e2a\u5b9e\u73b0\u7c7b\n        return new DefaultSqlSessionFactory(configurantion);\n    }<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u7c7b\u4e2d\u6211\u4eec\u53d1\u73b0\u9700\u8981\u5bf9\u914d\u7f6e\u4fe1\u606f\u8fdb\u884c\u5c01\u88c5\uff0c\u5c06\u6570\u636e\u5e93\u8fde\u63a5\u4e2d\u7684driver\uff0curl,userName\uff0cpassword\u7b49\u4fe1\u606f\u5c01\u88c5\u5230\u4e00\u4e2a\u5bf9\u8c61Configuration\u4e2d\u53bb\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package mybatis;\nimport java.util.HashMap;\nimport java.util.Map;\n\n\/**\n * Classname:Configurantion\n *\n * @description:\u6570\u636e\u5e93\u8fde\u63a5\u914d\u7f6e\u4fe1\u606f\u7c7b\n * @author: \u964c\u610f\u968f\u5f71\n * @Date: 2020-07-25 15:40\n * @Version: 1.0\n **\/\npublic class Configurantion {\n    \/\/\u8fde\u63a5\u9a71\u52a8\n    private  String driver;\n    \/\/\u8fde\u63a5\u7528\u6237\u540d\n    private  String username;\n    \/\/\u5bc6\u7801\n    private  String password;\n    \/\/\u8fde\u63a5URL\n    private String url;\n    \/\/dao\u63a5\u53e3\u7684\u5173\u952e\u914d\u7f6e\u4fe1\u606fmap\n    private Map&lt;String,Mapper> mapperMap = new HashMap&lt;>();\n\n    public Map&lt;String, Mapper> getMapperMap() {\n        return mapperMap;\n    }\n\n    public void setMapperMap(Map&lt;String, Mapper> mapperMap) {\n        this.mapperMap.putAll(mapperMap);\n    }\n\n    public String getDriver() {\n        return driver;\n    }\n\n    public void setDriver(String driver) {\n        this.driver = driver;\n    }\n\n    public String getUsername() {\n        return username;\n    }\n\n    public void setUsername(String username) {\n        this.username = username;\n    }\n\n    public String getPassword() {\n        return password;\n    }\n\n    public void setPassword(String password) {\n        this.password = password;\n    }\n\n    public String getUrl() {\n        return url;\n    }\n\n    public void setUrl(String url) {\n        this.url = url;\n    }\n\n    @Override\n    public String toString() {\n        return \"Configurantion{\" +\n                \"driver='\" + driver + '\\'' +\n                \", username='\" + username + '\\'' +\n                \", password='\" + password + '\\'' +\n                \", url='\" + url + '\\'' +\n                '}';\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u6211\u4eec\u7ee7\u7eed\u521b\u5efa\u4e00\u4e2a\u5de5\u5177\u7c7bMybatisUtil\uff0c\u5e76\u6dfb\u52a0\u4e00\u4e2astatic\u65b9\u6cd5LoaderConfiguration\u7528\u4e8e\u52a0\u8f7d\u914d\u7f6e\u4fe1\u606f\u5e76\u5c01\u88c5\u5230Configuration\u5bf9\u8c61\u4e2d\u7136\u540e\u8fd4\u56de\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> \/**\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) {\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                    \/\/\u662f\u6ce8\u89e3\u914d\u7f6e\n                }\n            }\n\n        } catch (DocumentException e) {\n            e.printStackTrace();\n        }\n\n        return  configurantion;\n    }<\/code><\/pre>\n\n\n\n<p>\u5728\u8fdb\u884c\u89e3\u6790\u914d\u7f6e\u6587\u4ef6\u65f6\uff0c\u4f7f\u7528\u4e86dom4j\u6280\u672f\u8fdb\u884c\u89e3\u6790xml\u6587\u4ef6\u3002\u5728\u8fdb\u884c\u89e3\u6790\u7684\u8fc7\u7a0b\u4e2d\uff0c\u8003\u8651\u5230mappers\u7684\u914d\u7f6e\u6709xml\u914d\u7f6e\u548c\u6ce8\u89e3\u914d\u7f6e\uff0c\u6240\u4ee5\u9700\u8981\u5206\u5f00\u6765\uff0c\u6b64\u6b21\u9996\u5148\u8003\u8651\u7684\u662f\u7ee7\u7eedxml\u7684\u914d\u7f6e\u3002\u4e3a\u4e86\u6211\u4eec\u5b9e\u73b0\u529f\u80fd\uff0c\u6211\u4eec\u9700\u8981\u4ecedao.xml\u914d\u7f6e\u6587\u4ef6\u4e2d\u83b7\u53d6\u63a5\u53e3\u7684\u5168\u9650\u5b9a\u65b9\u6cd5\u540d\u4ee5\u53ca\u8fd4\u7c7b\u578b\u7684\u5168\u9650\u5b9a\u7c7b\u540d\uff0c\u7136\u540e\u5c01\u88c5\u5230\u4e00\u4e2amapper\u4e2d\uff0c\u6bd4\u5982UserDao\u4e2d\u7684getAll(\uff09\u65b9\u6cd5\uff0c\u90a3\u4e48\u5176\u5168\u9650\u5b9a\u65b9\u6cd5\u540d\u4e3a\u201cdao.UserDao.getAll\u201d,\u8868\u793a\u5305dao\u4e0b\u7684\u63a5\u53e3UserDao\u4e2d\u7684getAll\u65b9\u6cd5\uff0c\u201centity.User\u201d\u8868\u793aentity\u5305\u4e0b\u7684User\u7c7b\uff0c\u6bcf\u4e00\u4e2a\u65b9\u6cd5\u5bf9\u5e94\u4e00\u6761Sql\u8bed\u53e5\uff0c\u540c\u65f6\u5bf9\u5e94\u4e00\u4e2amapper\uff0c\u6240\u4ee5\u4e00\u4e2aConfiguration\u4e2d\u9700\u8981\u6709\u591a\u4e2amapper\uff0c\u53ef\u4ee5\u4f7f\u7528Map\u6765\u5b58\u53d6\uff0c\u5b83\u7684key\u5c31\u662f\u63a5\u53e3\u7684\u5168\u9650\u5b9a\u65b9\u6cd5\u540d\uff0cvalue\u5c31\u662f\u5bf9\u5e94\u7684mapper\uff0c\u5176Mapper\u7c7b\u7684\u5177\u4f53\u4ee3\u7801\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package mybatis;\n\n\/**\n * Classname:Mapper\n *\n * @description:\u5c01\u88c5sql\u8bed\u53e5\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u5168\u9650\u5b9a\u7c7b\u540d\n * @author: \u964c\u610f\u968f\u5f71\n * @Date: 2020-07-25 17:04\n * @Version: 1.0\n **\/\npublic class Mapper {\n    \/\/\u8fd4\u56de\u503c\u4e86\u7c7b\u578b\u7684\u7c7b\u9650\u5b9a\u540d\n    private String resultTypePath;\n    \/\/sql\u8bed\u53e5\n    private String sql;\n    \/\/sql\u64cd\u4f5c\u7c7b\u578b\n    private  int sqlType;\n    \/**\u67e5\u8be2\u7c7b\u578b*\/\n    public  static int SELECT = 1;\n    \/**\u63d2\u5165\u7c7b\u578b*\/\n    public  static int INSERT = 2;\n    \/**\u5220\u9664\u7c7b\u578b*\/\n    public  static int DELETE = 3;\n    \/**\u66f4\u65b0\u7c7b\u578b*\/\n    public  static int UPDATE = 4;\n    public String getResultTypePath() {\n        return resultTypePath;\n    }\n    public int getSqlType() {\n        return sqlType;\n    }\n\n    public void setSqlType(int sqlType) {\n        this.sqlType = sqlType;\n    }\n    public void setResultTypePath(String resultTypePath) {\n        this.resultTypePath = resultTypePath;\n    }\n\n    public String getSql() {\n        return sql;\n    }\n\n    public void setSql(String sql) {\n        this.sql = sql;\n    }\n\n    @Override\n    public String toString() {\n        return \"Mapper{\" +\n                \"resultTypePath='\" + resultTypePath + '\\'' +\n                \", sql='\" + sql + '\\'' +\n                \", sqlType=\" + sqlType +\n                '}';\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u5c06\u5c01\u88c5\u597d\u7684Mapper\u5b58\u5165\u5230Configuration\u4e2d\u53bb\u3002\u6211\u4eec\u83b7\u53d6\u4e86Configuration\u540e\uff0c\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u63a5\u53e3SqlSessionFactory\u7684\u4e00\u4e2a\u5b9e\u73b0\u7c7bDefaultSqlSessionFactory\u8fdb\u884c\u8fd4\u56de\u4e00\u4e2aSqlSessionFactory\u3002<br>5.\u7d27\u63a5\u7740\u5b9e\u73b0SqlSessionFactory\u63a5\u53e3\u4e2d\u7684openSession\u65b9\u6cd5\uff0c\u5b9e\u9645\u4e0a\u662f\u5728SqlSessionFactory\u7684\u4e00\u4e2a\u5b9e\u73b0\u7c7bDefaultSqlSessionFactory\u4e2d\u8fdb\u884c\u5b9e\u73b0\uff0c\u5176\u4ee3\u7801\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * Classname:DefaultSqlSessionFactory\n *\n * @description:\u6784\u5efaSQLSession\u7684\u9ed8\u8ba4\u5de5\u5382\n * @author: \u964c\u610f\u968f\u5f71\n * @Date: 2020-07-25 17:52\n * @Version: 1.0\n **\/\npublic class DefaultSqlSessionFactory  implements SqlSessionFactory {\n     private Configurantion configurantion ;\n     public DefaultSqlSessionFactory(Configurantion configurantion){\n         this.configurantion = configurantion;\n     }\n    @Override\n    public SqlSession openSession() {\n        return new DefaultSqlSession(configurantion);\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u53d1\u73b0\u5b9e\u9645\u4e0a\u5728\u8c03\u7528sqlSessionFactory.openSession();\u65f6\uff0c\u5b9e\u9645\u4e0a\u662f\u8c03\u7528new DefaultSqlSession(configurantion)\u540e\u83b7\u53d6\u4e00\u4e2aSqlSession\u63a5\u53e3\u7684\u5b9e\u73b0\u7c7b\u3002<br>6.\u5b9e\u73b0 sqlSession.getMapper(UserDao.class)\uff0c\u8be5\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u4e86\u52a8\u6001\u4ee3\u7406\u7684\u65b9\u6cd5\u3002\u5176\u4ee3\u7801\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> @Override\n    public &lt;T> T getMapper(Class&lt;T> daoClass) {\n        Object proxyInstance = Proxy.newProxyInstance(daoClass.getClassLoader(), new Class&#91;]{daoClass}, new MapperProxy(configurantion.getMapperMap(), connection));\n        return (T) proxyInstance;\n    }<\/code><\/pre>\n\n\n\n<p>\u5728\u8be5\u65b9\u6cd5\u8fc7\u7a0b\u4e2d\uff0c\u4f7f\u7528\u4e86\u52a8\u6001\u4ee3\u7406\u8fdb\u884c\u52a0\u5f3a\u9700\u8981\u88ab\u4ee3\u7406\u7684\u5bf9\u8c61\u3002<br><strong>\u52a8\u6001\u4ee3\u7406\u5e95\u5c42\u5b9e\u73b0<\/strong><br>\u52a8\u6001\u4ee3\u7406\u5177\u4f53\u6b65\u9aa4\uff1a<br>6.1\u901a\u8fc7\u5b9e\u73b0 InvocationHandler \u63a5\u53e3\u521b\u5efa\u81ea\u5df1\u7684\u8c03\u7528\u5904\u7406\u5668\uff1b<br>6.2\u901a\u8fc7\u4e3a Proxy \u7c7b\u6307\u5b9a ClassLoader \u5bf9\u8c61\u548c\u4e00\u7ec4 interface \u6765\u521b\u5efa\u52a8\u6001\u4ee3\u7406\u7c7b\uff1b<br>6.3\u901a\u8fc7\u53cd\u5c04\u673a\u5236\u83b7\u5f97\u52a8\u6001\u4ee3\u7406\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u5176\u552f\u4e00\u53c2\u6570\u7c7b\u578b\u662f\u8c03\u7528\u5904\u7406\u5668\u63a5\u53e3\u7c7b\u578b\uff1b<br>6.4\u901a\u8fc7\u6784\u9020\u51fd\u6570\u521b\u5efa\u52a8\u6001\u4ee3\u7406\u7c7b\u5b9e\u4f8b\uff0c\u6784\u9020\u65f6\u8c03\u7528\u5904\u7406\u5668\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u88ab\u4f20\u5165\u3002<br>newProxyInstance\u200b(ClassLoader loader,\uff08\u88ab\u4ee3\u7406\u5bf9\u8c61\u7684\u7c7b\u52a0\u8f7d\u5668\uff09<br>Class[] interfaces,\uff08\u88ab\u4ee3\u7406\u5bf9\u50cf\u63a5\u53e3\u7684\u5b57\u8282\u7801\u6570\u7ec4\uff09<br>InvocationHandler h\uff08\u7531\u4ee3\u7406\u5b9e\u4f8b\u7684\u8c03\u7528\u5904\u7406\u7a0b\u5e8f\u5b9e\u73b0\u7684\u63a5\u53e3\u3002\uff09<br>\uff09<br>\u4e3a\u4e86\u65b9\u4fbf\uff0c\u6dfb\u52a0InvocationHandler \u63a5\u53e3\u7684\u4e00\u4e2a\u5b9e\u73b0\u7c7bMapperProxy,\u5176\u4ee3\u7801\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package mybatis;\n\nimport utils.DefaultResultSetHandler;\nimport utils.MyBatisJDBCUtil;\n\nimport java.lang.reflect.InvocationHandler;\nimport java.lang.reflect.Method;\nimport java.sql.Connection;\nimport java.util.Collection;\nimport java.util.List;\nimport java.util.Map;\n\n\/**\n * Classname:MapperProxy\n *\n * @description:Mapper\u7684\u4ee3\u7406\u5bf9\u8c61\u7c7b\n * @author: \u964c\u610f\u968f\u5f71\n * @Date: 2020-07-25 18:06\n * @Version: 1.0\n **\/\npublic class MapperProxy implements InvocationHandler {\n\n    private Map&lt;String, Mapper> mapperMap;\n    private Connection connection;\n    public MapperProxy( Map&lt;String, Mapper> mapperMap,Connection connection) {\n        this.mapperMap = mapperMap;\n        this.connection = connection;\n    }\n\n    @Override\n    public Object invoke(Object proxy, Method method, Object&#91;] args) throws Throwable {\n        \/\/\u83b7\u53d6\u65b9\u6cd5\u540d\n        String methodName = method.getName();\n        \/\/\u83b7\u53d6\u65b9\u6cd5\u6240\u5728\u7684\u7c7b\u7684\u540d\u79f0\n        String className = method.getDeclaringClass().getName();\n        \/\/\u7ec4\u5408\u6210\u4e00\u4e2akey\n        String key = className+\".\"+methodName;\n        \/\/\u83b7\u53d6mapperMap\u4e2d\u7684\u7684\u5bf9\u5e94mapper\n        Mapper mapper = mapperMap.get(key);\n        \/\/\u5224\u65ad\u662f\u5426\u6709\u5bf9\u5e94\u7684mapper\n        if (mapper == null){\n            throw  new RuntimeException(\"\u4f20\u7684\u53c2\u6570\u6709\u8bef\uff01\");\n        }\n\n        \/\/\u6267\u884c\u67e5\u8be2\u8bed\u53e5\u8fd4\u56de\u7ed3\u679c\n        List&lt;Object> objectList = MyBatisJDBCUtil.query(mapper, connection, new DefaultResultSetHandler(),args);\n        \/\/\u83b7\u53d6\u8fd4\u56de\u7c7b\u578b\n        Class&lt;?> returnType = method.getReturnType();\n        \/\/\u83b7\u53d6\u96c6\u5408\u7684\u5b57\u8282\u7801\u7528\u4e8e\u5224\u65ad\u8fd4\u56de\u7c7b\u578b\u662f\u5426\u662f\u96c6\u5408\u7684\u5b50\u7c7b\n        Class&lt;Collection> collectionClass = Collection.class;\n        if (objectList!= null &amp;&amp; objectList.size() != 0){\n            if (collectionClass.isAssignableFrom(returnType)){\n                \/\/\u5982\u679c\u8fd4\u56de\u7c7b\u578b\u662f\u96c6\u5408\u5219\u8fd4\u56de\u4e00\u4e2a\u96c6\u5408\n                return objectList;\n            }else{\n                \/\/\u8fd4\u56de\u7c7b\u578b\u662f\u4e00\u4e2a\u5bf9\u8c61\u5219\u76f4\u63a5\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\n                return  objectList.get(0);\n            }\n        }else{\n            return  null;\n        }\n\n\n\n    }\n}<\/code><\/pre>\n\n\n\n<p>Object invoke\u200b(Object proxy,Method method, Object[] args) throws Throwable\u5904\u7406\u4ee3\u7406\u5b9e\u4f8b\u4e0a\u7684\u65b9\u6cd5\u8c03\u7528\u5e76\u8fd4\u56de\u7ed3\u679c\u3002 \u5f53\u5728\u4e0e\u4e4b\u5173\u8054\u7684\u4ee3\u7406\u5b9e\u4f8b\u4e0a\u8c03\u7528\u65b9\u6cd5\u65f6\uff0c\u5c06\u5728\u8c03\u7528\u5904\u7406\u7a0b\u5e8f\u4e2d\u8c03\u7528\u6b64\u65b9\u6cd5\u3002<br>\u53c2\u6570<br>proxy - \u8c03\u7528\u8be5\u65b9\u6cd5\u7684\u4ee3\u7406\u5b9e\u4f8b<br>method - \u5bf9\u5e94\u4e8e\u5728\u4ee3\u7406\u5b9e\u4f8b\u4e0a\u8c03\u7528\u7684\u63a5\u53e3\u65b9\u6cd5\u7684\u65b9\u6cd5\u5b9e\u4f8b\u3002 \u65b9\u6cd5\u5bf9\u8c61\u7684\u58f0\u660e\u7c7b\u5c06\u662f\u65b9\u6cd5\u58f0\u660e\u7684\u63a5\u53e3\uff0c\u5b83\u53ef\u4ee5\u662f\u4ee3\u7406\u7c7b\u7ee7\u627f\u65b9\u6cd5\u7684\u4ee3\u7406\u63a5\u53e3\u7684\u8d85\u7ea7\u63a5\u53e3\u3002<br>args - \u5305\u542b\u4ee3\u7406\u5b9e\u4f8b\u4e0a\u65b9\u6cd5\u8c03\u7528\u4e2d\u4f20\u9012\u7684\u53c2\u6570\u503c\u7684\u5bf9\u8c61\u6570\u7ec4\uff0c\u5982\u679c\u63a5\u53e3\u65b9\u6cd5\u4e0d\u5e26\u53c2\u6570\uff0c null \u3002 \u539f\u59cb\u7c7b\u578b\u7684\u53c2\u6570\u5305\u542b\u5728\u9002\u5f53\u7684\u539f\u59cb\u5305\u88c5\u5668\u7c7b\u7684\u5b9e\u4f8b\u4e2d\uff0c\u4f8b\u5982java.lang.Integer\u6216java.lang.Boolean<br>\u5728\u8fdb\u884c\u5bf9\u88ab\u4ee3\u7406\u5bf9\u8c61\u7684\u63a5\u53e3\u65b9\u6cd5\u8fdb\u884c\u52a0\u5f3a\u7684\u65f6\u5019\uff0c\u9700\u8981\u7528\u5230\u4e0e\u6570\u636e\u5e93\u8fde\u63a5\u540e\u8fdb\u884c\u64cd\u4f5c\u7684\u5de5\u5177\u7c7bMyBatisJDBCUtil\uff0c\u5176\u5177\u4f53\u4ee3\u7801\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package utils;\n\nimport mybatis.Configurantion;\nimport mybatis.Mapper;\n\nimport java.sql.*;\nimport java.util.List;\n\n\/**\n * Classname:MyBatisJDBCUtil\n *\n * @description:\u83b7\u53d6sql\u8fde\u63a5\u7684\u5de5\u5177\u7c7b\n * @author: \u964c\u610f\u968f\u5f71\n * @Date: 2020-07-25 18:17\n * @Version: 1.0\n **\/\npublic class MyBatisJDBCUtil {\n    public static Connection getConnection(Configurantion configurantion) {\n        try {\n            \/\/\u52a0\u8f7d\u9a71\u52a8\n            Class.forName(configurantion.getDriver());\n            try {\n                \/\/\u83b7\u53d6\u8fde\u63a5\n                Connection connection = DriverManager.getConnection(configurantion.getUrl(), configurantion.getUsername(), configurantion.getPassword());\n                return  connection;\n            } catch (SQLException e) {\n                e.printStackTrace();\n            }\n        } catch (ClassNotFoundException e) {\n            e.printStackTrace();\n        }\n        return  null;\n    }\n  public  static &lt;T> List&lt;T> query(Mapper mapper, Connection connection, ResultSetHandler resultSetHandler, Object&#91;] param) throws Exception {\n      \/\/\u8bbe\u7f6e\u7981\u6b62\u81ea\u52a8\u63d0\u4ea4\n        connection.setAutoCommit(false);\n      PreparedStatement preparedStatement =  null;\n      List&lt;T> t = null;\n      try {\n          \/\/\u83b7\u53d6\u9884\u5904\u7406\u5bf9\u8c61\n          preparedStatement =  connection.prepareStatement(mapper.getSql());\n          \/\/\u8bbe\u7f6e\u5360\u4f4d\u53c2\u6570\n          if (param != null &amp;&amp; param.length != 0){\n              for (int i = 0; i &lt; param.length;i++){\n                  preparedStatement.setObject(i+1,param&#91;i]);\n              }\n          }\n          \/\/\u83b7\u53d6\u7ed3\u679c\u96c6\n          ResultSet resultSet = preparedStatement.executeQuery();\n          \/\/\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u5904\u7406\n          t = resultSetHandler.resultSet(resultSet,mapper.getResultTypePath());\n      }catch (Exception e){\n          e.printStackTrace();\n      }finally {\n          \/\/\u8bbe\u7f6e\u5141\u8bb8\u81ea\u52a8\u63d0\u4ea4\n          connection.setAutoCommit(true);\n          \/\/\u8fd4\u56de\u5904\u7406\u7ed3\u679c\n          return  t;\n      }\n\n\n\n  }\n    public static int update(String sql,Connection connection,String ...param) throws Exception {\n        \/\/\u8bbe\u7f6e\u7981\u6b62\u81ea\u52a8\u63d0\u4ea4\n        connection.setAutoCommit(false);\n        PreparedStatement preparedStatement =  null;\n        int  result = -1;\n        try {\n            \/\/\u83b7\u53d6\u9884\u5904\u7406\u5bf9\u8c61\n            preparedStatement =  connection.prepareStatement(sql);\n            \/\/\u8bbe\u7f6e\u5360\u4f4d\u53c2\u6570\n            if (param != null &amp;&amp; param.length != 0){\n                for (int i = 0; i &lt; param.length;i++){\n                    preparedStatement.setObject(i+1,param&#91;i]);\n                }\n            }\n            \/\/\u83b7\u53d6\u7ed3\u679c\u96c6\n           result = preparedStatement.executeUpdate();\n        }catch (Exception e){\n            e.printStackTrace();\n        }finally {\n            \/\/\u8bbe\u7f6e\u5141\u8bb8\u81ea\u52a8\u63d0\u4ea4\n            connection.setAutoCommit(true);\n            \/\/\u8fd4\u56de\u5904\u7406\u7ed3\u679c\n            return  result;\n        }\n\n\n\n    }\n\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u5de5\u5177\u7c7bMyBatisJDBCUtil\u4e2d\uff0c\u4f7f\u7528\u5230\u4e86ResultSetHandler\u63a5\u53e3\u7528\u4e8e\u5904\u7406\u6570\u636e\u5e93\u67e5\u8be2\u540e\u83b7\u53d6\u7684\u7ed3\u679c\u96c6Resultset,ResultSetHandler\u7684\u4e00\u4e2a\u5b50\u7c7b\u5b9e\u73b0DefaultResultSetHandler\uff0c\u5176\u5177\u4f53\u7684\u4ee3\u7801\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package utils;\n\nimport java.lang.reflect.Constructor;\nimport java.lang.reflect.Method;\nimport java.sql.ResultSet;\nimport java.util.ArrayList;\nimport java.util.Date;\nimport java.util.List;\n\n\/**\n * Classname:DefaultResultSetHandler\n * @description:\u5904\u7406\u7ed3\u679c\u96c6\n * @author: \u964c\u610f\u968f\u5f71\n * @Date: 2020-07-25 21:12\n * @Version: 1.0\n **\/\npublic class DefaultResultSetHandler&lt;T> implements ResultSetHandler {\n    \/**\n     * @param resultTypePath \uff1a\u9700\u8981\u5c01\u88c5\u5230\u7684\u5b9e\u4f53\u7c7b\n     * @return java.util.List&lt;T>\n     * @Description :\u5904\u7406\u5e76\u5c01\u88c5\u7ed3\u679c\u96c6\u5bf9\u8c61\u5230\u5bf9\u5e94\u7684\u5b9e\u4f53\u7c7b\u4e2d\u53bb\n     * @Date 22:58 2020\/7\/25 0025\n     * @Param * @param resultSet \uff1a\u5305\u542b\u6570\u636e\u5e93\u4e2d\u5b9e\u4f53\u7c7b\u8868\u4fe1\u606f\u7684\u7ed3\u679c\u96c6\n     **\/\n    @Override\n    public List&lt;T> resultSet(ResultSet resultSet, String resultTypePath) throws Exception {\n        \/\/\u901a\u8fc7\u53cd\u5c04\u83b7\u53d6\u8fd4\u56de\u7c7b\u578b\u7684\u5b57\u8282\u7801\n        Class&lt;T> obj = (Class&lt;T>) Class.forName(resultTypePath);\n        if (obj == null) {\n            throw new IllegalAccessException(\"\u5c01\u88c5\u7c7b\u578b\u6709\u8bef\uff01\");\n        }\n        \/\/\u83b7\u53d6\u6784\u9020\u5668\n        Constructor&lt;T> constructor = obj.getConstructor();\n        \/\/\u83b7\u53d6\u6240\u6709\u7684\u65b9\u6cd5\n        Method&#91;] objMethods = obj.getMethods();\n        List&lt;T> list = new ArrayList&lt;>();\n        while (resultSet.next()) {\n            \/\/\u5b9e\u4f8b\u5316\u4e00\u4e2a\u5b9e\u4f53\u7c7b\n            T t = constructor.newInstance();\n            \/\/\u83b7\u53d6\u5b9e\u4f53\u7c7b\u7684set\u65b9\u6cd5\n            for (Method method : objMethods) {\n                \/\/\u83b7\u53d6\u65b9\u6cd5\u7684\u540d\u5b57\n                String methodName = method.getName();\n                \/\/\u627e\u5230set\u65b9\u6cd5\n                if (methodName.startsWith(\"set\")) {\n                    \/\/\u83b7\u53d6set\u65b9\u6cd5\u5bf9\u5e94\u7684\u5c5e\u6027\u540d\u79f0\n                    String fieldName = methodName.substring(3);\n                    \/\/\u83b7\u53d6set\u65b9\u6cd5\u7684\u53c2\u6570\u7c7b\u578b,set\u65b9\u6cd5\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\n                    Class&lt;?> parameterType = method.getParameterTypes()&#91;0];\n                    \/\/\u83b7\u53d6set\u65b9\u6cd5\u53c2\u6570\u7c7b\u578b\n                    String parameterTypeName = parameterType.getName();\n                    \/\/\u6839\u636e\u5c5e\u6027\u540d\u4eceresult\u4e2d\u7684\u83b7\u53d6\u5bf9\u5e94\u6570\u636e\u5e93\u4e2d\u7684\u503c\u5e76\u586b\u5145\u5230\u5b9e\u4f53\u5bf9\u8c61\u4e2d\u56fd\n                    if (\"java.lang.String\".equals(parameterTypeName)) {\n                        method.invoke(t, resultSet.getString(fieldName));\n                    } else if (\"java.lang.Integer\".equals(parameterTypeName)) {\n                        method.invoke(t, resultSet.getInt(fieldName));\n                    } else if (\"java.lang.Double\".equals(parameterTypeName)) {\n                        method.invoke(t, resultSet.getDouble(fieldName));\n                    } else if (\"java.lang.Byte\".equals(parameterTypeName)) {\n                        method.invoke(t, resultSet.getByte(fieldName));\n                    } else if (\"java.lang.Float\".equals(parameterTypeName)) {\n                        method.invoke(t, resultSet.getFloat(fieldName));\n                    } else if (\"java.lang.Boolean\".equals(parameterTypeName)) {\n                        method.invoke(t, resultSet.getBoolean(fieldName));\n                    } else if (\"java.util.Date\".equals(parameterTypeName)) {\n                        \/\/\u5c06\u65e5\u671f\u8f6c\u5316\u4e3a\u6307\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\n                        String dateToStr = DateUtil.dateToStr(resultSet.getDate(fieldName));\n                        \/\/\u5c06\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\n                        Date date = DateUtil.strToDate(dateToStr);\n                        method.invoke(t, date);\n                    }\n                }\n\n            }\n            list.add(t);\n        }\n        return list;\n    }\n}<\/code><\/pre>\n\n\n\n<p>7.\u6211\u4eec\u8981\u81ea\u5df1\u5199\u4e00\u4e2adao\u63a5\u53e3\uff0c\u7528\u4e8e\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u5176\u4ee3\u7801\u4e3a:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package dao;\n\nimport entity.User;\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    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   User getOne(int id);\n\n}<\/code><\/pre>\n\n\n\n<p>\u901a\u8fc7 UserDao userDao = sqlSession.getMapper(UserDao.class);\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u4e00\u4e2a\u7ecf\u8fc7\u52a8\u6001\u4ee3\u7406\u6a21\u5f0f\u52a0\u5f3a\u540e\u7684UserDao\u63a5\u53e3\u7c7b\uff0c\u5f53\u8c03\u7528UserDao\u7684\u65b9\u6cd5\u65f6\uff0c\u4f1a\u76f8\u5e94\u81ea\u52a8\u8c03\u7528MapperProxy\u4e2d\u7684invoke\u65b9\u6cd5\uff0c\u4ece\u800cUserDao\u7684\u65b9\u6cd5\u4f1a\u76f8\u5e94\u5f97\u5230\u52a0\u5f3a\uff0c\u5373\u4f7f\u4e0d\u7528\u81ea\u5df1\u624b\u52a8UserDao\u63a5\u53e3\u7684\u5b9e\u73b0\u7c7b\uff0c\u4e5f\u80fd\u591f\u5b8c\u6210\u6211\u4eec\u60f3\u8981\u7684\u529f\u80fd\u3002<br>8.\u5728resources\u4e2d\u521b\u5efa\u4e00\u4e2a\u5305dao\uff0c\u7136\u540e\u5728\u5305dao\u4e2d\u521b\u5efa\u4e00\u4e2aUserDao.xml\uff0c\u5176\u4ee3\u7801\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n\n&lt;!--\u6307\u5b9aUserDao\u63a5\u53e3\u7684\u5168\u9650\u5b9a\u7c7b\u540d-->\n&lt;mapper namespace=\"dao.UserDao\">\n    &lt;!-- id\u5373\u4e3aUserDao\u63a5\u53e3\u7684getAll\uff08\uff09\u51fd\u6570\u7684\u65b9\u6cd5\u540d-->\n    &lt;select id=\"getAll\" resultType=\"entity.User\">\n    select * from User\n  &lt;\/select>\n    &lt;select id =\"getOne\" resultType=\"entity.User\">\n        select *from User where id=?;\n    &lt;\/select>\n&lt;\/mapper><\/code><\/pre>\n\n\n\n<p>9.\u5230\u6b64\u81ea\u5b9a\u4e49\u7684mybatis\u6846\u67b6\u57fa\u672c\u5b8c\u6210\uff0c\u4e0b\u9762\u8fdb\u884c\u6d4b\u8bd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package dao;\nimport entity.User;\nimport mybatis.Resources;\nimport mybatis.SqlSession;\nimport mybatis.SqlSessionFactory;\nimport mybatis.SqlSessionFactoryBuilder;\nimport org.junit.jupiter.api.Test;\nimport java.io.InputStream;\nimport java.util.List;\n\/**\n * Classname:UserDaoTest\n *\n * @description:\u6d4b\u8bd5Userdao\n * @author: \u964c\u610f\u968f\u5f71\n * @Date: 2020-07-24 10:49\n * @Version: 1.0\n **\/\npublic class UserDaoTest {\n    @Test\n    public void testGetUserAll(){\n        try {\n            \/\/\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\n            InputStream inputStream = Resources.getResourceAsStream(\"mybatis-config.xml\");\n            \/\/\u5b9e\u4f8b\u5316SqlSessionFactoryBuilder\n            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();\n          \/\/\u6784\u5efaSqlSessionFactory\n            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);\n           \/\/\u4ea7\u751f\u4e00\u4e2aSqlSession\n            SqlSession sqlSession = sqlSessionFactory.openSession();\n            \/\/\u5229\u7528SqlSession\u521b\u5efaUserDAao\u7684\u4ee3\u7406\u5bf9\u8c61\n            UserDao userDao = sqlSession.getMapper(UserDao.class);\n            \/\/\u5229\u7528\u4ee3\u7406\u5bf9\u8c61\u6267\u884c\u65b9\u6cd5\n            List&lt;User> userList = userDao.getAll();\n\n            for (User u:userList ) {\n                System.out.println(u);\n            }\n            User user = userDao.getOne(1);\n            \/\/\u5173\u95ed\u8d44\u6e90\n            sqlSession.close();\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img   class=\"lazyload\" data-src=\"https:\/\/img-blog.csdnimg.cn\/20200726021737161.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=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"\/><\/figure >\n<noscript><img src=\"https:\/\/img-blog.csdnimg.cn\/20200726021737161.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTE4NzAwMjI=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"\/><\/figure><\/noscript>\n\n\n\n<p>10.\u9879\u76ee\u7684\u76ee\u5f55\u4e3a\uff1a<br><img  src=\"https:\/\/img-blog.csdnimg.cn\/2020072602135229.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTE4NzAwMjI=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><br><img  class=\"lazyload\" data-src=\"https:\/\/img-blog.csdnimg.cn\/20200726021420792.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=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><br>11.\u672c\u9879\u76ee\u7684\u6570\u636e\u5e93MySQL\u6587\u4ef6User\u8868\u4e3a\uff1a<\/p >\n<noscript><img src=\"https:\/\/img-blog.csdnimg.cn\/2020072602135229.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTE4NzAwMjI=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><br><img src=\"https:\/\/img-blog.csdnimg.cn\/20200726021420792.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTE4NzAwMjI=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><br>11.\u672c\u9879\u76ee\u7684\u6570\u636e\u5e93MySQL\u6587\u4ef6User\u8868\u4e3a\uff1a<\/p><\/noscript>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE `user` (\n  `id` int(4) NOT NULL AUTO_INCREMENT,\n  `name` varchar(16) NOT NULL,\n  `password` varchar(16) NOT NULL,\n  `age` int(3) DEFAULT NULL,\n  `createtime` datetime DEFAULT CURRENT_TIMESTAMP,\n  PRIMARY KEY (`id`)\n) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;<\/code><\/pre>\n\n\n\n<p><strong>12\u672c\u9879\u76ee\u6e90\u7801\u5df2\u7ecf\u6253\u5305\uff0c\u6709\u9700\u8981\u8bf7\u76f4\u63a5\u70b9\u51fb\u4e0b\u8f7d\uff08\u4e0b\u8f7d\u89e3\u538b\u540e\u76f4\u63a5\u5bfc\u5165idea\u5373\u53ef\uff09\uff1a<\/strong><a href=\"http:\/\/moyisuiying.com\/wp-content\/uploads\/2020\/07\/mybatis-self.rar\">http:\/\/moyisuiying.com\/wp-content\/uploads\/2020\/07\/mybatis-self.rar<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u81ea\u5b9a\u4e49\u5b9e\u73b0mybatis\u7684\u7b80\u5355\u6846\u67b6<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[19,13],"_links":{"self":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts\/169"}],"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=169"}],"version-history":[{"count":1,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts\/169\/revisions"}],"predecessor-version":[{"id":427,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts\/169\/revisions\/427"}],"wp:attachment":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/media?parent=169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/categories?post=169"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/tags?post=169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}