{"id":141,"date":"2024-08-08T09:55:28","date_gmt":"2024-08-08T01:55:28","guid":{"rendered":"https:\/\/note.ui11.cc\/?p=141"},"modified":"2024-08-08T09:55:29","modified_gmt":"2024-08-08T01:55:29","slug":"%e5%88%86%e9%a1%b5%e6%9f%a5%e8%af%a2pagehelper%e6%8f%92%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/note.ui11.cc\/index.php\/2024\/08\/08\/%e5%88%86%e9%a1%b5%e6%9f%a5%e8%af%a2pagehelper%e6%8f%92%e4%bb%b6\/","title":{"rendered":"\u5206\u9875\u67e5\u8be2PageHelper\u63d2\u4ef6"},"content":{"rendered":"\n<pre class=\"wp-block-preformatted\">\u9996\u5148\u5f15\u5165\u4f9d\u8d56<br><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code> &lt;dependency&gt;\n                &lt;groupId&gt;com.github.pagehelper&lt;\/groupId&gt;\n                &lt;artifactId&gt;pagehelper-spring-boot-starter&lt;\/artifactId&gt;\n                &lt;version&gt;${pagehelper}&lt;\/version&gt;\n            &lt;\/dependency&gt;<\/code><\/pre>\n\n\n\n<p>\u4e3b\u8981\u5c31\u662f\u8981\u544a\u8bc9\u63d2\u4ef6\uff0c\u4ece\u54ea\u9875\u5f00\u59cb\u67e5\uff0c\u4e00\u6b21\u67e5\u591a\u5c11<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">PageHelper.<em>startPage<\/em>(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());<\/pre>\n\n\n\n<p>employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize()\u662f\u4e24\u4e2a\u53c2\u6570\uff0c\u4e00\u4e2a\u662f\u9875\u7801\u4e00\u4e2a\u662f\u8bb0\u5f55\u6570\uff0c<\/p>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>SQL\u8bed\u53e5\u4e4btest<\/summary>\n<p>\u5728\u60a8\u63d0\u4f9b\u7684MyBatis\u6620\u5c04\u6587\u4ef6\u7247\u6bb5\u4e2d\uff0c<code>name<\/code>&nbsp;\u662f\u4e00\u4e2a\u53ef\u9009\u7684\u67e5\u8be2\u53c2\u6570\u3002\u901a\u8fc7&nbsp;<code>&lt;if&gt;<\/code>&nbsp;\u6807\u7b7e\u5185\u7684&nbsp;<code>test<\/code>&nbsp;\u5c5e\u6027\uff0cMyBatis\u4f1a\u6839\u636e&nbsp;<code>name<\/code>&nbsp;\u53c2\u6570\u7684\u503c\u6765\u51b3\u5b9a\u662f\u5426\u5c06\u76f8\u5e94\u7684SQL\u7247\u6bb5\u5305\u542b\u5728\u6700\u7ec8\u751f\u6210\u7684\u67e5\u8be2\u8bed\u53e5\u4e2d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>name<\/code>&nbsp;\u53c2\u6570\u7684\u6761\u4ef6\u903b\u8f91<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u00a0<code>name<\/code>\u00a0\u53c2\u6570\u4e0d\u4e3a\u00a0<code>null<\/code>\u00a0\u4e14\u4e0d\u4e3a\u7a7a\u5b57\u7b26\u4e32\uff08<code>''<\/code>\uff09\uff0c\u5219\u00a0<code>test<\/code>\u00a0\u6761\u4ef6\u4e3a\u00a0<code>true<\/code>\uff0c<code>and name like concat('%',#{name},'%')<\/code>\u00a0\u8fd9\u4e2aSQL\u7247\u6bb5\u4f1a\u88ab\u5305\u542b\u5728\u67e5\u8be2\u8bed\u53e5\u4e2d\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u00a0<code>name<\/code>\u00a0\u53c2\u6570\u4e3a\u00a0<code>null<\/code>\u00a0\u6216\u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c<code>test<\/code>\u00a0\u6761\u4ef6\u4e3a\u00a0<code>false<\/code>\uff0c<code>and name like concat('%',#{name},'%')<\/code>\u00a0\u8fd9\u4e2aSQL\u7247\u6bb5\u4e0d\u4f1a\u88ab\u5305\u542b\u5728\u67e5\u8be2\u8bed\u53e5\u4e2d\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5206\u9875\u67e5\u8be2\u7684\u5b9e\u73b0<\/h3>\n\n\n\n<p>\u5728MyBatis\u4e2d\uff0c\u5206\u9875\u67e5\u8be2\u901a\u5e38\u9700\u8981\u989d\u5916\u7684\u53c2\u6570\uff0c\u5982\u5f53\u524d\u9875\u7801\u548c\u6bcf\u9875\u663e\u793a\u7684\u8bb0\u5f55\u6570\u3002\u8fd9\u4e9b\u53c2\u6570\u7528\u4e8e\u8ba1\u7b97\u67e5\u8be2\u7684\u8d77\u59cb\u4f4d\u7f6e\u548c\u7ed3\u675f\u4f4d\u7f6e\uff0c\u4ece\u800c\u5b9e\u73b0\u5206\u9875\u6548\u679c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b<\/h3>\n\n\n\n<p>\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u5206\u9875\u67e5\u8be2\u7684\u63a5\u53e3\uff0c\u5b83\u63a5\u53d7\u5f53\u524d\u9875\u7801&nbsp;<code>currentPage<\/code>&nbsp;\u548c\u6bcf\u9875\u8bb0\u5f55\u6570&nbsp;<code>pageSize<\/code>&nbsp;\u4f5c\u4e3a\u53c2\u6570\uff0c\u4ee5\u53ca\u53ef\u9009\u7684&nbsp;<code>name<\/code>&nbsp;\u53c2\u6570\u7528\u4e8e\u6a21\u7cca\u641c\u7d22\u3002\u60a8\u7684MyBatis\u6620\u5c04\u6587\u4ef6\u53ef\u80fd\u9700\u8981\u50cf\u8fd9\u6837\u4fee\u6539\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;select id=\"pageQuery\" resultType=\"com.sky.entity.Employee\">\n    select * from employee\n    &lt;where>\n        &lt;if test=\"name != null and name != ''\">\n            and name like concat('%', #{name}, '%')\n        &lt;\/if>\n    &lt;\/where>\n    order by create_time desc\n    limit #{offset}, #{limit}\n&lt;\/select>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c#{offset} \u548c #{limit} \u662fMyBatis\u7684\u53c2\u6570\u5360\u4f4d\u7b26\uff0c\u5b83\u4eec\u5206\u522b\u4ee3\u8868\u67e5\u8be2\u7684\u8d77\u59cb\u4f4d\u7f6e\u548c\u8bb0\u5f55\u6570\u3002\u8fd9\u4e9b\u53c2\u6570\u9700\u8981\u5728\u8c03\u7528\u67e5\u8be2\u65b9\u6cd5\u65f6\u63d0\u4f9b\u3002<\/code><\/pre>\n<\/details>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>SQL\u8bed\u53e5\u4e4band<\/summary>\n<p>\u5728MyBatis\u7684\u52a8\u6001SQL\u4e2d\uff0c<code>&lt;where&gt;<\/code>&nbsp;\u6807\u7b7e\u7528\u4e8e\u6784\u5efa&nbsp;<code>WHERE<\/code>&nbsp;\u5b50\u53e5\uff0c\u5e76\u4e14\u4f1a\u667a\u80fd\u5730\u5904\u7406&nbsp;<code>AND<\/code>&nbsp;\u548c&nbsp;<code>OR<\/code>&nbsp;\u5173\u952e\u5b57\u7684\u4f7f\u7528\u3002<code>&lt;where&gt;<\/code>&nbsp;\u6807\u7b7e\u4f1a\u6839\u636e\u5185\u90e8\u6761\u4ef6\u7684\u5b9e\u9645\u60c5\u51b5\u6765\u51b3\u5b9a\u662f\u5426\u5305\u542b&nbsp;<code>AND<\/code>&nbsp;\u5173\u952e\u5b57\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>&lt;where&gt;<\/code>&nbsp;\u6807\u7b7e\u7684\u884c\u4e3a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u00a0<code>&lt;where><\/code>\u00a0\u6807\u7b7e\u5185\u90e8\u6ca1\u6709\u4efb\u4f55\u6761\u4ef6\u6ee1\u8db3\uff08\u5373\u6240\u6709\u6761\u4ef6\u7684\u00a0<code>test<\/code>\u00a0\u5c5e\u6027\u8bc4\u4f30\u4e3a\u00a0<code>false<\/code>\uff09\uff0c\u5219\u00a0<code>&lt;where><\/code>\u00a0\u6807\u7b7e\u4e0d\u4f1a\u751f\u6210\u4efb\u4f55\u00a0<code>WHERE<\/code>\u00a0\u5b50\u53e5\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u00a0<code>&lt;where><\/code>\u00a0\u6807\u7b7e\u5185\u90e8\u53ea\u6709\u4e00\u4e2a\u6761\u4ef6\u6ee1\u8db3\uff0c\u90a3\u4e48\u8fd9\u4e2a\u6761\u4ef6\u5c06\u76f4\u63a5\u4f5c\u4e3a\u00a0<code>WHERE<\/code>\u00a0\u5b50\u53e5\u7684\u4e00\u90e8\u5206\uff0c\u4e0d\u4f1a\u6dfb\u52a0\u00a0<code>AND<\/code>\u00a0\u5173\u952e\u5b57\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u00a0<code>&lt;where><\/code>\u00a0\u6807\u7b7e\u5185\u90e8\u6709\u591a\u4e2a\u6761\u4ef6\u6ee1\u8db3\uff0c\u90a3\u4e48\u8fd9\u4e9b\u6761\u4ef6\u5c06\u901a\u8fc7\u00a0<code>AND<\/code>\u00a0\u5173\u952e\u5b57\u8fde\u63a5\u8d77\u6765\uff0c\u5f62\u6210\u5b8c\u6574\u7684\u00a0<code>WHERE<\/code>\u00a0\u5b50\u53e5\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b<\/h3>\n\n\n\n<p>\u8003\u8651\u4ee5\u4e0bMyBatis\u6620\u5c04\u6587\u4ef6\u4e2d\u7684&nbsp;<code>&lt;select&gt;<\/code>&nbsp;\u5143\u7d20\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;select id=\"findUsers\" resultType=\"User\">\n    SELECT * FROM users\n    &lt;where>\n        &lt;if test=\"username != null\">\n            AND username = #{username}\n        &lt;\/if>\n        &lt;if test=\"age != null\">\n            AND age = #{age}\n        &lt;\/if>\n    &lt;\/where>\n&lt;\/select>\u5982\u679c username \u548c age \u90fd\u4e3a null\uff0c\u5219\u4e0d\u4f1a\u751f\u6210 WHERE \u5b50\u53e5\u3002\n\u5982\u679c\u53ea\u6709 username \u4e0d\u4e3a null\uff0c\u5219\u751f\u6210\u7684SQL\u4e3a SELECT * FROM users WHERE username = ?\u3002\n\u5982\u679c username \u548c age \u90fd\u4e0d\u4e3a null\uff0c\u5219\u751f\u6210\u7684SQL\u4e3a SELECT * FROM users WHERE username = ? AND age = ?\u3002\n\u6ce8\u610f\u4e8b\u9879\n&lt;where> \u6807\u7b7e\u5185\u90e8\u7684\u6761\u4ef6\u5e94\u8be5\u4f7f\u7528 AND \u5173\u952e\u5b57\uff0c\u56e0\u4e3a &lt;where> \u6807\u7b7e\u4f1a\u81ea\u52a8\u5904\u7406 AND \u7684\u6dfb\u52a0\u3002\n\u5982\u679c\u9700\u8981\u4f7f\u7528 OR \u5173\u952e\u5b57\u8fde\u63a5\u6761\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528 &lt;choose>\u3001&lt;when> \u548c &lt;otherwise> \u6807\u7b7e\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u903b\u8f91\u3002\n\u901a\u8fc7\u4f7f\u7528 &lt;where> \u6807\u7b7e\uff0c\u53ef\u4ee5\u7b80\u5316\u52a8\u6001SQL\u7684\u7f16\u5199\uff0c\u907f\u514d\u5728\u6761\u4ef6\u8f83\u591a\u65f6\u624b\u52a8\u7ba1\u7406 AND \u548c OR \u5173\u952e\u5b57\u7684\u4f7f\u7528\uff0c\u4ece\u800c\u51cf\u5c11\u9519\u8bef\u548c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\n\n<\/code><\/pre>\n<\/details>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\u5f15\u5165\u4f9d\u8d56 \u4e3b\u8981\u5c31\u662f\u8981\u544a\u8bc9\u63d2\u4ef6\uff0c\u4ece\u54ea\u9875\u5f00\u59cb\u67e5\uff0c\u4e00\u6b21\u67e5\u591a\u5c11 PageHelper.startPage(empl [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4],"tags":[],"class_list":["post-141","post","type-post","status-publish","format-standard","hentry","category-uncategorized","category-4"],"_links":{"self":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts\/141","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=141"}],"version-history":[{"count":1,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts\/141\/revisions"}],"predecessor-version":[{"id":144,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts\/141\/revisions\/144"}],"wp:attachment":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/media?parent=141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/categories?post=141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/tags?post=141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}