springboot如何配置mybatisplus
时间: 2023-11-27 15:43:50 浏览: 36
配置MybatisPlus需要以下步骤:
1. 在pom.xml中添加MybatisPlus的依赖:
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.1</version>
</dependency>
```
2. 在application.yml中配置数据源和MybatisPlus:
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
mapper-locations: classpath:/mapper/*.xml
configuration:
map-underscore-to-camel-case: true
```
3. 创建Mapper接口并继承BaseMapper:
```java
public interface UserMapper extends BaseMapper<User> {
}
```
4. 在Service中注入Mapper并使用MybatisPlus提供的方法进行数据库操作:
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> list() {
return userMapper.selectList(null);
}
}
```