mybatisplus配置过程
时间: 2023-09-07 13:13:12 浏览: 79
mybatis 配置
1. 引入mybatisplus依赖
```xml
<!-- mybatisplus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
```
2. 配置数据源
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&maxReconnects=10
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
```
3. 配置mybatisplus
```java
@Configuration
@MapperScan("com.example.mapper")
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
```
4. 编写mapper
```java
@Mapper
public interface UserMapper extends BaseMapper<User> {
List<User> selectAll();
}
```
5. 使用mybatisplus提供的API进行操作
```java
@RunWith(SpringRunner.class)
@SpringBootTest
public class MybatisPlusTest {
@Autowired
private UserMapper userMapper;
@Test
public void select() {
List<User> userList = userMapper.selectAll();
System.out.println(userList);
}
@Test
public void insert() {
User user = new User();
user.setUsername("test");
user.setPassword("123456");
userMapper.insert(user);
}
}
```
阅读全文