{"id":128,"date":"2024-08-07T18:33:10","date_gmt":"2024-08-07T10:33:10","guid":{"rendered":"https:\/\/note.ui11.cc\/?p=128"},"modified":"2024-08-07T18:36:39","modified_gmt":"2024-08-07T10:36:39","slug":"%e5%af%b9%e8%b1%a1copy","status":"publish","type":"post","link":"https:\/\/note.ui11.cc\/index.php\/2024\/08\/07\/%e5%af%b9%e8%b1%a1copy\/","title":{"rendered":"\u5bf9\u8c61Copy"},"content":{"rendered":"\n<p><code>BeanUtils.copyProperties<\/code>&nbsp;\u662f Apache Commons BeanUtils \u5e93\u4e2d\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u7528\u4e8e\u5728\u4e24\u4e2a Java \u5bf9\u8c61\u4e4b\u95f4\u590d\u5236\u5c5e\u6027\u3002\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u503c\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u5bf9\u8c61\u4e2d\uff0c\u57fa\u4e8e\u5c5e\u6027\u540d\u8fdb\u884c\u5339\u914d\u3002\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u6709\u7528\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5c06\u6570\u636e\u4ece\u4e00\u4e2a\u5bf9\u8c61\u8f6c\u79fb\u5230\u53e6\u4e00\u4e2a\u5bf9\u8c61\u65f6\uff0c\u4f8b\u5982\u5728\u6570\u636e\u4f20\u8f93\u5bf9\u8c61\uff08DTO\uff09\u548c\u5b9e\u4f53\u5bf9\u8c61\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528&nbsp;<code>BeanUtils.copyProperties<\/code>&nbsp;\u65b9\u6cd5\u7684\u793a\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.apache.commons.beanutils.BeanUtils;\n\npublic class CopyPropertiesExample {\n    public static void main(String&#91;] args) {\n         EmployeeDTO employeeDTO = new EmployeeDTO();\n         employeeDTO.setName(\"John Doe\");\n         employeeDTO.setAge(30);\n\n         Employee employee = new Employee();\n         \/\/ \u5047\u8bbe Employee \u7c7b\u6709\u76f8\u5e94\u7684\u5c5e\u6027\u548c setter \u65b9\u6cd5\n\n         try {\n             BeanUtils.copyProperties(employee, employeeDTO);\n             \/\/ \u73b0\u5728 employee \u5bf9\u8c61\u7684\u5c5e\u6027\u503c\u5e94\u8be5\u4e0e employeeDTO \u4e2d\u7684\u503c\u76f8\u540c\n         } catch (Exception e) {\n             e.printStackTrace();\n         }\n     }\n}\n\nclass Employee {\n    private String name;\n    private int age;\n    \/\/ setter \u548c getter \u65b9\u6cd5\n}\n\nclass EmployeeDTO {\n    private String name;\n    private int age;\n    \/\/ setter \u548c getter \u65b9\u6cd5\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Employee<\/code>&nbsp;\u7c7b\u548c&nbsp;<code>EmployeeDTO<\/code>&nbsp;\u7c7b\u90fd\u6709&nbsp;<code>name<\/code>&nbsp;\u548c&nbsp;<code>age<\/code>&nbsp;\u5c5e\u6027\uff0c\u5e76\u4e14\u6709\u76f8\u5e94\u7684 setter \u65b9\u6cd5\u3002\u4f7f\u7528&nbsp;<code>BeanUtils.copyProperties<\/code>&nbsp;\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06&nbsp;<code>EmployeeDTO<\/code>&nbsp;\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\u590d\u5236\u5230&nbsp;<code>Employee<\/code>&nbsp;\u5bf9\u8c61\u4e2d\u3002<\/p>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>BeanUtils.copyProperties<\/code>&nbsp;\u65b9\u6cd5\u5728\u590d\u5236\u5c5e\u6027\u65f6\uff0c\u4f1a\u5ffd\u7565&nbsp;<code>null<\/code>&nbsp;\u503c\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c&nbsp;<code>employeeDTO<\/code>&nbsp;\u4e2d\u7684\u67d0\u4e2a\u5c5e\u6027\u503c\u4e3a&nbsp;<code>null<\/code>\uff0c\u90a3\u4e48\u5728\u590d\u5236\u8fc7\u7a0b\u4e2d\uff0c<code>employee<\/code>&nbsp;\u5bf9\u8c61\u4e2d\u76f8\u5e94\u7684\u5c5e\u6027\u4e0d\u4f1a\u88ab\u8bbe\u7f6e\u4e3a&nbsp;<code>null<\/code>\u3002<\/p>\n\n\n\n<p>\u6b64\u5916\uff0c<code>BeanUtils.copyProperties<\/code>&nbsp;\u65b9\u6cd5\u5728\u590d\u5236\u5c5e\u6027\u65f6\uff0c\u4f1a\u6839\u636e\u5c5e\u6027\u540d\u8fdb\u884c\u5339\u914d\u3002\u5982\u679c\u4e24\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u540d\u76f8\u540c\uff0c\u90a3\u4e48\u8fd9\u4e9b\u5c5e\u6027\u7684\u503c\u5c06\u88ab\u590d\u5236\u3002\u5982\u679c\u5c5e\u6027\u540d\u4e0d\u540c\uff0c\u90a3\u4e48\u8fd9\u4e9b\u5c5e\u6027\u4e0d\u4f1a\u88ab\u590d\u5236\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528&nbsp;<code>BeanUtils.copyProperties<\/code>&nbsp;\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u4e24\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u7c7b\u578b\u517c\u5bb9\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u629b\u51fa\u5f02\u5e38\u3002\u6b64\u5916\uff0c\u5982\u679c\u5bf9\u8c61\u7684\u5c5e\u6027\u8f83\u591a\uff0c\u6216\u8005\u5c5e\u6027\u540d\u4e0d\u4e00\u81f4\uff0c\u624b\u52a8\u590d\u5236\u5c5e\u6027\u53ef\u80fd\u66f4\u52a0\u7075\u6d3b\u548c\u53ef\u63a7\u3002<\/p>\n\n\n\n<p><code>BeanUtils.copyProperties(employeeDTO, employee)<\/code>&nbsp;\u8fd9\u884c\u4ee3\u7801\u5b9e\u9645\u4e0a\u662f\u5c06&nbsp;<code>employeeDTO<\/code>&nbsp;\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\u590d\u5236\u7ed9&nbsp;<code>employee<\/code>&nbsp;\u5bf9\u8c61\u3002\u5728\u4f7f\u7528 Apache Commons BeanUtils \u5e93\u7684&nbsp;<code>copyProperties<\/code>&nbsp;\u65b9\u6cd5\u65f6\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u8981\u4ece\u4e2d\u590d\u5236\u5c5e\u6027\u503c\u7684\u5bf9\u8c61\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u76ee\u6807\u5bf9\u8c61\uff0c\u5373\u5c5e\u6027\u503c\u5c06\u88ab\u590d\u5236\u5230\u7684\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\uff0c\u6267\u884c&nbsp;<code>BeanUtils.copyProperties(employeeDTO, employee)<\/code>&nbsp;\u540e\uff0c<code>employee<\/code>&nbsp;\u5bf9\u8c61\u7684\u5c5e\u6027\u5c06\u88ab&nbsp;<code>employeeDTO<\/code>&nbsp;\u5bf9\u8c61\u7684\u76f8\u5e94\u5c5e\u6027\u503c\u66f4\u65b0\u3002\u8fd9\u901a\u5e38\u7528\u4e8e\u5c06\u6570\u636e\u4ece\u4e00\u4e2a\u6570\u636e\u4f20\u8f93\u5bf9\u8c61\uff08DTO\uff09\u590d\u5236\u5230\u4e00\u4e2a\u5b9e\u4f53\u5bf9\u8c61\uff0c\u6216\u8005\u5728\u4e24\u4e2a\u5b9e\u4f53\u5bf9\u8c61\u4e4b\u95f4\u540c\u6b65\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u5982\u679c&nbsp;<code>employeeDTO<\/code>&nbsp;\u5305\u542b\u4e86\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\uff0c\u800c&nbsp;<code>employee<\/code>&nbsp;\u662f\u6570\u636e\u5e93\u4e2d\u7684\u5b9e\u4f53\u5bf9\u8c61\uff0c\u90a3\u4e48\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u5c06\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u66f4\u65b0\u5230\u6570\u636e\u5e93\u5b9e\u4f53\u4e2d\u3002<\/p>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>copyProperties<\/code>&nbsp;\u65b9\u6cd5\u5728\u590d\u5236\u5c5e\u6027\u65f6\uff0c\u4f1a\u5ffd\u7565&nbsp;<code>null<\/code>&nbsp;\u503c\uff0c\u5373\u5982\u679c&nbsp;<code>employeeDTO<\/code>&nbsp;\u4e2d\u7684\u67d0\u4e2a\u5c5e\u6027\u503c\u4e3a&nbsp;<code>null<\/code>\uff0c\u90a3\u4e48\u5728\u590d\u5236\u8fc7\u7a0b\u4e2d\uff0c<code>employee<\/code>&nbsp;\u5bf9\u8c61\u4e2d\u76f8\u5e94\u7684\u5c5e\u6027\u4e0d\u4f1a\u88ab\u8bbe\u7f6e\u4e3a&nbsp;<code>null<\/code>\u3002\u6b64\u5916\uff0c\u5982\u679c\u4e24\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u540d\u4e0d\u5b8c\u5168\u76f8\u540c\uff0c\u90a3\u4e48\u8fd9\u4e9b\u5c5e\u6027\u4e0d\u4f1a\u88ab\u590d\u5236\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>BeanUtils.copyProperties&nbsp;\u662f Apache Commons BeanUtil [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,4],"tags":[],"class_list":["post-128","post","type-post","status-publish","format-standard","hentry","category-mybatis","category-4"],"_links":{"self":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts\/128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/comments?post=128"}],"version-history":[{"count":2,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts\/128\/revisions"}],"predecessor-version":[{"id":130,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts\/128\/revisions\/130"}],"wp:attachment":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/media?parent=128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/categories?post=128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/tags?post=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}