spring mybatis-plus
pom.xml
1 | <dependency> |
- properties
1
2
3
4
5
6# DataSource Config
MYSQL_HOST=192.168.50.28
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:13306/summer?allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=summer
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver - Application add
@MapperScan({"com.example.summermetrics.mapper"})
- User.java
1
2
3
4
5
6
7
8
public class User {
private Long id;
private String name;
private Integer age;
private String email;
} - UerMapper.java
1
2
3
4
5
6
7
8
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
unit test
1 | <dependency> |
目录结构
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15.
├── java
│ └── com
│ └── example
│ └── summermetrics
│ ├── SummerMetricsApplicationTests.java
│ └── mapper
│ └── MybatisPlusSampleTest.java
└── resources
├── application-test.properties
├── db
│ ├── data-h2.sql
│ └── schema-h2.sql
└── log4j2-test.xmlapplication-test.properties
1
2
3
4
5
6
7
8
9logging.config=classpath:log4j2-test.xml
# DataSource Config
spring.datasource.url=jdbc:h2:mem:test
spring.datasource.username=root
spring.datasource.password=test
spring.sql.init.schema-locations=classpath:db/schema-h2.sql
spring.sql.init.data-locations=classpath:db/data-h2.sql
spring.datasource.driver-class-name=org.h2.DriverMybatisPlusSampleTest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48package com.example.summermetrics.mapper;
import java.util.List;
import org.junit.FixMethodOrder;
import org.junit.internal.MethodSorter;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.runner.OrderWith;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import com.baomidou.mybatisplus.test.autoconfigure.MybatisPlusTest;
import com.example.summermetrics.domain.User;
class MybatisPlusSampleTest {
private UserMapper userMapper;
void testInsert() {
User user = User.builder().id(1111L).name("test").age(3).email("email").build();
userMapper.insert(user);
User user1 = userMapper.selectById(1111L);
assert user1.getName().equals("test");
}
void testGetUser() {
List<User> users = userMapper.selectList(null);
assert users.size() == 5;
}
}参考文章