mirror of
https://github.com/ityouknow/spring-boot-examples.git
synced 2026-06-01 02:19:17 +08:00
MyBatis Plus demo
This commit is contained in:
@@ -33,6 +33,7 @@ Spring Boot 使用的各种示例,以最简单、最实用为标准,此开
|
||||
- [spring-boot-webflux](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-webflux) :Spring Boot webflux 示例
|
||||
- [spring-boot-elasticsearch](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-elasticsearch) :Spring Boot elasticsearch 示例
|
||||
- [spring-boot-swagger](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-swagger) :Spring Boot swagger2 示例
|
||||
- [spring-boot-mybatis-plus](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-mybatis-plus) :Spring Boot 集成 MyBatis Plus 示例
|
||||
|
||||
**参考文章**
|
||||
|
||||
|
||||
@@ -23,6 +23,9 @@ Spring Boot examples, using the simplest and the most useful scene demos.
|
||||
- [spring-boot-webflux](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-webflux) :Spring Boot webflux demo
|
||||
- [spring-boot-elasticsearch](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-elasticsearch) :Spring Boot elasticsearch demo
|
||||
- [spring-boot-swagger](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-swagger) :Spring Boot swagger2 demo
|
||||
- [spring-boot-mybatis-plus](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-mybatis-plus) :Spring Boot MyBatis Plus demo
|
||||
|
||||
|
||||
---
|
||||
|
||||
## Spring Boot (Already upgraded to 2.x)
|
||||
|
||||
58
spring-boot-mybatis-plus/pom.xml
Normal file
58
spring-boot-mybatis-plus/pom.xml
Normal file
@@ -0,0 +1,58 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>spring-boot-mybatis-plus</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Spring Boot MyBatis Plus</name>
|
||||
<description>Spring Boot 2 Demo</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.1.4.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<version>3.1.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.neo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class MyBatisPlusApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(MyBatisPlusApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.neo.config;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@MapperScan("com.neo.mapper")
|
||||
public class MybatisPlusConfig {
|
||||
|
||||
/**
|
||||
* 分页插件
|
||||
*/
|
||||
@Bean
|
||||
public PaginationInterceptor paginationInterceptor() {
|
||||
return new PaginationInterceptor();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.neo.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.neo.model.User;
|
||||
|
||||
public interface UserMapper extends BaseMapper<User> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.neo.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class User {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Integer age;
|
||||
private String email;
|
||||
}
|
||||
14
spring-boot-mybatis-plus/src/main/resources/application.yml
Normal file
14
spring-boot-mybatis-plus/src/main/resources/application.yml
Normal file
@@ -0,0 +1,14 @@
|
||||
# DataSource Config
|
||||
spring:
|
||||
datasource:
|
||||
driver-class-name: org.h2.Driver
|
||||
schema: classpath:db/schema-h2.sql
|
||||
data: classpath:db/data-h2.sql
|
||||
url: jdbc:h2:mem:test
|
||||
username: root
|
||||
password: test
|
||||
|
||||
# Logger Config
|
||||
logging:
|
||||
level:
|
||||
com.neo: debug
|
||||
@@ -0,0 +1,8 @@
|
||||
DELETE FROM user;
|
||||
|
||||
INSERT INTO user (id, name, age, email) VALUES
|
||||
(1, 'neo', 18, 'smile1@ityouknow.com'),
|
||||
(2, 'keep', 36, 'smile@ityouknow.com'),
|
||||
(3, 'pure', 28, 'smile@ityouknow.com'),
|
||||
(4, 'smile', 21, 'smile@ityouknow.com'),
|
||||
(5, 'it', 24, 'smile@ityouknow.com');
|
||||
10
spring-boot-mybatis-plus/src/main/resources/db/schema-h2.sql
Normal file
10
spring-boot-mybatis-plus/src/main/resources/db/schema-h2.sql
Normal file
@@ -0,0 +1,10 @@
|
||||
DROP TABLE IF EXISTS user;
|
||||
|
||||
CREATE TABLE user
|
||||
(
|
||||
id BIGINT(20) NOT NULL COMMENT '主键ID',
|
||||
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
|
||||
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
|
||||
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
|
||||
PRIMARY KEY (id)
|
||||
);
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.neo;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class MyBatisPlusApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package com.neo;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.neo.mapper.UserMapper;
|
||||
import com.neo.model.User;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class MyBatisPlusTest {
|
||||
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
|
||||
@Test
|
||||
public void testSelectOne() {
|
||||
User user = userMapper.selectById(1L);
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsert() {
|
||||
User user = new User();
|
||||
user.setName("微笑");
|
||||
user.setAge(3);
|
||||
user.setEmail("neo@tooool.org");
|
||||
assertThat(userMapper.insert(user)).isGreaterThan(0);
|
||||
// 成功直接拿会写的 ID
|
||||
assertThat(user.getId()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelete() {
|
||||
assertThat(userMapper.deleteById(3L)).isGreaterThan(0);
|
||||
assertThat(userMapper.delete(new QueryWrapper<User>()
|
||||
.lambda().eq(User::getName, "smile"))).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdate() {
|
||||
User user = userMapper.selectById(2);
|
||||
assertThat(user.getAge()).isEqualTo(36);
|
||||
assertThat(user.getName()).isEqualTo("keep");
|
||||
|
||||
userMapper.update(
|
||||
null,
|
||||
Wrappers.<User>lambdaUpdate().set(User::getEmail, "123@123").eq(User::getId, 2)
|
||||
);
|
||||
assertThat(userMapper.selectById(2).getEmail()).isEqualTo("123@123");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelect() {
|
||||
List<User> userList = userMapper.selectList(null);
|
||||
Assert.assertEquals(5, userList.size());
|
||||
userList.forEach(System.out::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectCondition() {
|
||||
QueryWrapper<User> wrapper = new QueryWrapper<>();
|
||||
wrapper.select("max(id) as id");
|
||||
List<User> userList = userMapper.selectList(wrapper);
|
||||
userList.forEach(System.out::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPage() {
|
||||
System.out.println("----- baseMapper 自带分页 ------");
|
||||
Page<User> page = new Page<>(1, 2);
|
||||
IPage<User> userIPage = userMapper.selectPage(page, new QueryWrapper<User>()
|
||||
.gt("age", 6));
|
||||
assertThat(page).isSameAs(userIPage);
|
||||
System.out.println("总条数 ------> " + userIPage.getTotal());
|
||||
System.out.println("当前页数 ------> " + userIPage.getCurrent());
|
||||
System.out.println("当前每页显示数 ------> " + userIPage.getSize());
|
||||
print(userIPage.getRecords());
|
||||
System.out.println("----- baseMapper 自带分页 ------");
|
||||
}
|
||||
|
||||
private <T> void print(List<T> list) {
|
||||
if (!CollectionUtils.isEmpty(list)) {
|
||||
list.forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user