{"id":218233,"date":"2021-05-21T10:57:50","date_gmt":"2021-05-21T02:57:50","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=218233"},"modified":"2021-05-05T15:58:38","modified_gmt":"2021-05-05T07:58:38","slug":"mybatis-param-annotation","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/mybatis-param-annotation.html","title":{"rendered":"MyBatis \u4e2d @Param \u6ce8\u89e3\u7684\u56db\u79cd\u4f7f\u7528\u573a\u666f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u6709\u4e00\u4e9b\u5c0f\u4f19\u4f34\u89c9\u5f97 MyBatis \u53ea\u6709\u65b9\u6cd5\u4e2d\u5b58\u5728\u591a\u4e2a\u53c2\u6570\u7684\u65f6\u5019\uff0c\u624d\u9700\u8981\u6dfb\u52a0 @Param \u6ce8\u89e3\uff0c\u5176\u5b9e\u8fd9\u4e2a\u7406\u89e3\u662f\u4e0d\u51c6\u786e\u7684\u3002\u5373\u4f7f MyBatis \u65b9\u6cd5\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\uff0c\u4e5f\u53ef\u80fd\u4f1a\u7528\u5230 @Param \u6ce8\u89e3\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u4f46\u662f\uff0c\u5728\u4f60\u603b\u7ed3\u51fa\u89c4\u5f8b\u4e4b\u524d\uff0c\u4f60\u53ef\u80fd\u4f1a\u89c9\u5f97\u83ab\u540d\u5176\u5999\uff0c\u6709\u7684\u65f6\u5019\u4e00\u4e2a\u53c2\u6570\u660e\u660e\u4e0d\u7528\u6dfb\u52a0 @Param \u6ce8\u89e3\uff0c\u6709\u7684\u65f6\u5019\uff0c\u5374\u9700\u8981\u6dfb\u52a0\uff0c\u4e0d\u6dfb\u52a0\u4f1a\u62a5\u9519\u3002<\/p>\n

\u6709\u7684\u4eba\u4f1a\u89c9\u5f97\u8fd9\u662f MyBatis \u5404\u4e2a\u7248\u672c\u5dee\u5f02\u7684\u9505\uff0c\u4e0d\u53ef\u5426\u8ba4\uff0cMyBatis \u53d1\u5c55\u5f88\u5feb\uff0c\u4e0d\u540c\u7248\u672c\u4e4b\u95f4\u7684\u5dee\u5f02\u8fd8\u633a\u660e\u663e\u7684\uff0c\u4e0d\u8fc7\u8fd9\u4e2a\u52a0\u4e0d\u52a0 @Param \u6ce8\u89e3\u7684\u95ee\u9898\uff0c\u5374\u5e76\u4e0d\u662f\u7248\u672c\u7684\u9505\uff01\u4eca\u5929\u677e\u54e5\u5c31\u548c\u5927\u5bb6\u6765\u804a\u4e00\u804a\u8fd9\u4e2a\u95ee\u9898\uff0c\u5230\u5e95\u54ea\u4e9b\u60c5\u51b5\u4e0b\u9700\u8981\u6dfb\u52a0 @Param \u6ce8\u89e3\u3002<\/p>\n

\u9996\u5148\uff0c\u5982\u4e0b\u51e0\u4e2a\u9700\u8981\u6dfb\u52a0 @Param \u6ce8\u89e3\u7684\u573a\u666f\uff0c\u76f8\u4fe1\u5927\u5bb6\u90fd\u5df2\u7ecf\u6709\u5171\u8bc6\u4e86\uff1a<\/p>\n

\u7b2c\u4e00\u79cd\uff1a\u65b9\u6cd5\u6709\u591a\u4e2a\u53c2\u6570\uff0c\u9700\u8981 @Param \u6ce8\u89e3
\n\u4f8b\u5982\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n

\r\n@Mapper\r\npublic interface UserMapper {\r\n    Integer insert(@Param(\"username\") String username, @Param(\"address\") String address);\r\n}\r\n<\/pre>\n

\u5bf9\u5e94\u7684 XML \u6587\u4ef6\u5982\u4e0b\uff1a<\/p>\n

\r\n<insert id="insert" parameterType="org.javaboy.helloboot.bean.User">\r\n    insert into user (username,address) values (#{username},#{address});\r\n<\/insert>\r\n<\/pre>\n

\u8fd9\u662f\u6700\u5e38\u89c1\u7684\u9700\u8981\u6dfb\u52a0 @Param<\/code>\u6ce8\u89e3\u7684\u573a\u666f\u3002<\/p>\n

\u7b2c\u4e8c\u79cd\uff1a\u65b9\u6cd5\u53c2\u6570\u8981\u53d6\u522b\u540d\uff0c\u9700\u8981 @Param<\/code> \u6ce8\u89e3
\n\u5f53\u9700\u8981\u7ed9\u53c2\u6570\u53d6\u4e00\u4e2a\u522b\u540d\u7684\u65f6\u5019\uff0c\u6211\u4eec\u4e5f\u9700\u8981 @Param <\/code>\u6ce8\u89e3\uff0c\u4f8b\u5982\u65b9\u6cd5\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n

\r\n@Mapper\r\npublic interface UserMapper {\r\n    User getUserByUsername(@Param(\"name\") String username);\r\n}\r\n<\/pre>\n

\u5bf9\u5e94\u7684 XML \u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n

\r\n<select id="getUserByUsername" parameterType="org.javaboy.helloboot.bean.User">\r\n    select * from user where username=#{name};\r\n<\/select>\r\n<\/pre>\n

\u8001\u5b9e\u8bf4\uff0c\u8fd9\u79cd\u9700\u6c42\u4e0d\u591a\uff0c\u8d39\u4e8b\u3002<\/p>\n

\u7b2c\u4e09\u79cd\uff1aXML \u4e2d\u7684 SQL \u4f7f\u7528\u4e86 $ \uff0c\u90a3\u4e48\u53c2\u6570\u4e2d\u4e5f\u9700\u8981 @Param \u6ce8\u89e3
\n$ \u4f1a\u6709\u6ce8\u5165\u6f0f\u6d1e\u7684\u95ee\u9898\uff0c\u4f46\u662f\u6709\u7684\u65f6\u5019\u4f60\u4e0d\u5f97\u4e0d\u4f7f\u7528 $ \u7b26\u53f7\uff0c\u4f8b\u5982\u8981\u4f20\u5165\u5217\u540d\u6216\u8005\u8868\u540d\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u65f6\u5019\u5fc5\u987b\u8981\u6dfb\u52a0 @Param \u6ce8\u89e3\uff0c\u4f8b\u5982\uff1a<\/p>\n

\r\n@Mapper\r\npublic interface UserMapper {\r\n    List getAllUsers(@Param(\"order_by\")String order_by);\r\n}\r\n<\/user><\/pre>\n

\u5bf9\u5e94\u7684 XML \u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n

\r\n<select id="getAllUsers" resultType="org.javaboy.helloboot.bean.User">\r\n    select * from user\r\n    <if test="order_by!=null and order_by!=''">\r\n        order by ${order_by} desc\r\n    <\/if>\r\n<\/select>\r\n<\/pre>\n

\u524d\u9762\u8fd9\u4e09\u79cd\uff0c\u90fd\u5f88\u5bb9\u6613\u61c2\uff0c\u76f8\u4fe1\u5f88\u591a\u5c0f\u4f19\u4f34\u4e5f\u90fd\u61c2\uff0c\u9664\u4e86\u8fd9\u4e09\u79cd\u5e38\u89c1\u7684\u573a\u666f\u4e4b\u5916\uff0c\u8fd8\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u573a\u666f\uff0c\u7ecf\u5e38\u88ab\u4eba\u5ffd\u7565\u3002<\/p>\n

\u7b2c\u56db\u79cd\uff0c\u90a3\u5c31\u662f\u52a8\u6001 SQL \uff0c\u5982\u679c\u5728\u52a8\u6001 SQL \u4e2d\u4f7f\u7528\u4e86\u53c2\u6570\u4f5c\u4e3a\u53d8\u91cf\uff0c\u90a3\u4e48\u4e5f\u9700\u8981 @Param \u6ce8\u89e3\uff0c\u5373\u4f7f\u4f60\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\u3002
\n\u5982\u679c\u6211\u4eec\u5728\u52a8\u6001 SQL \u4e2d\u7528\u5230\u4e86 \u53c2\u6570\u4f5c\u4e3a\u5224\u65ad\u6761\u4ef6\uff0c\u90a3\u4e48\u4e5f\u662f\u4e00\u5b9a\u8981\u52a0 @Param \u6ce8\u89e3\u7684\uff0c\u4f8b\u5982\u5982\u4e0b\u65b9\u6cd5\uff1a<\/p>\n

\r\n@Mapper\r\npublic interface UserMapper {\r\n    List getUserById(@Param(\"id\")Integer id);\r\n}\r\n<\/user><\/pre>\n

\u5b9a\u4e49\u51fa\u6765\u7684 SQL \u5982\u4e0b\uff1a<\/p>\n

\r\n<select id="getUserById" resultType="org.javaboy.helloboot.bean.User">\r\n    select * from user\r\n    <if test="id!=null">\r\n        where id=#{id}\r\n    <\/if>\r\n<\/select>\r\n<\/pre>\n

\u8fd9\u79cd\u60c5\u51b5\uff0c\u5373\u4f7f\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\uff0c\u4e5f\u9700\u8981\u6dfb\u52a0 @Param \u6ce8\u89e3\uff0c\u800c\u8fd9\u79cd\u60c5\u51b5\u5374\u7ecf\u5e38\u88ab\u4eba\u5ffd\u7565\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"

\u4f46\u662f\uff0c\u5728\u4f60\u603b\u7ed3\u51fa\u89c4\u5f8b\u4e4b\u524d\uff0c\u4f60\u53ef\u80fd\u4f1a\u89c9\u5f97\u83ab\u540d\u5176\u5999\uff0c\u6709\u7684\u65f6\u5019\u4e00\u4e2a\u53c2\u6570\u660e\u660e\u4e0d\u7528\u6dfb\u52a0 @Param \u6ce8\u89e3\uff0c\u6709\u7684\u65f6\u5019\uff0c\u5374 […]<\/p>\n","protected":false},"author":1903,"featured_media":218248,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-218233","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/218233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=218233"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/218233\/revisions"}],"predecessor-version":[{"id":218249,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/218233\/revisions\/218249"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/218248"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=218233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=218233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=218233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}