以及配置
时间: 2023-07-04 15:24:16 浏览: 78
在添加了mybatis-plus-boot-starter依赖后,我们还需要进行一些配置,以使mybatis-plus能够在项目中正常工作。具体配置如下:
1. 配置数据源
在application.yml或者application.properties中配置数据源信息,例如:
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=utf-8
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
```
2. 配置mybatis-plus
在application.yml或者application.properties中添加以下mybatis-plus的配置:
```yaml
mybatis-plus:
mapper-locations: classpath*:/mapper/*.xml
#实体扫描,多个package用逗号或者分号分隔
typeAliasesPackage: com.example.entity
global-config:
db-config:
id-type: auto
field-strategy: not_empty
```
其中,mapper-locations表示mapper文件的位置,typeAliasesPackage表示实体类所在的包名,global-config表示全局配置,db-config表示数据库相关配置,id-type表示主键生成策略,field-strategy表示非空字段策略。
3. 创建Mapper接口
创建Mapper接口时,可以继承mybatis-plus提供的BaseMapper接口,例如:
```java
@Repository
public interface UserMapper extends BaseMapper<User> {
}
```
其中,User是实体类的名称。
至此,我们就完成了mybatis-plus的配置。如果需要使用mybatis-plus提供的增删改查功能,只需要在Service层中调用相关方法即可。
阅读全文
相关推荐









