{"id":4357,"date":"2021-12-03T17:50:08","date_gmt":"2021-12-03T09:50:08","guid":{"rendered":"http:\/\/123.57.164.21\/?p=4357"},"modified":"2021-12-03T17:50:08","modified_gmt":"2021-12-03T09:50:08","slug":"spring-boot%e6%95%b4%e5%90%88mybatis","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=4357","title":{"rendered":"Spring Boot\u6574\u5408MyBatis"},"content":{"rendered":"\n<p>MyBatis \u662f\u4e00\u4e2a\u534a\u81ea\u52a8\u5316\u7684 ORM \u6846\u67b6\uff0c\u6240\u8c13\u534a\u81ea\u52a8\u5316\u662f\u6307 MyBatis \u53ea\u652f\u6301\u5c06\u6570\u636e\u5e93\u67e5\u51fa\u7684\u6570\u636e\u6620\u5c04\u5230 POJO \u5b9e\u4f53\u7c7b\u4e0a\uff0c\u800c\u5b9e\u4f53\u5230\u6570\u636e\u5e93\u7684\u6620\u5c04\u5219\u9700\u8981\u6211\u4eec\u81ea\u5df1\u7f16\u5199 SQL \u8bed\u53e5\u5b9e\u73b0\uff0c\u76f8\u8f83\u4e8eHibernate \u8fd9\u79cd\u5b8c\u5168\u81ea\u52a8\u5316\u7684\u6846\u67b6\uff0cMybatis \u66f4\u52a0\u7075\u6d3b\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u81ea\u8eab\u7684\u9700\u6c42\u7f16\u5199 sql \u8bed\u53e5\u6765\u5b9e\u73b0\u590d\u6742\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u968f\u7740 Spring Boot \u8d8a\u6765\u8d8a\u6d41\u884c\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u88ab\u5382\u5546\u53ca\u5f00\u53d1\u8005\u6240\u8ba4\u53ef\uff0cMyBatis \u4e5f\u5f00\u53d1\u4e86\u4e00\u5957\u57fa\u4e8e Spring Boot \u6a21\u5f0f\u7684 starter\uff1amybatis-spring-boot-starter\u3002\u672c\u8282\u6211\u4eec\u5c31\u4ecb\u7ecd\u4e0b\u5982\u4f55\u5728 Spring Boot \u9879\u76ee\u4e2d\u6574\u5408 MyBatis\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u5f15\u5165\u4f9d\u8d56<\/h5>\n\n\n\n<p>Spring Boot \u6574\u5408 MyBatis \u7684\u7b2c\u4e00\u6b65\uff0c\u5c31\u662f\u5728\u9879\u76ee\u7684 pom.xml \u4e2d\u5f15\u5165\u00a0mybatis-spring-boot-starter \u7684\u4f9d\u8d56\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;!--\u5f15\u5165 mybatis-spring-boot-starter \u7684\u4f9d\u8d56-->\n&lt;dependency>\n    &lt;groupId>org.mybatis.spring.boot&lt;\/groupId>\n    &lt;artifactId>mybatis-spring-boot-starter&lt;\/artifactId>\n    &lt;version>2.2.0&lt;\/version>\n&lt;\/dependency>\n<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u914d\u7f6e MyBatis<\/h5>\n\n\n\n<p>\u5728 Spring Boot \u7684\u914d\u7f6e\u6587\u4ef6\uff08application.properties\/yml\uff09\u4e2d\u5bf9 MyBatis \u8fdb\u884c\u914d\u7f6e\uff0c\u4f8b\u5982\u6307\u5b9a mapper.xml \u7684\u4f4d\u7f6e\u3001\u5b9e\u4f53\u7c7b\u7684\u4f4d\u7f6e\u3001\u662f\u5426\u5f00\u542f\u9a7c\u5cf0\u547d\u540d\u6cd5\u7b49\u7b49\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">###################################### MyBatis \u914d\u7f6e######################################\nmybatis:\n  # \u6307\u5b9a mapper.xml \u7684\u4f4d\u7f6e\n  mapper-locations: classpath:mybatis\/mapper\/*.xml\n  #\u626b\u63cf\u5b9e\u4f53\u7c7b\u7684\u4f4d\u7f6e,\u5728\u6b64\u5904\u6307\u660e\u626b\u63cf\u5b9e\u4f53\u7c7b\u7684\u5305\uff0c\u5728 mapper.xml \u4e2d\u5c31\u53ef\u4ee5\u4e0d\u5199\u5b9e\u4f53\u7c7b\u7684\u5168\u8def\u5f84\u540d\n  type-aliases-package: net.biancheng.www.bean\n  configuration:\n    #\u9ed8\u8ba4\u5f00\u542f\u9a7c\u5cf0\u547d\u540d\u6cd5\uff0c\u53ef\u4ee5\u4e0d\u7528\u8bbe\u7f6e\u8be5\u5c5e\u6027\n    map-underscore-to-camel-case: true  \n<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-92-1024x63.png\" alt=\"\" class=\"wp-image-4360\" width=\"736\" height=\"45\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-92-1024x63.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-92-300x19.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-92-768x48.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-92-1536x95.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-92-830x51.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-92-230x14.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-92-350x22.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-92-480x30.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-92.png 1810w\" sizes=\"(max-width: 736px) 100vw, 736px\" \/><\/figure><\/div>\n\n\n\n<h5 class=\"wp-block-heading\">\u521b\u5efa\u5b9e\u4f53\u7c7b<\/h5>\n\n\n\n<p>\u5728\u6307\u5b9a\u7684\u6570\u636e\u5e93\u5185\u521b\u5efa\u4e00\u4e2a user \u8868\uff0c\u5e76\u63d2\u5165\u4e00\u4e9b\u6570\u636e\uff0c\u5982\u4e0b\u8868\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-93.png\" alt=\"\" class=\"wp-image-4361\" width=\"586\" height=\"203\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-93.png 838w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-93-300x104.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-93-768x266.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-93-830x287.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-93-230x80.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-93-350x121.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-93-480x166.png 480w\" sizes=\"(max-width: 586px) 100vw, 586px\" \/><\/figure><\/div>\n\n\n\n<p>\u6839\u636e\u6570\u636e\u5e93 user \u8868\uff0c\u521b\u5efa\u76f8\u5e94\u7684\u5b9e\u4f53\u7c7b User\uff0c\u4ee3\u7801\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package net.biancheng.www.bean;\npublic class User {\n    private Integer id;\n    private String userId;\n    private String userName;\n    private String password;\n    private String email;\n    public Integer getId() {\n        return id;\n    }\n    public void setId(Integer id) {\n        this.id = id;\n    }\n    public String getUserId() {\n        return userId;\n    }\n    public void setUserId(String userId) {\n        this.userId = userId == null ? null : userId.trim();\n    }\n    public String getUserName() {\n        return userName;\n    }\n    public void setUserName(String userName) {\n        this.userName = userName == null ? null : userName.trim();\n    }\n    public String getPassword() {\n        return password;\n    }\n    public void setPassword(String password) {\n        this.password = password == null ? null : password.trim();\n    }\n    public String getEmail() {\n        return email;\n    }\n    public void setEmail(String email) {\n        this.email = email == null ? null : email.trim();\n    }\n}\n<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u521b\u5efa Mapper \u63a5\u53e3<\/h5>\n\n\n\n<p>\u5728 net.biancheng.www.mapper \u4e2d\u521b\u5efa\u4e00\u4e2a UserMapper \u63a5\u53e3\uff0c\u5e76\u5728\u8be5\u7c7b\u4e0a\u4f7f\u7528 @Mapper \u6ce8\u89e3\uff0c\u4ee3\u7801\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package net.biancheng.www.mapper;\nimport net.biancheng.www.bean.User;\nimport org.apache.ibatis.annotations.Mapper;\n@Mapper\npublic interface UserMapper {\n    \/\/\u901a\u8fc7\u7528\u6237\u540d\u5bc6\u7801\u67e5\u8be2\u7528\u6237\u6570\u636e\n    User getByUserNameAndPassword(User user);\n}\n<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-94-1024x85.png\" alt=\"\" class=\"wp-image-4362\" width=\"766\" height=\"63\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-94-1024x85.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-94-300x25.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-94-768x64.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-94-1536x127.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-94-830x69.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-94-230x19.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-94-350x29.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-94-480x40.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-94.png 1760w\" sizes=\"(max-width: 766px) 100vw, 766px\" \/><\/figure><\/div>\n\n\n\n<h5 class=\"wp-block-heading\">\u521b\u5efa Mapper \u6620\u5c04\u6587\u4ef6<\/h5>\n\n\n\n<p>\u5728\u914d\u7f6e\u6587\u4ef6 application.properties\/yml \u901a\u8fc7\u00a0mybatis.mapper-locations \u6307\u5b9a\u7684\u4f4d\u7f6e\u4e2d\u521b\u5efa UserMapper.xml\uff0c\u4ee3\u7801\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?>\n&lt;!DOCTYPE mapper PUBLIC \"-\/\/mybatis.org\/\/DTD Mapper 3.0\/\/EN\" \"http:\/\/mybatis.org\/dtd\/mybatis-3-mapper.dtd\">\n&lt;mapper namespace=\"net.biancheng.www.mapper.UserMapper\">\n    &lt;resultMap id=\"BaseResultMap\" type=\"User\">\n        &lt;id column=\"id\" jdbcType=\"INTEGER\" property=\"id\"\/>\n        &lt;result column=\"user_id\" jdbcType=\"VARCHAR\" property=\"userId\"\/>\n        &lt;result column=\"user_name\" jdbcType=\"VARCHAR\" property=\"userName\"\/>\n        &lt;result column=\"password\" jdbcType=\"VARCHAR\" property=\"password\"\/>\n        &lt;result column=\"email\" jdbcType=\"VARCHAR\" property=\"email\"\/>\n    &lt;\/resultMap>\n    &lt;sql id=\"Base_Column_List\">\n        id, user_id, user_name, password, email\n    &lt;\/sql>\n    &lt;!--\u6839\u636e\u7528\u6237\u540d\u5bc6\u7801\u67e5\u8be2\u7528\u6237\u4fe1\u606f-->\n    &lt;!--application.yml \u4e2d\u901a\u8fc7 type-aliases-package \u6307\u5b9a\u4e86\u5b9e\u4f53\u7c7b\u7684\u4e3a\u4e86\uff0c\u56e0\u6b64-->\n    &lt;select id=\"getByUserNameAndPassword\" resultType=\"User\">\n        select *\n        from user\n        where user_name = #{userName,jdbcType=VARCHAR}\n          and password = #{password,jdbcType=VARCHAR}\n    &lt;\/select>\n&lt;\/mapper>\n<\/pre>\n\n\n\n<p>\u4f7f\u7528 Mapper \u8fdb\u884c\u5f00\u53d1\u65f6\uff0c\u9700\u8981\u9075\u5faa\u4ee5\u4e0b\u89c4\u5219\uff1a<\/p>\n\n\n\n<ul><li>mapper \u6620\u5c04\u6587\u4ef6\u4e2d namespace \u5fc5\u987b\u4e0e\u5bf9\u5e94\u7684 mapper \u63a5\u53e3\u7684\u5b8c\u5168\u9650\u5b9a\u540d\u4e00\u81f4\u3002<\/li><li>mapper \u6620\u5c04\u6587\u4ef6\u4e2d statement \u7684 id \u5fc5\u987b\u4e0e mapper \u63a5\u53e3\u4e2d\u7684\u65b9\u6cd5\u7684\u65b9\u6cd5\u540d\u4e00\u81f4<\/li><li>mapper \u6620\u5c04\u6587\u4ef6\u4e2d statement \u7684 parameterType \u6307\u5b9a\u7684\u7c7b\u578b\u5fc5\u987b\u4e0e mapper \u63a5\u53e3\u4e2d\u65b9\u6cd5\u7684\u53c2\u6570\u7c7b\u578b\u4e00\u81f4\u3002<\/li><li>mapper \u6620\u5c04\u6587\u4ef6\u4e2d statement \u7684 resultType \u6307\u5b9a\u7684\u7c7b\u578b\u5fc5\u987b\u4e0e mapper \u63a5\u53e3\u4e2d\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4e00\u81f4\u3002<\/li><\/ul>\n\n\n\n<h5 class=\"wp-block-heading\">\u793a\u4f8b 1<\/h5>\n\n\n\n<p>1. \u5728\u00a0spring-boot-adminex \u9879\u76ee\u4e2d net.biancheng.www.service \u5305\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u00a0UserService \u7684\u63a5\u53e3\uff0c\u4ee3\u7801\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package net.biancheng.www.service;\nimport net.biancheng.www.bean.User;\npublic interface UserService {\n    public User getByUserNameAndPassword(User user);\n}\n<\/pre>\n\n\n\n<p>2. \u5728\u00a0net.biancheng.www.service.impl \u5305\u4e2d\u521b\u5efa\u00a0UserService \u63a5\u53e3\u7684\u5b9e\u73b0\u7c7b\uff0c\u5e76\u4f7f\u7528 @@Service \u6ce8\u89e3\u5c06\u5176\u4ee5\u7ec4\u4ef6\u7684\u5f62\u5f0f\u6dfb\u52a0\u5230\u5bb9\u5668\u4e2d\uff0c\u4ee3\u7801\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package net.biancheng.www.service.impl;\nimport net.biancheng.www.bean.User;\nimport net.biancheng.www.mapper.UserMapper;\nimport net.biancheng.www.service.UserService;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Service;\n@Service(\"userService\")\npublic class UserServiceImpl implements UserService {\n    @Autowired\n    UserMapper userMapper;\n    @Override\n    public User getByUserNameAndPassword(User user) {\n        User loginUser = userMapper.getByUserNameAndPassword(user);\n        return loginUser;\n    }\n}\n<\/pre>\n\n\n\n<p>3. \u4fee\u6539 LoginController \u4e2d\u7684 doLogin() \u65b9\u6cd5 ,\u4ee3\u7801\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package net.biancheng.www.controller;\nimport lombok.extern.slf4j.Slf4j;\nimport net.biancheng.www.bean.User;\nimport net.biancheng.www.service.UserService;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Controller;\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport javax.servlet.http.HttpSession;\nimport java.util.Map;\n@Slf4j\n@Controller\npublic class LoginController {\n    @Autowired\n    UserService userService;\n    @RequestMapping(\"\/user\/login\")\n    public String doLogin(User user, Map&lt;String, Object> map, HttpSession session) {\n        \/\/\u4ece\u6570\u636e\u5e93\u4e2d\u67e5\u8be2\u7528\u6237\u4fe1\u606f\n        User loginUser = userService.getByUserNameAndPassword(user);\n        if (loginUser != null) {\n            session.setAttribute(\"loginUser\", loginUser);\n            log.info(\"\u767b\u9646\u6210\u529f\uff0c\u7528\u6237\u540d\uff1a\" + loginUser.getUserName());\n            \/\/\u9632\u6b62\u91cd\u590d\u63d0\u4ea4\u4f7f\u7528\u91cd\u5b9a\u5411\n            return \"redirect:\/main.html\";\n        } else {\n            map.put(\"msg\", \"\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef\");\n            log.error(\"\u767b\u9646\u5931\u8d25\");\n            return \"login\";\n        }\n    }\n}\n<\/pre>\n\n\n\n<p>4. \u542f\u52a8 Spring Boot\uff0c\u6d4f\u89c8\u5668\u5730\u5740\u680f\u8f93\u5165\u201chttp:\/\/localhost:8080\/\u201d \uff0c\u8bbf\u95ee AdminEx \u7cfb\u7edf\u7684\u767b\u9646\u9875\u9762\uff0c\u5206\u522b\u8f93\u5165\u7528\u6237\u540d\u201cuser\u201d\u548c\u5bc6\u7801\u201c123456\u201d,\u7ed3\u679c\u4e0b\u56fe\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-96-734x1024.png\" alt=\"\" class=\"wp-image-4364\" width=\"418\" height=\"583\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-96-734x1024.png 734w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-96-215x300.png 215w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-96-768x1072.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-96-830x1158.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-96-230x321.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-96-350x488.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-96-480x670.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-96.png 1002w\" sizes=\"(max-width: 418px) 100vw, 418px\" \/><\/figure><\/div>\n\n\n\n<p>5. \u70b9\u51fb\u767b\u9646\u6309\u94ae\uff0c\u7ed3\u679c\u5982\u4e0b\u56fe\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-97-1024x572.png\" alt=\"\" class=\"wp-image-4365\" width=\"563\" height=\"314\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-97-1024x572.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-97-300x168.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-97-768x429.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-97-1536x858.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-97-830x464.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-97-230x129.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-97-350x196.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-97-480x268.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-97.png 1786w\" sizes=\"(max-width: 563px) 100vw, 563px\" \/><\/figure><\/div>\n\n\n\n<h5 class=\"wp-block-heading\">\u6ce8\u89e3\u65b9\u5f0f<\/h5>\n\n\n\n<p>\u901a\u8fc7\u4e0a\u9762\u7684\u5b66\u4e60\uff0c\u6211\u4eec\u77e5\u9053 mapper \u6620\u5c04\u6587\u4ef6\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a XML \u914d\u7f6e\u6587\u4ef6\uff0c\u5b83\u5b58\u5728 XML \u914d\u7f6e\u6587\u4ef6\u7684\u901a\u75c5\uff0c\u5373\u7f16\u5199\u7e41\u7410\uff0c\u5bb9\u6613\u51fa\u9519\u3002\u5373\u4f7f\u662f\u4e00\u4e2a\u5341\u5206\u7b80\u5355\u9879\u76ee\uff0c\u6d89\u53ca\u7684 SQL \u8bed\u53e5\u4e5f\u90fd\u5341\u5206\u7b80\u5355\uff0c\u6211\u4eec\u4ecd\u7136\u9700\u8981\u82b1\u8d39\u4e00\u5b9a\u7684\u65f6\u95f4\u5728mapper \u6620\u5c04\u6587\u4ef6\u7684\u914d\u7f6e\u4e0a\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cMyBatis \u9488\u5bf9\u5b9e\u9645\u5b9e\u9645\u4e1a\u52a1\u4e2d\u4f7f\u7528\u6700\u591a\u7684\u201c\u589e\u4f24\u6539\u67e5\u201d\u64cd\u4f5c\uff0c\u5206\u522b\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u6ce8\u89e3\u6765\u66ff\u6362 mapper \u6620\u5c04\u6587\u4ef6\uff0c\u7b80\u5316\u914d\u7f6e\uff1a<\/p>\n\n\n\n<ul><li>@Select<\/li><li>@Insert<\/li><li>@Update<\/li><li>@Delete<\/li><\/ul>\n\n\n\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6ce8\u89e3\uff0c\u57fa\u672c\u53ef\u4ee5\u6ee1\u8db3\u6211\u4eec\u5bf9\u6570\u636e\u5e93\u7684\u589e\u5220\u6539\u67e5\u64cd\u4f5c\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package net.biancheng.www.mapper;\nimport net.biancheng.www.bean.User;\nimport org.apache.ibatis.annotations.*;\nimport java.util.List;\n@Mapper\npublic interface UserMapper {\n    @Select(\"select * from user where user_name = #{userName,jdbcType=VARCHAR} and password = #{password,jdbcType=VARCHAR}\")\n    List&lt;User> getByUserNameAndPassword(User user);\n    @Delete(\"delete from user where id = #{id,jdbcType=INTEGER}\")\n    int deleteByPrimaryKey(Integer id);\n    @Insert(\"insert into user ( user_id, user_name, password, email)\" +\n            \"values ( #{userId,jdbcType=VARCHAR}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR})\")\n    int insert(User record);\n    @Update(\" update user\" +\n            \"    set user_id = #{userId,jdbcType=VARCHAR},\\n\" +\n            \"      user_name = #{userName,jdbcType=VARCHAR},\\n\" +\n            \"      password = #{password,jdbcType=VARCHAR},\\n\" +\n            \"      email = #{email,jdbcType=VARCHAR}\\n\" +\n            \"    where id = #{id,jdbcType=INTEGER}\")\n    int updateByPrimaryKey(User record);\n}<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u6ce8\u610f\u4e8b\u9879<\/h5>\n\n\n\n<p>mapper \u63a5\u53e3\u4e2d\u7684\u4efb\u4f55\u4e00\u4e2a\u65b9\u6cd5\uff0c\u90fd\u53ea\u80fd\u4f7f\u7528\u4e00\u79cd\u914d\u7f6e\u65b9\u5f0f\uff0c\u5373\u6ce8\u89e3\u548c mapper \u6620\u5c04\u6587\u4ef6\u4e8c\u9009\u4e00\uff0c\u4f46\u4e0d\u540c\u65b9\u6cd5\u4e4b\u95f4\uff0c\u8fd9\u4e24\u79cd\u65b9\u5f0f\u5219\u53ef\u4ee5\u6df7\u5408\u4f7f\u7528\uff0c\u4f8b\u5982\u65b9\u6cd5 1 \u4f7f\u7528\u6ce8\u89e3\u65b9\u5f0f\uff0c\u65b9\u6cd5 2 \u4f7f\u7528 mapper \u6620\u5c04\u6587\u4ef6\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u6839\u636e SQL \u7684\u590d\u6742\u7a0b\u5ea6\uff0c\u9009\u62e9\u4e0d\u540c\u7684\u65b9\u5f0f\u6765\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n\n\n\n<ul><li>\u5982\u679c\u6ca1\u6709\u590d\u6742\u7684\u8fde\u63a5\u67e5\u8be2\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6ce8\u89e3\u7684\u65b9\u5f0f\u6765\u7b80\u5316\u914d\u7f6e\uff1b<\/li><li>\u5982\u679c\u6d89\u53ca\u7684 sql \u8f83\u4e3a\u590d\u6742\u65f6\uff0c\u5219\u4f7f\u7528 XML \uff08mapper \u6620\u5c04\u6587\u4ef6\uff09\u7684\u65b9\u5f0f\u66f4\u597d\u4e00\u4e9b\u3002<\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>MyBatis \u662f\u4e00\u4e2a\u534a\u81ea\u52a8\u5316\u7684 ORM \u6846\u67b6\uff0c\u6240\u8c13\u534a\u81ea\u52a8\u5316\u662f\u6307 MyBatis \u53ea\u652f\u6301\u5c06\u6570\u636e\u5e93\u67e5\u51fa\u7684\u6570\u636e\u6620\u5c04 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4357"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4357"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4357\/revisions"}],"predecessor-version":[{"id":4367,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4357\/revisions\/4367"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}