{"id":400,"date":"2025-07-20T03:27:11","date_gmt":"2025-07-19T19:27:11","guid":{"rendered":"https:\/\/www.hgfwq.com\/blog\/?p=400"},"modified":"2025-07-20T03:27:11","modified_gmt":"2025-07-19T19:27:11","slug":"mybatis-plus%e5%85%a8%e5%ad%97%e6%ae%b5%e6%a8%a1%e7%b3%8a%e6%9f%a5%e8%af%a2%e5%ae%9e%e6%88%98%e4%b8%8e%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/www.hgfwq.com\/blog\/400.html","title":{"rendered":"MyBatis-Plus\u5168\u5b57\u6bb5\u6a21\u7cca\u67e5\u8be2\u5b9e\u6218\u4e0e\u6700\u4f73\u5b9e\u8df5"},"content":{"rendered":"<p><h>MyBatis-Plus\u5168\u5b57\u6bb5\u6a21\u7cca\u67e5\u8be2\u6307\u5357<\/h><\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.hgfwq.com\/blog\/wp-content\/uploads\/2025\/07\/Zl832.jpg\" alt=\"MyBatis-Plus\u5168\u5b57\u6bb5\u6a21\u7cca\u67e5\u8be2\u5b9e\u6218\u4e0e\u6700\u4f73\u5b9e\u8df5\" title=\"MyBatis-Plus\u5168\u5b57\u6bb5\u6a21\u7cca\u67e5\u8be2\u5b9e\u6218\u4e0e\u6700\u4f73\u5b9e\u8df5\"><\/p>\n<p><p>\u968f\u7740\u9879\u76ee\u7684\u4e0d\u65ad\u6269\u5c55\uff0c\u6570\u636e\u5e93\u8868\u7684\u6570\u91cf\u548c\u590d\u6742\u6027\u4e5f\u5728\u4e0d\u65ad\u589e\u52a0\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5b9e\u73b0\u5168\u5b57\u6bb5\u7684\u6a21\u7cca\u67e5\u8be2\u529f\u80fd\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u7528\u6237\u4f53\u9a8c\u3002MyBatis-Plus\u662f\u4e00\u6b3e\u4f18\u79c0\u7684\u6301\u4e45\u5c42\u6846\u67b6\uff0c\u652f\u6301\u5168\u5b57\u6bb5\u6a21\u7cca\u67e5\u8be2\uff0c\u4e0b\u9762\u5c06\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u5728MyBatis-Plus\u4e2d\u5b9e\u73b0\u5168\u5b57\u6bb5\u6a21\u7cca\u67e5\u8be2\u3002<\/p>\n<\/p>\n<p><h>1. \u914d\u7f6eMyBatis-Plus<\/h><\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684\u9879\u76ee\u4e2d\u5df2\u7ecf\u6dfb\u52a0\u4e86MyBatis-Plus\u4f9d\u8d56\u3002\u5728Maven\u9879\u76ee\u4e2d\uff0c\u53ef\u4ee5\u5728pom.xml\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4f9d\u8d56\uff1a<\/p>\n<\/p>\n<p>&#8220;`xml<\/p>\n<p><dependency><\/p>\n<p>    <groupId>com.baomidou<\/groupId><\/p>\n<p>    <artifactId>mybatis-plus-boot-starter<\/artifactId><\/p>\n<p>    <version>\u6700\u65b0\u7248\u672c\u53f7<\/version><\/p>\n<p><\/dependency><\/p>\n<p>&#8220;`<\/p>\n<p><h>2. \u521b\u5efa\u5b9e\u4f53\u7c7b<\/h><\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u5b9e\u4f53\u7c7b\uff0c\u7528\u4e8e\u6620\u5c04\u6570\u636e\u5e93\u8868\u3002\u5b9e\u4f53\u7c7b\u4e2d\u9700\u8981\u5305\u542b\u6240\u6709\u9700\u8981\u6a21\u7cca\u67e5\u8be2\u7684\u5b57\u6bb5\uff0c\u5e76\u4f7f\u7528`@TableField`\u6ce8\u89e3\u6307\u5b9a\u5b57\u6bb5\u540d\u3002<\/p>\n<\/p>\n<p>&#8220;`java<\/p>\n<p>import com.baomidou.mybatisplus.annotation.TableField;<\/p>\n<p>import com.baomidou.mybatisplus.annotation.TableId;<\/p>\n<p>import com.baomidou.mybatisplus.annotation.TableName;<\/p>\n<p>@TableName(&#8220;your_table_name&#8221;)<\/p>\n<p>public class YourEntity {<\/p>\n<p>    @TableId<\/p>\n<p>    private Long id;<\/p>\n<p>    @TableField(&#8220;field1&#8221;)<\/p>\n<p>    private String field1;<\/p>\n<p>    @TableField(&#8220;field2&#8221;)<\/p>\n<p>    private String field2;<\/p>\n<p>    @TableField(&#8220;field3&#8221;)<\/p>\n<p>    private String field3;<\/p>\n<p>    \/\/ &#8230; \u5176\u4ed6\u5b57\u6bb5<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p><h>3. \u521b\u5efaMapper\u63a5\u53e3<\/h><\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aMapper\u63a5\u53e3\uff0c\u7ee7\u627fMyBatis-Plus\u7684BaseMapper\u63a5\u53e3\u3002\u5728Mapper\u63a5\u53e3\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528`SelectAllLike`\u65b9\u6cd5\u5b9e\u73b0\u5168\u5b57\u6bb5\u6a21\u7cca\u67e5\u8be2\u3002<\/p>\n<\/p>\n<p>&#8220;`java<\/p>\n<p>import com.baomidou.mybatisplus.core.mapper.BaseMapper;<\/p>\n<p>import com.baomidou.mybatisplus.extension.plugins.pagination.Page;<\/p>\n<p>import org.apache.ibatis.annotations.Param;<\/p>\n<p>public interface YourMapper extends BaseMapper<YourEntity> {<\/p>\n<p>    Page<YourEntity> selectAllLike(@Param(&#8220;keyWord&#8221;) String keyWord);<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p><h>4. \u5b9e\u73b0\u5168\u5b57\u6bb5\u6a21\u7cca\u67e5\u8be2<\/h><\/p>\n<p><p>\u5728Service\u5c42\u4e2d\uff0c\u5b9e\u73b0\u5168\u5b57\u6bb5\u6a21\u7cca\u67e5\u8be2\u7684\u65b9\u6cd5\u3002\u8fd9\u91cc\u4f7f\u7528MyBatis-Plus\u7684`LambdaQueryWrapper`\u6765\u6784\u5efa\u67e5\u8be2\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p>&#8220;`java<\/p>\n<p>import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;<\/p>\n<p>import com.baomidou.mybatisplus.extension.plugins.pagination.Page;<\/p>\n<p>import org.springframework.beans.factory.annotation.Autowired;<\/p>\n<p>import org.springframework.stereotype.Service;<\/p>\n<p>@Service<\/p>\n<p>public class YourService {<\/p>\n<p>    @Autowired<\/p>\n<p>    private YourMapper yourMapper;<\/p>\n<p>    public Page<YourEntity> searchAllByKeyword(String keyword) {<\/p>\n<p>        LambdaQueryWrapper<YourEntity> queryWrapper = new LambdaQueryWrapper<>();<\/p>\n<p>        queryWrapper.like(YourEntity::getField1, keyword)<\/p>\n<p>                    .or()<\/p>\n<p>                    .like(YourEntity::getField2, keyword)<\/p>\n<p>                    .or()<\/p>\n<p>                    .like(YourEntity::getField3, keyword)<\/p>\n<p>                    \/\/ &#8230; \u5bf9\u5176\u4ed6\u5b57\u6bb5\u8fdb\u884c\u76f8\u540c\u7684\u6a21\u7cca\u67e5\u8be2<\/p>\n<p>                    .or()<\/p>\n<p>                    .like(YourEntity::getFieldN, keyword);<\/p>\n<p>        return yourMapper.selectPage(new Page<>(1, 10), queryWrapper);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p><h>5. \u4f7f\u7528\u5168\u5b57\u6bb5\u6a21\u7cca\u67e5\u8be2<\/h><\/p>\n<p><p>\u5728Controller\u5c42\u4e2d\uff0c\u8c03\u7528Service\u5c42\u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u5168\u5b57\u6bb5\u6a21\u7cca\u67e5\u8be2\u7684\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p>&#8220;`java<\/p>\n<p>import org.springframework.beans.factory.annotation.Autowired;<\/p>\n<p>import org.springframework.web.bind.annotation.GetMapping;<\/p>\n<p>import org.springframework.web.bind.annotation.RequestParam;<\/p>\n<p>import org.springframework.web.bind.annotation.RestController;<\/p>\n<p>@RestController<\/p>\n<p>public class YourController {<\/p>\n<p>    @Autowired<\/p>\n<p>    private YourService yourService;<\/p>\n<p>    @GetMapping(&#8220;\/search&#8221;)<\/p>\n<p>    public Page<YourEntity> search(@RequestParam(&#8220;keyword&#8221;) String keyword) {<\/p>\n<p>        return yourService.searchAllByKeyword(keyword);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p><h>\u95ee\u7b54\u73af\u8282<\/h><\/p>\n<p><b>\u95ee\uff1a<\/b>MyBatis-Plus\u7684\u5168\u5b57\u6bb5\u6a21\u7cca\u67e5\u8be2\u4f1a\u5bf9\u6570\u636e\u5e93\u6027\u80fd\u4ea7\u751f\u5f88\u5927\u5f71\u54cd\u5417\uff1f<\/p>\n<p><p>\u7b54\uff1a\u5168\u5b57\u6bb5\u6a21\u7cca\u67e5\u8be2\u53ef\u80fd\u4f1a\u5bf9\u6570\u636e\u5e93\u6027\u80fd\u4ea7\u751f\u4e00\u5b9a\u5f71\u54cd\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u626b\u63cf\u6240\u6709\u5b57\u6bb5\u3002\u5728\u6570\u636e\u91cf\u8f83\u5927\u6216\u5b57\u6bb5\u8f83\u591a\u7684\u8868\u4e0a\u4f7f\u7528\u65f6\uff0c\u5efa\u8bae\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u4f18\u5316\uff0c\u5982\u5efa\u7acb\u9002\u5f53\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><b>\u95ee\uff1a<\/b>\u5982\u4f55\u4f18\u5316\u5168\u5b57\u6bb5\u6a21\u7cca\u67e5\u8be2\u7684\u6027\u80fd\uff1f<\/p>\n<p><p>\u7b54\uff1a\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u4f18\u5316\u5168\u5b57\u6bb5\u6a21\u7cca\u67e5\u8be2\u7684\u6027\u80fd\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5bf9\u67e5\u8be2\u7684\u5b57\u6bb5\u5efa\u7acb\u7d22\u5f15\u3002<\/li>\n<li>\u5408\u7406\u9650\u5236\u67e5\u8be2\u7ed3\u679c\u7684\u6570\u91cf\uff0c\u4f8b\u5982\u4f7f\u7528\u5206\u9875\u67e5\u8be2\u3002<\/li>\n<li>\u5728\u4e1a\u52a1\u903b\u8f91\u4e0a\u51cf\u5c11\u5168\u5b57\u6bb5\u6a21\u7cca\u67e5\u8be2\u7684\u4f7f\u7528\uff0c\u5c3d\u91cf\u4f7f\u7528\u7cbe\u786e\u67e5\u8be2\u3002<\/li>\n<\/ul>\n<p><b>\u95ee\uff1a<\/b>MyBatis-Plus\u7684\u5168\u5b57\u6bb5\u6a21\u7cca\u67e5\u8be2\u662f\u5426\u652f\u6301\u81ea\u5b9a\u4e49\u67e5\u8be2\u6761\u4ef6\uff1f<\/p>\n<p><p>\u7b54\uff1a\u662f\u7684\uff0cMyBatis-Plus\u7684\u5168\u5b57\u6bb5\u6a21\u7cca\u67e5\u8be2\u652f\u6301\u81ea\u5b9a\u4e49\u67e5\u8be2\u6761\u4ef6\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\uff0c\u5728LambdaQueryWrapper\u4e2d\u6dfb\u52a0\u4e0d\u540c\u7684\u67e5\u8be2\u6761\u4ef6\uff0c\u5b9e\u73b0\u590d\u6742\u7684\u67e5\u8be2\u903b\u8f91\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>MyBatis-Plus\u5168\u5b57\u6bb5\u6a21\u7cca\u67e5\u8be2\u6307\u5357 \u968f\u7740\u9879\u76ee\u7684\u4e0d\u65ad\u6269\u5c55\uff0c\u6570\u636e\u5e93\u8868\u7684\u6570\u91cf\u548c\u590d\u6742\u6027\u4e5f\u5728\u4e0d\u65ad\u589e\u52a0\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5b9e\u73b0\u5168\u5b57\u6bb5\u7684\u6a21\u7cca\u67e5\u8be2\u529f\u80fd\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u7528\u6237\u4f53\u9a8c\u3002MyBatis-Plus\u662f\u4e00\u6b3e\u4f18\u79c0\u7684\u6301\u4e45\u5c42\u6846\u67b6\uff0c\u652f\u6301\u5168\u5b57\u6bb5\u6a21\u7cca\u67e5\u8be2\uff0c\u4e0b&#8230;<\/p>\n","protected":false},"author":1,"featured_media":401,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[203,204,205],"topic":[],"class_list":["post-400","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-haiwai","tag-mybatis","tag-plus","tag-205"],"_links":{"self":[{"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/posts\/400","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/comments?post=400"}],"version-history":[{"count":1,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/posts\/400\/revisions"}],"predecessor-version":[{"id":403,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/posts\/400\/revisions\/403"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/media\/401"}],"wp:attachment":[{"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/media?parent=400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/categories?post=400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/tags?post=400"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/topic?post=400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}