├── .gitignore
├── LICENSE
├── README.md
└── mp
├── .gitignore
├── .mvn
└── wrapper
│ ├── MavenWrapperDownloader.java
│ ├── maven-wrapper.jar
│ └── maven-wrapper.properties
├── pom.xml
└── src
├── main
├── java
│ └── com
│ │ └── lord
│ │ └── mp
│ │ ├── MpApplication.java
│ │ ├── config
│ │ └── MyBatisPlusConfig.java
│ │ ├── dao
│ │ └── UserMapper.java
│ │ ├── entity
│ │ └── User.java
│ │ └── service
│ │ ├── UserService.java
│ │ └── impl
│ │ └── UserServiceImpl.java
└── resources
│ ├── application.yml
│ ├── mapper
│ └── UserMapper.xml
│ ├── mybatis-config.xml
│ └── sql
│ └── 数据库脚本.sql
└── test
└── java
└── com
└── lord
└── mp
├── MpApplicationTests.java
└── user
├── ARTest.java
├── SimpleTest.java
├── UserServiceTest.java
└── UserTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | # BlueJ files
8 | *.ctxt
9 |
10 | # Mobile Tools for Java (J2ME)
11 | .mtj.tmp/
12 |
13 | # Package Files #
14 | *.jar
15 | *.war
16 | *.nar
17 | *.ear
18 | *.zip
19 | *.tar.gz
20 | *.rar
21 |
22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23 | hs_err_pid*
24 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # imooc_mp
2 | 慕课网《MyBatis-Plus入门》课程源码
3 |
--------------------------------------------------------------------------------
/mp/.gitignore:
--------------------------------------------------------------------------------
1 | HELP.md
2 | /target/
3 | !.mvn/wrapper/maven-wrapper.jar
4 |
5 | ### STS ###
6 | .apt_generated
7 | .classpath
8 | .factorypath
9 | .project
10 | .settings
11 | .springBeans
12 | .sts4-cache
13 |
14 | ### IntelliJ IDEA ###
15 | .idea
16 | *.iws
17 | *.iml
18 | *.ipr
19 |
20 | ### NetBeans ###
21 | /nbproject/private/
22 | /nbbuild/
23 | /dist/
24 | /nbdist/
25 | /.nb-gradle/
26 | /build/
27 |
28 | ### VS Code ###
29 | .vscode/
30 |
--------------------------------------------------------------------------------
/mp/.mvn/wrapper/MavenWrapperDownloader.java:
--------------------------------------------------------------------------------
1 | /*
2 | Licensed to the Apache Software Foundation (ASF) under one
3 | or more contributor license agreements. See the NOTICE file
4 | distributed with this work for additional information
5 | regarding copyright ownership. The ASF licenses this file
6 | to you under the Apache License, Version 2.0 (the
7 | "License"); you may not use this file except in compliance
8 | with the License. You may obtain a copy of the License at
9 |
10 | https://www.apache.org/licenses/LICENSE-2.0
11 |
12 | Unless required by applicable law or agreed to in writing,
13 | software distributed under the License is distributed on an
14 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | KIND, either express or implied. See the License for the
16 | specific language governing permissions and limitations
17 | under the License.
18 | */
19 |
20 | import java.io.File;
21 | import java.io.FileInputStream;
22 | import java.io.FileOutputStream;
23 | import java.io.IOException;
24 | import java.net.URL;
25 | import java.nio.channels.Channels;
26 | import java.nio.channels.ReadableByteChannel;
27 | import java.util.Properties;
28 |
29 | public class MavenWrapperDownloader {
30 |
31 | /**
32 | * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
33 | */
34 | private static final String DEFAULT_DOWNLOAD_URL =
35 | "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar";
36 |
37 | /**
38 | * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
39 | * use instead of the default one.
40 | */
41 | private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
42 | ".mvn/wrapper/maven-wrapper.properties";
43 |
44 | /**
45 | * Path where the maven-wrapper.jar will be saved to.
46 | */
47 | private static final String MAVEN_WRAPPER_JAR_PATH =
48 | ".mvn/wrapper/maven-wrapper.jar";
49 |
50 | /**
51 | * Name of the property which should be used to override the default download url for the wrapper.
52 | */
53 | private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
54 |
55 | public static void main(String args[]) {
56 | System.out.println("- Downloader started");
57 | File baseDirectory = new File(args[0]);
58 | System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
59 |
60 | // If the maven-wrapper.properties exists, read it and check if it contains a custom
61 | // wrapperUrl parameter.
62 | File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
63 | String url = DEFAULT_DOWNLOAD_URL;
64 | if(mavenWrapperPropertyFile.exists()) {
65 | FileInputStream mavenWrapperPropertyFileInputStream = null;
66 | try {
67 | mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
68 | Properties mavenWrapperProperties = new Properties();
69 | mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
70 | url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
71 | } catch (IOException e) {
72 | System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
73 | } finally {
74 | try {
75 | if(mavenWrapperPropertyFileInputStream != null) {
76 | mavenWrapperPropertyFileInputStream.close();
77 | }
78 | } catch (IOException e) {
79 | // Ignore ...
80 | }
81 | }
82 | }
83 | System.out.println("- Downloading from: : " + url);
84 |
85 | File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
86 | if(!outputFile.getParentFile().exists()) {
87 | if(!outputFile.getParentFile().mkdirs()) {
88 | System.out.println(
89 | "- ERROR creating output direcrory '" + outputFile.getParentFile().getAbsolutePath() + "'");
90 | }
91 | }
92 | System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
93 | try {
94 | downloadFileFromURL(url, outputFile);
95 | System.out.println("Done");
96 | System.exit(0);
97 | } catch (Throwable e) {
98 | System.out.println("- Error downloading");
99 | e.printStackTrace();
100 | System.exit(1);
101 | }
102 | }
103 |
104 | private static void downloadFileFromURL(String urlString, File destination) throws Exception {
105 | URL website = new URL(urlString);
106 | ReadableByteChannel rbc;
107 | rbc = Channels.newChannel(website.openStream());
108 | FileOutputStream fos = new FileOutputStream(destination);
109 | fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
110 | fos.close();
111 | rbc.close();
112 | }
113 |
114 | }
115 |
--------------------------------------------------------------------------------
/mp/.mvn/wrapper/maven-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wangshicon/imooc_mp/27f0801e64c722577143545c35a12d680c1d31cc/mp/.mvn/wrapper/maven-wrapper.jar
--------------------------------------------------------------------------------
/mp/.mvn/wrapper/maven-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip
2 |
--------------------------------------------------------------------------------
/mp/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | com.lord
7 | mp
8 | 0.0.1-SNAPSHOT
9 | jar
10 |
11 | MP
12 | MP project for Spring Boot
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter-parent
17 | 2.1.4.RELEASE
18 |
19 |
20 |
21 |
22 | UTF-8
23 | UTF-8
24 | 1.8
25 |
26 |
27 |
28 |
29 | org.springframework.boot
30 | spring-boot-starter
31 |
32 |
33 |
34 | org.springframework.boot
35 | spring-boot-starter-test
36 | test
37 |
38 |
39 |
40 | org.projectlombok
41 | lombok
42 | true
43 |
44 |
45 |
46 | com.baomidou
47 | mybatis-plus-boot-starter
48 | 3.1.1
49 |
50 |
51 |
52 | mysql
53 | mysql-connector-java
54 | runtime
55 |
56 |
57 |
58 |
59 |
60 |
61 | org.springframework.boot
62 | spring-boot-maven-plugin
63 |
64 |
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/mp/src/main/java/com/lord/mp/MpApplication.java:
--------------------------------------------------------------------------------
1 | package com.lord.mp;
2 |
3 | import org.mybatis.spring.annotation.MapperScan;
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 |
7 | @SpringBootApplication
8 | @MapperScan("com.lord.mp.dao")
9 | public class MpApplication {
10 |
11 | public static void main(String[] args) {
12 | SpringApplication.run(MpApplication.class, args);
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/mp/src/main/java/com/lord/mp/config/MyBatisPlusConfig.java:
--------------------------------------------------------------------------------
1 | package com.lord.mp.config;
2 |
3 | import org.springframework.context.annotation.Bean;
4 | import org.springframework.context.annotation.Configuration;
5 |
6 | import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
7 |
8 | @Configuration
9 | public class MyBatisPlusConfig {
10 |
11 | @Bean
12 | public PaginationInterceptor paginationInterceptor() {
13 | return new PaginationInterceptor();
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/mp/src/main/java/com/lord/mp/dao/UserMapper.java:
--------------------------------------------------------------------------------
1 | package com.lord.mp.dao;
2 |
3 | import java.util.List;
4 |
5 | import org.apache.ibatis.annotations.Param;
6 | import org.springframework.stereotype.Repository;
7 |
8 | import com.baomidou.mybatisplus.core.conditions.Wrapper;
9 | import com.baomidou.mybatisplus.core.mapper.BaseMapper;
10 | import com.baomidou.mybatisplus.core.metadata.IPage;
11 | import com.baomidou.mybatisplus.core.toolkit.Constants;
12 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
13 | import com.lord.mp.entity.User;
14 |
15 | /**
16 | * @Description: 用户信息持久层
17 | * @author Lord
18 | * @date 2019年5月30日
19 | */
20 | @Repository
21 | public interface UserMapper extends BaseMapper {
22 |
23 | // @Select("select * from user ${ew.customSqlSegment}")
24 | List selectAll(@Param(Constants.WRAPPER) Wrapper wrapper);
25 |
26 | IPage selectUserPage(Page page, @Param(Constants.WRAPPER) Wrapper wrapper);
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/mp/src/main/java/com/lord/mp/entity/User.java:
--------------------------------------------------------------------------------
1 | package com.lord.mp.entity;
2 |
3 | import java.util.Date;
4 |
5 | import com.baomidou.mybatisplus.annotation.IdType;
6 | import com.baomidou.mybatisplus.annotation.SqlCondition;
7 | import com.baomidou.mybatisplus.annotation.TableField;
8 | import com.baomidou.mybatisplus.annotation.TableId;
9 | import com.baomidou.mybatisplus.annotation.TableName;
10 | import com.baomidou.mybatisplus.extension.activerecord.Model;
11 |
12 | import lombok.Data;
13 | import lombok.EqualsAndHashCode;
14 |
15 | /**
16 | * @Description: 用户信息表的对应实体
17 | * @author Lord
18 | * @date 2019年5月30日
19 | */
20 | @Data
21 | @EqualsAndHashCode(callSuper = false)
22 | @TableName("user")
23 | public class User extends Model {
24 |
25 | private static final long serialVersionUID = 2603890837103787306L;
26 |
27 | /**
28 | * 主键
29 | */
30 | @TableId(type = IdType.AUTO)
31 | private Long id;
32 |
33 | /**
34 | * 姓名
35 | */
36 | @TableField(value = "name", condition = SqlCondition.LIKE)
37 | private String name;
38 |
39 | /**
40 | * 年龄
41 | */
42 | @TableField(condition = "%s<#{%s}")
43 | private Integer age;
44 |
45 | /**
46 | * 邮箱
47 | */
48 | private String email;
49 |
50 | /**
51 | * 直属上级id
52 | */
53 | private Long managerId;
54 |
55 | /**
56 | * 创建时间
57 | */
58 | private Date createTime;
59 |
60 | /*
61 | * 备注(不与数据库字段对应) # transient 不参与序列化
62 | */
63 | @TableField(exist = false)
64 | private String remark;
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/mp/src/main/java/com/lord/mp/service/UserService.java:
--------------------------------------------------------------------------------
1 | package com.lord.mp.service;
2 |
3 | import com.baomidou.mybatisplus.extension.service.IService;
4 | import com.lord.mp.entity.User;
5 |
6 | public interface UserService extends IService {
7 |
8 | }
9 |
--------------------------------------------------------------------------------
/mp/src/main/java/com/lord/mp/service/impl/UserServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.lord.mp.service.impl;
2 |
3 | import org.springframework.stereotype.Service;
4 |
5 | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
6 | import com.lord.mp.dao.UserMapper;
7 | import com.lord.mp.entity.User;
8 | import com.lord.mp.service.UserService;
9 |
10 | @Service
11 | public class UserServiceImpl extends ServiceImpl implements UserService {
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/mp/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | datasource:
3 | driver-class-name: com.mysql.cj.jdbc.Driver
4 | url: jdbc:mysql://127.0.0.1:3306/mp?useSSL=false&serverTimezone=GMT%2B8
5 | username: root
6 | password: root
7 |
8 | mybatis-plus:
9 | # config-location: classpath:mybatis-config.xml
10 | mapper-locations: classpath:mapper/**/*Mapper.xml #指定mapper.xml的位置
11 | type-aliases-package: com.lord.mp.entity
12 | global-config:
13 | db-config:
14 | id-type: uuid
15 |
16 | logging:
17 | level:
18 | root: warn
19 | com.lord.mp.dao: trace
20 | pattern:
21 | console: '%p%m%n' #p:级别 m:内容 n:换行
--------------------------------------------------------------------------------
/mp/src/main/resources/mapper/UserMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | id, `name`, age, email, manager_id, create_time
14 |
15 |
16 |
22 |
23 |
29 |
30 |
--------------------------------------------------------------------------------
/mp/src/main/resources/mybatis-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
35 |
36 |
--------------------------------------------------------------------------------
/mp/src/main/resources/sql/数据库脚本.sql:
--------------------------------------------------------------------------------
1 | #创建用户表
2 | CREATE TABLE user (
3 | id BIGINT(20) PRIMARY KEY NOT NULL COMMENT '主键',
4 | name VARCHAR(30) DEFAULT NULL COMMENT '姓名',
5 | age INT(11) DEFAULT NULL COMMENT '年龄',
6 | email VARCHAR(50) DEFAULT NULL COMMENT '邮箱',
7 | manager_id BIGINT(20) DEFAULT NULL COMMENT '直属上级id',
8 | create_time DATETIME DEFAULT NULL COMMENT '创建时间',
9 | CONSTRAINT manager_fk FOREIGN KEY (manager_id)
10 | REFERENCES user (id)
11 | ) ENGINE=INNODB CHARSET=UTF8;
12 |
13 | #初始化数据:
14 | INSERT INTO user (id, name, age, email, manager_id
15 | , create_time)
16 | VALUES (1087982257332887553, '大boss', 40, 'boss@baomidou.com', NULL
17 | , '2019-01-11 14:20:20'),
18 | (1088248166370832385, '王天风', 25, 'wtf@baomidou.com', 1087982257332887553
19 | , '2019-02-05 11:12:22'),
20 | (1088250446457389058, '李艺伟', 28, 'lyw@baomidou.com', 1088248166370832385
21 | , '2019-02-14 08:31:16'),
22 | (1094590409767661570, '张雨琪', 31, 'zjq@baomidou.com', 1088248166370832385
23 | , '2019-01-14 09:15:15'),
24 | (1094592041087729666, '刘红雨', 32, 'lhm@baomidou.com', 1088248166370832385
25 | , '2019-01-14 09:48:16');
26 |
27 | #修改主键为自增:
28 | alter table user change column id id bigint(20) auto_increment;
29 |
--------------------------------------------------------------------------------
/mp/src/test/java/com/lord/mp/MpApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.lord.mp;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class MpApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/mp/src/test/java/com/lord/mp/user/ARTest.java:
--------------------------------------------------------------------------------
1 | package com.lord.mp.user;
2 |
3 | import java.util.Date;
4 |
5 | import org.junit.Test;
6 |
7 | import com.lord.mp.MpApplicationTests;
8 | import com.lord.mp.entity.User;
9 |
10 | public class ARTest extends MpApplicationTests {
11 |
12 | @Test
13 | public void insert() {
14 | User user = new User();
15 | user.setName("丁茂义");
16 | user.setAge(40);
17 | user.setEmail("dmy@baomidou.com");
18 | user.setManagerId(1088248166370832385L);
19 | user.setCreateTime(new Date());
20 | boolean flag = user.insert();
21 | System.out.println("是否成功:" + flag);
22 | }
23 |
24 | @Test
25 | public void selectById1() {
26 | User user = new User();
27 | User userSelect = user.selectById(1134351388596264961L);
28 | System.out.println(userSelect == user);
29 | System.out.println(userSelect);
30 | }
31 |
32 | @Test
33 | public void selectById2() {
34 | User user = new User();
35 | user.setId(1134351388596264961L);
36 | User userSelect = user.selectById();
37 | System.out.println(userSelect == user);
38 | System.out.println(userSelect);
39 | }
40 |
41 | @Test
42 | public void updateById() {
43 | User user = new User();
44 | user.setId(1134351388596264961L);
45 | user.setName("张无忌@@@");
46 | boolean flag = user.updateById();
47 | System.out.println("是否成功:" + flag);
48 | }
49 |
50 | @Test
51 | public void deleteById() {
52 | User user = new User();
53 | user.setId(1134351388596264961L);
54 | boolean flag = user.deleteById();
55 | System.out.println("是否成功:" + flag);
56 | }
57 |
58 | @Test
59 | public void insertOrUpdate() {
60 | User user = new User();
61 | user.setId(1134354221018144770L);
62 | user.setAge(35);
63 | user.setCreateTime(new Date());
64 | boolean flag = user.insertOrUpdate();
65 | System.out.println("是否成功:" + flag);
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/mp/src/test/java/com/lord/mp/user/SimpleTest.java:
--------------------------------------------------------------------------------
1 | package com.lord.mp.user;
2 |
3 | import java.util.Date;
4 | import java.util.List;
5 |
6 | import org.junit.Assert;
7 | import org.junit.Test;
8 | import org.springframework.beans.factory.annotation.Autowired;
9 |
10 | import com.lord.mp.MpApplicationTests;
11 | import com.lord.mp.dao.UserMapper;
12 | import com.lord.mp.entity.User;
13 |
14 | public class SimpleTest extends MpApplicationTests {
15 |
16 | @Autowired
17 | private UserMapper userMapper;
18 |
19 | @Test
20 | public void insert() {
21 | User user = new User();
22 | user.setName("丁茂义");
23 | user.setAge(40);
24 | user.setEmail("dmy@baomidou.com");
25 | user.setManagerId(1088248166370832385L);
26 | user.setCreateTime(new Date());
27 | int rows = userMapper.insert(user);
28 | System.out.println("影响记录数:" + rows);
29 | System.out.println("主键:" + user.getId());
30 | }
31 |
32 | @Test
33 | public void select() {
34 | List userList = userMapper.selectList(null);
35 | Assert.assertEquals(5, userList.size());
36 | userList.forEach(System.out::println);
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/mp/src/test/java/com/lord/mp/user/UserServiceTest.java:
--------------------------------------------------------------------------------
1 | package com.lord.mp.user;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 |
6 | import org.junit.Test;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 |
9 | import com.baomidou.mybatisplus.core.toolkit.Wrappers;
10 | import com.lord.mp.MpApplicationTests;
11 | import com.lord.mp.entity.User;
12 | import com.lord.mp.service.UserService;
13 |
14 | public class UserServiceTest extends MpApplicationTests {
15 |
16 | @Autowired
17 | private UserService userService;
18 |
19 | // 此处会报错,因为数据库存在多条记录
20 | @Test
21 | public void getOne() {
22 | User one = userService.getOne(Wrappers.lambdaQuery().gt(User::getAge, 25));
23 | System.out.println(one);
24 | }
25 |
26 | @Test
27 | public void insertBatch() {
28 | User user1 = new User();
29 | user1.setName("小可爱");
30 | user1.setAge(28);
31 | user1.setManagerId(1088248166370832385L);
32 |
33 | User user2 = new User();
34 | user2.setName("大傻瓜");
35 | user2.setAge(19);
36 | user2.setManagerId(1088248166370832385L);
37 |
38 | List userList = Arrays.asList(user1, user2);
39 | boolean flag = userService.saveBatch(userList);
40 | System.out.println("是否成功:" + flag);
41 | }
42 |
43 | @Test
44 | public void insertOrUpdateBatch() {
45 | User user1 = new User();
46 | user1.setName("李莫愁");
47 | user1.setAge(28);
48 |
49 | User user2 = new User();
50 | user2.setId(1134354221018144774L);
51 | user2.setName("张三丰");
52 | user2.setAge(20);
53 | user2.setManagerId(1088248166370832385L);
54 |
55 | List userList = Arrays.asList(user1, user2);
56 | boolean flag = userService.saveOrUpdateBatch(userList);
57 | System.out.println("是否成功:" + flag);
58 | }
59 |
60 | @Test
61 | public void selectChain() {
62 | List userList = userService.lambdaQuery().gt(User::getAge, 25).like(User::getName, "雨").list();
63 | userList.forEach(System.out::println);
64 | }
65 |
66 | @Test
67 | public void updateChain() {
68 | boolean flag = userService.lambdaUpdate().eq(User::getAge, 25).set(User::getAge, 26).update();
69 | System.out.println("是否成功:" + flag);
70 | }
71 |
72 | @Test
73 | public void removeChain() {
74 | boolean flag = userService.lambdaUpdate().eq(User::getAge, 20).remove();
75 | System.out.println("是否成功:" + flag);
76 | }
77 |
78 | }
79 |
--------------------------------------------------------------------------------
/mp/src/test/java/com/lord/mp/user/UserTest.java:
--------------------------------------------------------------------------------
1 | package com.lord.mp.user;
2 |
3 | import java.util.Arrays;
4 | import java.util.Date;
5 | import java.util.HashMap;
6 | import java.util.List;
7 | import java.util.Map;
8 |
9 | import org.junit.Test;
10 | import org.springframework.beans.factory.annotation.Autowired;
11 |
12 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
13 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
14 | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
15 | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
16 | import com.baomidou.mybatisplus.core.metadata.IPage;
17 | import com.baomidou.mybatisplus.core.toolkit.StringUtils;
18 | import com.baomidou.mybatisplus.core.toolkit.Wrappers;
19 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
20 | import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
21 | import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
22 | import com.lord.mp.MpApplicationTests;
23 | import com.lord.mp.dao.UserMapper;
24 | import com.lord.mp.entity.User;
25 |
26 | public class UserTest extends MpApplicationTests {
27 |
28 | @Autowired
29 | private UserMapper userMapper;
30 |
31 | @Test
32 | public void insert() {
33 | User user = new User();
34 | user.setName("张无忌");
35 | user.setAge(30);
36 | user.setManagerId(1088248166370832385L);
37 | user.setCreateTime(new Date());
38 | int rows = userMapper.insert(user);
39 | System.out.println("影响记录数:" + rows);
40 | }
41 |
42 | // @Test
43 | public void updateById() {
44 | User user = new User();
45 | user.setId(1134013564424658946L);
46 | user.setEmail("zwj@baomidou.com");
47 | int rows = userMapper.updateById(user);
48 | System.out.println("影响记录数:" + rows);
49 | }
50 |
51 | // @Test
52 | public void updateByWrapper1() {
53 | UpdateWrapper userWrapper = Wrappers.update();
54 | userWrapper.eq("name", "李艺伟").eq("age", 28);
55 |
56 | User user = new User();
57 | user.setEmail("lyw2019@baomidou.com");
58 | user.setAge(29);
59 |
60 | int rows = userMapper.update(user, userWrapper);
61 | System.out.println("影响记录数:" + rows);
62 | }
63 |
64 | // 构造器参数和实体对象参数会重复出现
65 | // @Test
66 | public void updateByWrapper2() {
67 | User userWhere = new User();
68 | userWhere.setName("李艺伟");
69 |
70 | UpdateWrapper userWrapper = Wrappers.update(userWhere);
71 | userWrapper.eq("name", "李艺伟").eq("age", 28);
72 |
73 | User user = new User();
74 | user.setAge(29);
75 |
76 | int rows = userMapper.update(user, userWrapper);
77 | System.out.println("影响记录数:" + rows);
78 | }
79 |
80 | // @Test
81 | public void updateByWrapper3() {
82 | UpdateWrapper userWrapper = Wrappers.update();
83 | userWrapper.eq("name", "李艺伟").eq("age", 29).set("age", 30);
84 |
85 | User user = new User();
86 | user.setAge(29);
87 |
88 | int rows = userMapper.update(null, userWrapper);
89 | System.out.println("影响记录数:" + rows);
90 | }
91 |
92 | // @Test
93 | public void updateByWrapperLambda() {
94 | LambdaUpdateWrapper lambdaUpdate = Wrappers.lambdaUpdate();
95 | lambdaUpdate.eq(User::getName, "李艺伟").eq(User::getAge, 30).set(User::getAge, 31);
96 |
97 | int rows = userMapper.update(null, lambdaUpdate);
98 | System.out.println("影响记录数:" + rows);
99 | }
100 |
101 | // @Test
102 | public void updateByWrapperLambdaChain() {
103 | boolean flag = new LambdaUpdateChainWrapper(userMapper).eq(User::getName, "李艺伟").eq(User::getAge, 31)
104 | .set(User::getAge, 32).update();
105 |
106 | System.out.println("是否成功:" + flag);
107 | }
108 |
109 | // @Test
110 | public void deleteById() {
111 | int rows = userMapper.deleteById(1134013564424658946L);
112 | System.out.println("删除条数:" + rows);
113 | }
114 |
115 | // @Test
116 | public void deleteByMap() {
117 | Map columnMap = new HashMap<>();
118 | columnMap.put("name", "张无忌1");
119 | columnMap.put("age", 31);
120 |
121 | int rows = userMapper.deleteByMap(columnMap);
122 | System.out.println("删除条数:" + rows);
123 | }
124 |
125 | // @Test
126 | public void deleteBatchByIds() {
127 | int rows = userMapper.deleteBatchIds(Arrays.asList(1134345992649379841L, 1134346040569274369L));
128 | System.out.println("删除条数:" + rows);
129 | }
130 |
131 | @Test
132 | public void deleteByWrapper() {
133 | LambdaQueryWrapper lambdaQuery = Wrappers.lambdaQuery();
134 | lambdaQuery.eq(User::getAge, 27).or().gt(User::getAge, 41);
135 | int rows = userMapper.delete(lambdaQuery);
136 | System.out.println("删除条数:" + rows);
137 | }
138 |
139 | // @Test
140 | public void selectById() {
141 | long id = 1094590409767661570L;
142 | User user = userMapper.selectById(id);
143 | System.out.println(user);
144 | }
145 |
146 | // @Test
147 | public void selectByIds() {
148 | List idList = Arrays.asList(1088248166370832385L, 1088250446457389058L, 1094590409767661570L);
149 | List userList = userMapper.selectBatchIds(idList);
150 | userList.forEach(System.out::println);
151 | }
152 |
153 | // @Test
154 | public void selectByMap() {
155 | Map paramMap = new HashMap<>();
156 | paramMap.put("name", "张无忌");
157 | paramMap.put("age", 31);
158 | List userList = userMapper.selectByMap(paramMap);
159 | userList.forEach(System.out::println);
160 | }
161 |
162 | /**
163 | * 姓名中含雨并且年龄小于40
164 | * name like '%雨%' and age < 40
165 | */
166 | // @Test
167 | public void selectByWarpper1() {
168 | // 条件构造器
169 | QueryWrapper userQuery = Wrappers.query();
170 | userQuery.like("name", "雨").lt("age", 40);
171 |
172 | List userList = userMapper.selectList(userQuery);
173 | userList.forEach(System.out::println);
174 | }
175 |
176 | /**
177 | * 姓名中含雨并且年龄大于等于20且小于等于40并且email不为空
178 | * name like '%雨%' and age between 20 and 40 and email is not null
179 | */
180 | // @Test
181 | public void selectByWarpper2() {
182 | // 条件构造器
183 | QueryWrapper userQuery = Wrappers.query();
184 | userQuery.like("name", "雨").between("age", 20, 40).isNotNull("email");
185 |
186 | List userList = userMapper.selectList(userQuery);
187 | userList.forEach(System.out::println);
188 | }
189 |
190 | /**
191 | * 姓名为王姓或者年龄大于等于25,按照年龄降序排列,年龄相同按照id升序排列
192 | * name like '王%' or age >= 25 order by age desc,id asc
193 | */
194 | @Test
195 | public void selectByWarpper3() {
196 | // 条件构造器
197 | QueryWrapper userQuery = Wrappers.query();
198 | userQuery.likeRight("name", "王").or().ge("age", 25).orderByDesc("age").orderByAsc("id");
199 |
200 | List userList = userMapper.selectList(userQuery);
201 | userList.forEach(System.out::println);
202 | }
203 |
204 | /**
205 | * 创建时间为2019年2月14日并且直属上级名字为王姓
206 | * date_format(create_time,'%Y-%m-%d') and manager_id in (select id from user where name like '王%')
207 | */
208 | // @Test
209 | public void selectByWarpper4() {
210 | // 条件构造器
211 | QueryWrapper userQuery = Wrappers.query();
212 | // 此处写法存在sql注入问题,不建议使用
213 | // userQuery.apply("date_format(create_time,'%Y-%m-%d')='2019-02-14' or true or true").inSql("manager_id",
214 | // "select id from user where name like '王%'");
215 |
216 | userQuery.apply("date_format(create_time,'%Y-%m-%d')={0}", "2019-02-14").inSql("manager_id",
217 | "select id from user where name like '王%'");
218 |
219 | List userList = userMapper.selectList(userQuery);
220 | userList.forEach(System.out::println);
221 | }
222 |
223 | /**
224 | * 名字为王姓并且(年龄小于40或邮箱不为空)
225 | * name like '王%' and (age < 40 or email is not null)
226 | */
227 | // @Test
228 | public void selectByWarpper5() {
229 | // 条件构造器
230 | QueryWrapper userQuery = Wrappers.query();
231 | userQuery.likeRight("name", "王").and(wq -> wq.lt("age", 40).or().isNotNull("email"));
232 |
233 | List userList = userMapper.selectList(userQuery);
234 | userList.forEach(System.out::println);
235 | }
236 |
237 | /**
238 | * 名字为王姓或者(年龄小于40并且大于20并且邮箱不为空)
239 | * name like '王%' or (age < 40 and age > 20 and email is not null)
240 | */
241 | // @Test
242 | public void selectByWarpper6() {
243 | // 条件构造器
244 | QueryWrapper userQuery = Wrappers.query();
245 | userQuery.likeRight("name", "王").or(wq -> wq.lt("age", 40).gt("age", 20).isNotNull("email"));
246 |
247 | List userList = userMapper.selectList(userQuery);
248 | userList.forEach(System.out::println);
249 | }
250 |
251 | /**
252 | * (年龄小于40或邮箱不为空)并且名字为王姓
253 | * (age < 40 or email is not null) and name like '王%'
254 | */
255 | // @Test
256 | public void selectByWarpper7() {
257 | // 条件构造器
258 | QueryWrapper userQuery = Wrappers.query();
259 | userQuery.nested(wq -> wq.lt("age", 40).or().isNotNull("email")).likeRight("name", "王");
260 |
261 | List userList = userMapper.selectList(userQuery);
262 | userList.forEach(System.out::println);
263 | }
264 |
265 | /**
266 | * 年龄为30,31,34,35
267 | * age in (30,31,34,35)
268 | */
269 | // @Test
270 | public void selectByWarpper8() {
271 | // 条件构造器
272 | QueryWrapper userQuery = Wrappers.query();
273 | userQuery.in("age", Arrays.asList(30, 31, 34, 35));
274 |
275 | List userList = userMapper.selectList(userQuery);
276 | userList.forEach(System.out::println);
277 | }
278 |
279 | /**
280 | * 只返回满足条件的其中一条语句即可
281 | * limit 1
282 | */
283 | // @Test
284 | public void selectByWarpper9() {
285 | // 条件构造器
286 | QueryWrapper userQuery = Wrappers.query();
287 | userQuery.in("age", Arrays.asList(30, 31, 34, 35)).last("limit 1");
288 |
289 | List userList = userMapper.selectList(userQuery);
290 | userList.forEach(System.out::println);
291 | }
292 |
293 | /**
294 | * 姓名中含雨并且年龄小于40(只返回id和name两列)
295 | * name like '%雨%' and age < 40
296 | */
297 | // @Test
298 | public void selectByWarpper10_1() {
299 | // 条件构造器
300 | QueryWrapper userQuery = Wrappers.query();
301 | userQuery.select("id", "name").like("name", "雨").lt("age", 40);
302 |
303 | List userList = userMapper.selectList(userQuery);
304 | userList.forEach(System.out::println);
305 | }
306 |
307 | /**
308 | * 姓名中含雨并且年龄小于40(只返回id、name、age、email列)
309 | * name like '%雨%' and age < 40
310 | */
311 | // @Test
312 | public void selectByWarpper10_2() {
313 | // 条件构造器
314 | QueryWrapper userQuery = Wrappers.query();
315 | userQuery.like("name", "雨").lt("age", 40).select(User.class,
316 | info -> !info.getColumn().equals("create_time") && !info.getColumn().equals("manager_id"));
317 |
318 | List userList = userMapper.selectList(userQuery);
319 | userList.forEach(System.out::println);
320 | }
321 |
322 | /**
323 | * 按照直属上级分组,查询每组的平均年龄、最大年龄、最小年龄。并且只取年龄总和小于500的组
324 | * select avg(age) avg_age, max(age) max_age, min(age) min_age from user
325 | * group by manager_id having sum(age) < 500
326 | */
327 | // @Test
328 | public void selectByWarpper11() {
329 | // 条件构造器
330 | QueryWrapper userQuery = Wrappers.query();
331 | userQuery.select("avg(age) avg_age", "max(age) max_age", "min(age) min_age").groupBy("manager_id")
332 | .having("sum(age) < {0}", 500);
333 |
334 | List