{"id":10006704,"date":"2022-10-19T14:23:03","date_gmt":"2022-10-19T06:23:03","guid":{"rendered":"https:\/\/ityet.com\/?p=10006704"},"modified":"2022-10-19T14:23:03","modified_gmt":"2022-10-19T06:23:03","slug":"%e6%89%94%e6%8e%89%e5%b7%a5%e5%85%b7%e7%b1%bb%ef%bc%81mybatis-%e4%b8%80%e4%b8%aa%e7%ae%80%e5%8d%95%e9%85%8d%e7%bd%ae%e6%90%9e%e5%ae%9a%e5%8a%a0%e5%af%86%e3%80%81%e8%a7%a3%e5%af%86%ef%bc%8c%e4%b8%8d","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006704","title":{"rendered":"\u6254\u6389\u5de5\u5177\u7c7b\uff01MyBatis \u4e00\u4e2a\u7b80\u5355\u914d\u7f6e\u641e\u5b9a\u52a0\u5bc6\u3001\u89e3\u5bc6\uff0c\u4e0d\u80fd\u592a\u65b9\u4fbf\u4e86\uff5e\uff01"},"content":{"rendered":"<p>\u6765\u6e90\uff1ajuejin.cn\/post\/6963811586184052767<\/p>\n<p><strong>\u524d\u8a00\uff1a\u4ecb\u7ecd\u4e00\u4e2a\u7b80\u5355\u7684MyBatis\u52a0\u89e3\u5bc6\u65b9\u5f0f\uff0c\u65e5\u5e38\u5b66\u4e60\u5de5\u4f5c\u4e2d\u63d0\u53ca\u8fd9\u79cd\u65b9\u6cd5\u7684\u6bd4\u8f83\u5c11\uff0c\u6240\u4ee5\u62ff\u6765\u8bf4\u8bf4\uff0c\u5982\u679c\u5df2\u7ecf\u77e5\u9053\u8fd9\u79cd\u65b9\u6cd5\u7684\u5ffd\u7565\u672c\u6587\uff01<\/strong><\/p>\n<h2>\u4e00\u3001\u80cc\u666f<\/h2>\n<p>\u5728\u6211\u4eec\u6570\u636e\u5e93\u4e2d\u6709\u4e9b\u65f6\u5019\u4f1a\u4fdd\u5b58\u4e00\u4e9b\u7528\u6237\u7684\u654f\u611f\u4fe1\u606f\uff0c\u6bd4\u5982\uff1a\u624b\u673a\u53f7\u3001\u94f6\u884c\u5361\u7b49\u4fe1\u606f\uff0c\u5982\u679c\u8fd9\u4e9b\u4fe1\u606f\u4ee5\u660e\u6587\u7684\u65b9\u5f0f\u4fdd\u5b58\uff0c\u90a3\u4e48\u662f\u4e0d\u5b89\u5168\u7684\u3002\u5047\u5982\uff1a\u9ed1\u5ba2\u9ed1\u8fdb\u4e86\u6570\u636e\u5e93\uff0c\u6216\u8005\u79bb\u804c\u4eba\u5458\u5bfc\u51fa\u4e86\u6570\u636e\uff0c\u90a3\u4e48\u5c31\u53ef\u80fd\u5bfc\u81f4\u8fd9\u4e9b\u654f\u611f\u6570\u636e\u7684\u6cc4\u6f0f\u3002\u56e0\u6b64\u6211\u4eec\u5c31\u9700\u8981\u627e\u5230\u4e00\u79cd\u65b9\u6cd5\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<h2>\u4e8c\u3001\u89e3\u51b3\u65b9\u6848<\/h2>\n<p>\u7531\u4e8e\u6211\u4eec\u7cfb\u7edf\u4e2d\u4f7f\u7528\u4e86Mybatis\u4f5c\u4e3a\u6570\u636e\u5e93\u6301\u4e45\u5c42\uff0c\u56e0\u6b64\u51b3\u5b9a\u4f7f\u7528Mybatis\u7684TypeHandler\u6216Plugin\u6765\u89e3\u51b3\u3002<\/p>\n<p><strong>TypeHandler<\/strong> :  \u9700\u8981\u6211\u4eec\u5728\u67d0\u4e9b\u5217\u4e0a\u624b\u52a8\u6307\u5b9a typeHandler \u6765\u9009\u62e9\u4f7f\u7528\u90a3\u4e2atypeHandler\u6216\u8005\u6839\u636e<code>@MappedJdbcTypes<\/code> \u548c <code>@MappedTypes<\/code>\u6ce8\u89e3\u6765\u81ea\u884c\u63a8\u65ad\u3002<\/p>\n<pre><code>&lt;result column=&quot;phone&quot; property=&quot;phone&quot;\ntypeHandler=&quot;com.huan.study.mybatis.typehandler.EncryptTypeHandler&quot;\/&gt;<\/code><\/pre>\n<p><strong>Plugin<\/strong> : \u53ef\u4ee5\u62e6\u622a\u7cfb\u7edf\u4e2d\u7684 select\u3001insert\u3001update\u3001delete\u7b49\u8bed\u53e5\uff0c\u4e5f\u80fd\u83b7\u53d6\u5230sql\u6267\u884c\u524d\u7684\u53c2\u6570\u548c\u6267\u884c\u540e\u7684\u6570\u636e\u3002<\/p>\n<p>\u7ecf\u8fc7\u8003\u8651\uff0c\u51b3\u5b9a\u4f7f\u7528TypeHandler\u6765\u52a0\u89e3\u5bc6\u6570\u636e\u3002<\/p>\n<p>\u63a8\u8350\u4e00\u4e2a Spring Boot \u57fa\u7840\u6559\u7a0b\u53ca\u5b9e\u6218\u793a\u4f8b\uff1a<\/p>\n<blockquote><p><a href=\"https:\/\/link.segmentfault.com\/?enc=MWK98Vi7XW5FDLPe0Cxt7Q%3D%3D.8SGWyAGGaq3pjVRawknDxhQUO3pSaeL8JX6%2FaeXZEBmSV70oVbpxyQBQNKWHIBsS2LIuAGRR9l6CewKD87TNQA%3D%3D\" rel=\"nofollow\">https:\/\/github.com\/javastacks&#8230;<\/a><\/p><\/blockquote>\n<h2>\u4e09\u3001\u9700\u6c42<\/h2>\n<p>\u6211\u4eec\u6709\u4e00\u5f20\u5ba2\u6237\u8868customer\uff0c\u91cc\u9762\u6709\u5ba2\u6237\u624b\u673a\u53f7(phone)\u548c\u5ba2\u6237\u5730\u5740(address)\u7b49\u5b57\u6bb5\uff0c\u5176\u4e2d\u5ba2\u6237\u624b\u673a\u53f7(phone)\u662f\u9700\u8981\u52a0\u5bc6\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u4e2d\u7684\u3002<\/p>\n<p>1\u3001\u5728\u6dfb\u52a0\u5ba2\u6237\u4fe1\u606f\u65f6\uff0c\u81ea\u52a8\u5c06\u5ba2\u6237\u624b\u673a\u53f7\u52a0\u5bc6\u4fdd\u5b58\u5230\u6570\u636e\u4e2d\u3002<\/p>\n<p>2\u3001\u5728\u67e5\u8be2\u5ba2\u6237\u4fe1\u606f\u65f6\uff0c\u81ea\u52a8\u89e3\u5bc6\u5ba2\u6237\u624b\u673a\u53f7\u3002<\/p>\n<h2>\u56db\u3001\u5b9e\u73b0\u601d\u8def<\/h2>\n<p><strong>1\u3001\u7f16\u5199\u4e00\u4e2a\u5b9e\u4f53\u7c7b\uff0c\u51e1\u662f\u6b64\u5b9e\u4f53\u7c7b\u7684\u6570\u636e\u90fd\u8868\u793a\u9700\u8981\u52a0\u89e3\u5bc6\u7684<\/strong><\/p>\n<pre><code>public class Encrypt {\n    private String value;\n\n    public Encrypt() {\n    }\n\n    public Encrypt(String value) {\n        this.value = value;\n    }\n\n    public String getValue() {\n        return value;\n    }\n\n    public void setValue(String value) {\n        this.value = value;\n    }\n}<\/code><\/pre>\n<p><strong>2\u3001\u7f16\u5199\u4e00\u4e2a\u52a0\u89e3\u5bc6\u7684TypeHandler<\/strong><\/p>\n<ul>\n<li>\u8bbe\u7f6e\u53c2\u6570\u65f6\uff0c\u52a0\u5bc6\u6570\u636e\u3002<\/li>\n<li>\u4ece\u6570\u636e\u5e93\u83b7\u53d6\u8bb0\u5f55\u65f6\uff0c\u89e3\u5bc6\u6570\u636e\u3002<\/li>\n<\/ul>\n<pre><code>package com.huan.study.mybatis.typehandler;\n\nimport cn.hutool.crypto.SecureUtil;\nimport cn.hutool.crypto.symmetric.AES;\nimport org.apache.ibatis.type.BaseTypeHandler;\nimport org.apache.ibatis.type.JdbcType;\nimport org.apache.ibatis.type.MappedJdbcTypes;\nimport org.apache.ibatis.type.MappedTypes;\n\nimport java.nio.charset.StandardCharsets;\nimport java.sql.CallableStatement;\nimport java.sql.PreparedStatement;\nimport java.sql.ResultSet;\nimport java.sql.SQLException;\n\n\/**\n * \u52a0\u89e3\u5bc6TypeHandler\n *\n * @author huan.fu 2021\/5\/18 - \u4e0a\u53489:20\n *\/\n@MappedJdbcTypes(JdbcType.VARCHAR)\n@MappedTypes(Encrypt.class)\npublic class EncryptTypeHandler extends BaseTypeHandler&lt;Encrypt&gt; {\n\n    private static final byte[] KEYS = &quot;12345678abcdefgh&quot;.getBytes(StandardCharsets.UTF_8);\n\n    \/**\n     * \u8bbe\u7f6e\u53c2\u6570\n     *\/\n    @Override\n    public void setNonNullParameter(PreparedStatement ps, int i, Encrypt parameter, JdbcType jdbcType) throws SQLException {\n        if (parameter == null || parameter.getValue() == null) {\n            ps.setString(i, null);\n            return;\n        }\n        AES aes = SecureUtil.aes(KEYS);\n        String encrypt = aes.encryptHex(parameter.getValue());\n        ps.setString(i, encrypt);\n    }\n\n    \/**\n     * \u83b7\u53d6\u503c\n     *\/\n    @Override\n    public Encrypt getNullableResult(ResultSet rs, String columnName) throws SQLException {\n        return decrypt(rs.getString(columnName));\n    }\n\n    \/**\n     * \u83b7\u53d6\u503c\n     *\/\n    @Override\n    public Encrypt getNullableResult(ResultSet rs, int columnIndex) throws SQLException {\n        return decrypt(rs.getString(columnIndex));\n    }\n\n    \/**\n     * \u83b7\u53d6\u503c\n     *\/\n    @Override\n    public Encrypt getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {\n        return decrypt(cs.getString(columnIndex));\n    }\n\n    public Encrypt decrypt(String value) {\n        if (null == value) {\n            return null;\n        }\n        return new Encrypt(SecureUtil.aes(KEYS).decryptStr(value));\n    }\n}<\/code><\/pre>\n<p>\u6ce8\u610f\u26a0\ufe0f\uff1a<\/p>\n<ul>\n<li><code>@MappedTypes<\/code>\uff1a\u8868\u793a\u8be5\u5904\u7406\u5668\u5904\u7406\u7684java\u7c7b\u578b\u662f\u4ec0\u4e48\u3002<\/li>\n<li><code>@MappedJdbcTypes<\/code>\uff1a\u8868\u793a\u5904\u7406\u5668\u5904\u7406\u7684Jdbc\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p><strong>3\u3001sql\u8bed\u53e5\u4e2d\u5199\u6cd5<\/strong><\/p>\n<pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&lt;!DOCTYPE mapper PUBLIC &quot;-\/\/mybatis.org\/\/DTD Mapper 3.0\/\/EN&quot; &quot;http:\/\/mybatis.org\/dtd\/mybatis-3-mapper.dtd&quot;&gt;\n&lt;mapper namespace=&quot;com.huan.study.mybatis.mappers.CustomerMapper&quot;&gt;\n\n    &lt;resultMap id=&quot;BaseResultMapper&quot; type=&quot;com.huan.study.mybatis.entity.Customer&quot;&gt;\n        &lt;id column=&quot;id&quot; property=&quot;id&quot;\/&gt;\n        &lt;result column=&quot;phone&quot; property=&quot;phone&quot;\/&gt;\n        &lt;result column=&quot;address&quot; property=&quot;address&quot;\/&gt;\n    &lt;\/resultMap&gt;\n\n    &lt;insert id=&quot;addCustomer&quot;&gt;\n        insert into customer(phone,address) values (#{phone},#{address})\n    &lt;\/insert&gt;\n\n    &lt;select id=&quot;findCustomer&quot; resultMap=&quot;BaseResultMapper&quot;&gt;\n        select * from customer where phone = #{phone}\n    &lt;\/select&gt;\n\n&lt;\/mapper&gt;<\/code><\/pre>\n<p>SQL\u4e2d\u6ca1\u6709\u4ec0\u4e48\u7279\u6b8a\u7684\u5199\u6cd5\u3002<\/p>\n<p><strong>4\u3001\u914d\u7f6e\u6587\u4ef6\u4e2d\u6307\u5b9aTypehandler\u7684\u5305\u8def\u5f84<\/strong><\/p>\n<pre><code>mybatis.type-handlers-package=com.huan.study.mybatis.typehandler<\/code><\/pre>\n<p><strong>5\u3001\u7f16\u5199\u540e\u53f0\u4ee3\u7801<\/strong><\/p>\n<ul>\n<li>\u63d0\u4f9b\u4e00\u4e2a\u6dfb\u52a0\u65b9\u6cd5<\/li>\n<li>\u63d0\u4f9b\u4e00\u4e2a\u6839\u636e\u624b\u673a\u53f7\u67e5\u8be2\u7684\u65b9\u6cd5<\/li>\n<\/ul>\n<p>\u540e\u53f0\u4ee3\u7801\u6bd4\u8f83\u7b80\u5355\uff0c\u76f4\u63a5\u67e5\u770b\uff1a<\/p>\n<blockquote>\n<p>\u201c<\/p>\n<p><a href=\"https:\/\/link.segmentfault.com\/?enc=Np%2BhDbVg8lbc3RVZy4FZKA%3D%3D.CyDFNl75B4l33tnIySvAoUvnZhyroAEyHlEN9wHjemc%3D\" rel=\"nofollow\">http:\/\/dw-z.ink\/1s520<\/a><\/p>\n<\/blockquote>\n<p>\u8d34\u4e00\u4e2amapper\u5c42\u7684\u622a\u56fe\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc25Yn\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><strong>6\u3001\u6d4b\u8bd5\u7ed3\u679c<\/strong><\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc25Yo\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4ece\u6d4b\u8bd5\u7ed3\u679c\u4e2d\u53ef\u77e5\uff0c\u6dfb\u52a0\u6570\u636e\u65f6\uff0c\u9700\u8981\u52a0\u5bc6\u7684\u6570\u636e(phone)\u5728\u6570\u636e\u5e93\u4e2d\u5df2\u7ecf\u52a0\u5bc6\u4e86\uff0c\u5728\u67e5\u8be2\u7684\u65f6\u5019\uff0c\u52a0\u5bc6\u7684\u6570\u636e\u5df2\u7ecf\u81ea\u52a8\u89e3\u5bc6\u4e86\u3002<\/p>\n<p><strong>\u8fd1\u671f\u70ed\u6587\u63a8\u8350\uff1a<\/strong><\/p>\n<p>1.<a href=\"https:\/\/link.segmentfault.com\/?enc=iJyLNWqFYCgyaHKsx%2F1%2Bug%3D%3D.0HO1sPvZvxReyFCi%2BH4FZ%2Bg79Hm2A4d96UPj94WyRHE%3D\" rel=\"nofollow\">1,000+ \u9053 Java\u9762\u8bd5\u9898\u53ca\u7b54\u6848\u6574\u7406(2022\u6700\u65b0\u7248)<\/a><\/p>\n<p>2.<a href=\"https:\/\/link.segmentfault.com\/?enc=kZu0uSTn2opnViPpb%2Bsbuw%3D%3D.vdFqc53Z%2BbZSKixdOBF4UTRNPwZvQlSLoKehY7lxWR3sBcsn725X9W23kN7Q%2Fgj%2BMpJcu5PU03HMp8egBZqmHA%3D%3D\" rel=\"nofollow\">\u52b2\u7206\uff01Java \u534f\u7a0b\u8981\u6765\u4e86\u3002\u3002\u3002<\/a><\/p>\n<p>3.<a href=\"https:\/\/link.segmentfault.com\/?enc=9VKSDraTg7kaPQiitfSQ7A%3D%3D.YHpofH1MEf%2BpGRnaYwGXYGUN94txgeXQAbXMX4d9f5wf2EKcel55lfiBFwHYXfls\" rel=\"nofollow\">Spring Boot 2.x \u6559\u7a0b\uff0c\u592a\u5168\u4e86\uff01<\/a><\/p>\n<p>4.<a href=\"https:\/\/link.segmentfault.com\/?enc=8B4L7AZWf0p4RRLzdWe6Uw%3D%3D.dCiXt4jvdvUketBJcsRtqk8xTN1XAdI6V6NuDQ5WYnVnCUAfS4%2B1z7KXRxLYjW6QMjHmjE1wKYjwJXd5g6l2oA%3D%3D\" rel=\"nofollow\">\u522b\u518d\u5199\u6ee1\u5c4f\u7684\u7206\u7206\u7206\u70b8\u7c7b\u4e86\uff0c\u8bd5\u8bd5\u88c5\u9970\u5668\u6a21\u5f0f\uff0c\u8fd9\u624d\u662f\u4f18\u96c5\u7684\u65b9\u5f0f\uff01\uff01<\/a><\/p>\n<p>5.<a href=\"https:\/\/link.segmentfault.com\/?enc=PRHNoY10%2Bo8DZ7tOCNRhhg%3D%3D.SjjI1HDqFvaTwWKUyHhCVHrqyuMz24zZRKLWl%2FJB1CHoAkWf%2BW9rQdFvm7%2FEBLbGRuBfOvSQrbaQoaL92rcDgebauQnZ6tFPDuuho%2BC5vw0%3D\" rel=\"nofollow\">\u300aJava\u5f00\u53d1\u624b\u518c\uff08\u5d69\u5c71\u7248\uff09\u300b\u6700\u65b0\u53d1\u5e03\uff0c\u901f\u901f\u4e0b\u8f7d\uff01<\/a><\/p>\n<p>\u89c9\u5f97\u4e0d\u9519\uff0c\u522b\u5fd8\u4e86\u968f\u624b\u70b9\u8d5e+\u8f6c\u53d1\u54e6\uff01<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042644607\">https:\/\/segmentfault.com\/a\/1190000042644607<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6765\u6e90\uff1ajuejin.cn\/post\/6963811586184052767 \u524d\u8a00\uff1a\u4ecb\u7ecd\u4e00\u4e2a\u7b80\u5355\u7684MyBatis [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006704","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006704","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006704"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006704\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}