引入依赖
在 pom.xml 文件中,加入 mybatis-plus-boot-starter 的依赖。mybatis-plus-boot-starter 是一个“全家桶”,它会自动帮你引入 Mybatis-Plus 核心库、MyBatis 核心库以及与 Spring Boot 集成的所有必要组件。
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3</version> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
|
配置数据源
在 src/main/resources/application.yaml 中,配置数据库的连接信息。
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/my_database?serverTimezone=UTC username: root password: your_password
|
创建绑定
使用 @TableName 创建实体类 Entity 和数据库表的绑定;使用 @TableId、@TableField 来创建属性和字段的绑定。
@Data @TableName("tb_user") public class User { @TableId(value = "id", type = IdType.AUTO) private Long id;
private String name; private Integer age; private String email; }
|
让 Mapper 接口继承 BaseMapper<T>。通过继承 BaseMapper<User>,UserMapper 接口就拥有了一整套强大的、无需编写 SQL 的 CRUD 方法,例如 insert, selectById, updateById, delete, selectList 等。
public interface UserMapper extends BaseMapper<User> { }
|
UserMapper 还是一个接口,没有实现类,所以最后还需在 Spring Boot 主启动类上,添加 @MapperScan 注解。@MapperScan 注解会告诉 Spring Boot 在启动时去扫描指定的包,为所有继承了 BaseMapper 的接口动态地创建一个代理实现类,并注册到 Spring 容器中。这样,你才能在其他地方通过 @Autowired 或 @Resource 注入 UserMapper。
@SpringBootApplication @MapperScan("com.example.project.mapper") public class ProjectApplication { public static void main(String[] args) { SpringApplication.run(ProjectApplication.class, args); } }
|
进阶:Service 层
让 Service 层的接口继承 IService<T>接口,Service 的实现类继承 ServiceImpl<M, T>。
public interface IUserService extends IService<User> { }
@Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService { }
|
进阶:插件
创建 Mybatis-Plus 的配置类,用来添加分页插件、乐观锁插件等。
@Configuration public class MybatisPlusConfig {
@Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor; } }
|