|
1 |
| -# Quick Start |
| 1 | +# Quick Start |
| 2 | + |
| 3 | +Let's have a simple Demo to show how to use MP. But before that, we suppose you: |
| 4 | + |
| 5 | +- Have environment and IDE for Java development |
| 6 | +- Are familiar with Spring Boot |
| 7 | +- Are familiar with Maven |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +Here is a table named `User` : |
| 12 | + |
| 13 | +| id | name | age | email | |
| 14 | +| --- | ------ | --- | ------------------ | |
| 15 | +| 1 | Jone | 18 | test1@baomidou.com | |
| 16 | +| 2 | Jack | 20 | test2@baomidou.com | |
| 17 | +| 3 | Tom | 28 | test3@baomidou.com | |
| 18 | +| 4 | Sandy | 21 | test4@baomidou.com | |
| 19 | +| 5 | Billie | 24 | test5@baomidou.com | |
| 20 | + |
| 21 | +Schema script as below: |
| 22 | + |
| 23 | +```sql |
| 24 | +DROP TABLE IF EXISTS user; |
| 25 | + |
| 26 | +CREATE TABLE user |
| 27 | +( |
| 28 | + id BIGINT(20) NOT NULL COMMENT 'Primary key', |
| 29 | + name VARCHAR(30) NULL DEFAULT NULL COMMENT 'name', |
| 30 | + age INT(11) NULL DEFAULT NULL COMMENT 'age', |
| 31 | + email VARCHAR(50) NULL DEFAULT NULL COMMENT 'email', |
| 32 | + PRIMARY KEY (id) |
| 33 | +); |
| 34 | +``` |
| 35 | + |
| 36 | +Data script as below: |
| 37 | + |
| 38 | +```sql |
| 39 | +DELETE FROM user; |
| 40 | + |
| 41 | +INSERT INTO user (id, name, age, email) VALUES |
| 42 | +(1, 'Jone', 18, 'test1@baomidou.com'), |
| 43 | +(2, 'Jack', 20, 'test2@baomidou.com'), |
| 44 | +(3, 'Tom', 28, 'test3@baomidou.com'), |
| 45 | +(4, 'Sandy', 21, 'test4@baomidou.com'), |
| 46 | +(5, 'Billie', 24, 'test5@baomidou.com'); |
| 47 | +``` |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +::: danger Question |
| 52 | +What should we do for CRUD by MP? |
| 53 | +::: |
| 54 | + |
| 55 | +## Initialization |
| 56 | + |
| 57 | +Create a project of Spring Boot(will use [H2 Database](http://www.h2database.com) for showcase by default) |
| 58 | + |
| 59 | +::: tip |
| 60 | +You can use [Spring Initializer](https://start.spring.io/) for Spring Boot Quick Start |
| 61 | +::: |
| 62 | + |
| 63 | +## Dependency |
| 64 | + |
| 65 | +Import Spring Boot Starter as parent: |
| 66 | +```xml |
| 67 | +<parent> |
| 68 | + <groupId>org.springframework.boot</groupId> |
| 69 | + <artifactId>spring-boot-starter-parent</artifactId> |
| 70 | + <version>spring-latest-version</version> |
| 71 | + <relativePath/> |
| 72 | +</parent> |
| 73 | +``` |
| 74 | + |
| 75 | +Import `spring-boot-starter`、`spring-boot-starter-test`、`mybatis-plus-boot-starter`、`lombok`、`h2` : |
| 76 | +```xml {18} |
| 77 | +<dependencies> |
| 78 | + <dependency> |
| 79 | + <groupId>org.springframework.boot</groupId> |
| 80 | + <artifactId>spring-boot-starter</artifactId> |
| 81 | + </dependency> |
| 82 | + <dependency> |
| 83 | + <groupId>org.springframework.boot</groupId> |
| 84 | + <artifactId>spring-boot-starter-test</artifactId> |
| 85 | + <scope>test</scope> |
| 86 | + </dependency> |
| 87 | + <dependency> |
| 88 | + <groupId>org.projectlombok</groupId> |
| 89 | + <artifactId>lombok</artifactId> |
| 90 | + <optional>true</optional> |
| 91 | + </dependency> |
| 92 | + <dependency> |
| 93 | + <groupId>com.baomidou</groupId> |
| 94 | + <artifactId>mybatis-plus-boot-starter</artifactId> |
| 95 | + <version>starter-latest-version</version> |
| 96 | + </dependency> |
| 97 | + <dependency> |
| 98 | + <groupId>com.h2database</groupId> |
| 99 | + <artifactId>h2</artifactId> |
| 100 | + <scope>runtime</scope> |
| 101 | + </dependency> |
| 102 | +</dependencies> |
| 103 | +``` |
| 104 | + |
| 105 | +## Configuration |
| 106 | + |
| 107 | +Define H2 Database configuration in `application.yml` : |
| 108 | + |
| 109 | +```yaml |
| 110 | +# DataSource Config |
| 111 | +spring: |
| 112 | + datasource: |
| 113 | + driver-class-name: org.h2.Driver |
| 114 | + schema: classpath:db/schema-h2.sql |
| 115 | + data: classpath:db/data-h2.sql |
| 116 | + url: jdbc:h2:mem:test |
| 117 | + username: root |
| 118 | + password: test |
| 119 | +``` |
| 120 | +
|
| 121 | +Add `@MapperScan` Annotation in Spring boot application class, configure Mapper's package: |
| 122 | +```java {2} |
| 123 | +@SpringBootApplication |
| 124 | +@MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper") |
| 125 | +public class QuickStartApplication { |
| 126 | +
|
| 127 | + public static void main(String[] args) { |
| 128 | + SpringApplication.run(QuickStartApplication.class, args); |
| 129 | + } |
| 130 | +
|
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +## Code |
| 135 | + |
| 136 | +Write `User.java` as an entity(Here using [Lombok](https://www.projectlombok.org/) to simplify getter/setter methods) |
| 137 | + |
| 138 | +```java |
| 139 | +@Data |
| 140 | +public class User { |
| 141 | + private Long id; |
| 142 | + private String name; |
| 143 | + private Integer age; |
| 144 | + private String email; |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +Write a Mapper `UserMapper.java` |
| 149 | + |
| 150 | +```java |
| 151 | +public interface UserMapper extends BaseMapper<User> { |
| 152 | +
|
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +## Let's begin |
| 157 | + |
| 158 | +Add a test case: |
| 159 | + |
| 160 | +```java |
| 161 | +@RunWith(SpringRunner.class) |
| 162 | +@SpringBootTest |
| 163 | +public class SampleTest { |
| 164 | +
|
| 165 | + @Autowired |
| 166 | + private UserMapper userMapper; |
| 167 | +
|
| 168 | + @Test |
| 169 | + public void testSelect() { |
| 170 | + System.out.println(("----- selectAll method test ------")); |
| 171 | + List<User> userList = userMapper.selectList(null); |
| 172 | + Assert.assertEquals(5, userList.size()); |
| 173 | + userList.forEach(System.out::println); |
| 174 | + } |
| 175 | +
|
| 176 | +} |
| 177 | +``` |
| 178 | + |
| 179 | +::: tip |
| 180 | +The parameter of `selectList()` is a condition constructor built-in MP, null means no condition(fetch all records) |
| 181 | +::: |
| 182 | + |
| 183 | +Console log: |
| 184 | + |
| 185 | +``` |
| 186 | +User(id=1, name=Jone, age=18, email=test1@baomidou.com) |
| 187 | +User(id=2, name=Jack, age=20, email=test2@baomidou.com) |
| 188 | +User(id=3, name=Tom, age=28, email=test3@baomidou.com) |
| 189 | +User(id=4, name=Sandy, age=21, email=test4@baomidou.com) |
| 190 | +User(id=5, name=Billie, age=24, email=test5@baomidou.com) |
| 191 | +``` |
| 192 | +
|
| 193 | +::: tip |
| 194 | +For Complete codes, please refer to: [Spring Boot Quick Start Sample](https://github.com/baomidou/mybatis-plus-samples/tree/master/mybatis-plus-sample-quickstart) |
| 195 | +::: |
| 196 | +
|
| 197 | +## Summary |
| 198 | +
|
| 199 | +We have achieved CURD for table `User` by the few steps above, even no XML file! |
| 200 | +
|
| 201 | +It's very simple to integrate `Mybatis-Plus`, just import `starter` and configure mapper's package. |
| 202 | +
|
| 203 | +But the powerful functions are far more than these. Keep going! |
| 204 | +
|
| 205 | +<script> |
| 206 | +export default { |
| 207 | + mounted () { |
| 208 | + var xmlHttp = new XMLHttpRequest() |
| 209 | + xmlHttp.open("GET", "https://img.shields.io/maven-central/v/com.baomidou/mybatis-plus-boot-starter.json", false) |
| 210 | + xmlHttp.send(null) |
| 211 | + var starterVersionInfo = JSON.parse(xmlHttp.responseText).value.replace('v', '') |
| 212 | + xmlHttp.open("GET", "https://img.shields.io/maven-central/v/org.springframework.boot/spring-boot-starter-parent.json", false) |
| 213 | + xmlHttp.send(null) |
| 214 | + var springVersionInfo = JSON.parse(xmlHttp.responseText).value.replace('v', '') |
| 215 | + var codeNodeList = document.querySelectorAll('code') |
| 216 | + for (var i = 0; i < codeNodeList.length; i++) { |
| 217 | + codeNodeList[i].innerHTML = codeNodeList[i].innerHTML.replace('starter-latest-version', starterVersionInfo) |
| 218 | + codeNodeList[i].innerHTML = codeNodeList[i].innerHTML.replace('spring-latest-version', springVersionInfo) |
| 219 | + } |
| 220 | + } |
| 221 | +} |
| 222 | +</script> |
0 commit comments