MybatisPlus配置

一、引入相关依赖

 <dependency>
    <groupId>com.baomidou</groupId>
     <artifactId>mybatis-plus-boot-starter</artifactId>
     <version>3.1.0</version>
</dependency>

二、编写配置文件

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&characterEncoding=UTF-8&allowMultiQueries=true
    username: root
    password: root
 
mybatis-plus:
  mapper-locations: classpath:mappers/*.xml

可能需要自己编写sql语句,因此需要配置mapper-locations去找到相应的xml文件

三、编写实体类

四、编写mapper

  1. 多项目下,可以在配置类上使用@MapperScan包扫描多包路径
  2. 不使用@MapperScan,就必须使用@Mapper并且类路径在Springboot路径下能找到,才可以正常使用。

五、编写service

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1. 引入依赖 在 pom.xml 文件中添加以下依赖: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency> ``` 2. 配置数据源 在 application.properties 文件中配置数据源信息,例如: ``` spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 3. 配置 MybatisPlus 在配置类中添加以下注解: ``` @Configuration @MapperScan("com.example.demo.mapper") public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } } ``` 其中,@MapperScan 注解用于扫描 Mapper 接口所在的包路径。 4. 编写 Mapper 接口和实体类 编写 Mapper 接口和实体类,例如: ``` public interface UserMapper extends BaseMapper<User> { } ``` ``` @Data public class User { private Long id; private String name; private Integer age; private String email; } ``` 5. 使用 MybatisPlus 在 Service 层中使用 MybatisPlus 提供的方法,例如: ``` @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { } ``` 其中,ServiceImpl 是 MybatisPlus 提供的 Service 实现类,可以直接继承使用。 以上就是使用 Spring Boot 配置 MybatisPlus 的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值