Spring Boot整合MyBatis-Plus全攻略
Spring Boot整合MyBatis-Plus
- 添加依赖
- 配置数据源
- 实体类映射
- Mapper接口
- 扫描Mapper接口
- 分页插件配置
- 使用示例
- 常用注解说明
- 注意事项
以下是Spring Boot整合MyBatis-Plus的详细步骤和配置说明:
添加依赖
在pom.xml
中引入核心依赖(需根据Spring Boot版本调整MyBatis-Plus版本):
<!--mybatis_plus--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.1</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.16</version></dependency><!--mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.33</version></dependency><dependency>
配置数据源
在application.yml
中配置数据库连接和MyBatis-Plus基础参数:
spring:datasource:url: jdbc:mysql://localhost:3306/db_name?serverTimezone=GMTusername: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Drivermybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 开启SQL日志
实体类映射
使用注解定义实体类与表的映射关系:
@TableName("student")//设置数据库表名(类名与数据库表面一致时可以省略)
public class Student {@TableId(type = IdType.AUTO) //自动主键回填也可配置在全局中private int id;
// @TableField(value = "name")//设置数据库表字段名称(一致时可以省略)private String name;
// @TableField(value = "sex")private int sex;
// @TableField(value = "cardId")private int cardid;@TableLogic(value="0",delval="1")
// @TableField(exist = false)@TableField(value = "isdelete")//value为正常数据的值,delval为删除数据的值private Integer deleted;@Versionprivate int version;
Mapper接口
继承BaseMapper
获得基础CRUD方法:
@Mapper
//BaseMapper是一个通用的 Mapper 接口 提供数据库操作
//Student是一个泛型参数 代表这个 Mapper 接口操作的实体类是Student类
public interface StudentMapper extends BaseMapper<Student> {}
扫描Mapper接口
在启动类添加@MapperScan
注解:
@SpringBootApplication
//该注解和接口上的@Mapper二者任选其一
@MapperScan("com.ape") // 指定Mapper接口所在包
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
分页插件配置
新增配置类启用分页功能:
@Configuration
public class MyBatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor());return interceptor;}
}
使用示例
在测试类中直接调用MyBatis-Plus提供的方法:
@SpringBootTest
class SpringbootMybatis02ApplicationTests {@Autowired(required = false)//不想看到报红 可以设置未找到就不装配
// @Autowired
//由于程序还未启动 没加载StudentMapper 报红不用管StudentMapper studentMapper;@Testvoid contextLoads() {//queryWrapper 从数据库中查询数据并返回一个列表//queryWrapper为null 表示查询所有符合 Student 映射表的记录List<Student> list = studentMapper.selectList(null);for (int i = 0; i < list.size(); i++) {Student student = list.get(i);System.out.println(student);}}// 分页@Testpublic void test01() {//1 创建IPage分页对象,设置分页参数,1为当前页码,3为每页显示的记录数IPage<Student> page = new Page<>(1, 3);//2 执行分页查询IPage<Student> pages = studentMapper.selectPage(page, null);//3 获取分页结果System.out.println("当前页码值:" + pages.getCurrent());System.out.println("每页显示数:" + pages.getSize());System.out.println("一共多少页:" + pages.getPages());System.out.println("一共多少条数据:" + pages.getTotal());System.out.println("数据:" + pages.getRecords());}
}
常用注解说明
@TableField
:字段映射(可指定条件、是否存在等)@Version
:乐观锁标记@EnumValue
:枚举类值标记@TableLogic
:逻辑删除标记
注意事项
- 实体类建议使用Lombok简化代码
- 避免实体类属性名与SQL关键字冲突(可通过
@TableField("
order")
转义) - 分页查询需先配置分页插件
- 多数据源场景需额外配置多个
SqlSessionFactory