当前位置: 首页 > news >正文

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:逻辑删除标记

注意事项

  1. 实体类建议使用Lombok简化代码
  2. 避免实体类属性名与SQL关键字冲突(可通过@TableField("order")转义)
  3. 分页查询需先配置分页插件
  4. 多数据源场景需额外配置多个SqlSessionFactory
http://www.dtcms.com/a/308372.html

相关文章:

  • 如何监控项目的每个阶段,提高执行效率
  • SchemaCrawler:一款免费开源的数据库文档工具
  • 斐波那契数
  • AI学习笔记三十三:基于Opencv的单目标跟踪
  • OpenCSG月度更新2025.7
  • leecode18 四数之和
  • 个股期权合约期内遇到标的停牌,如何处置?
  • DoRA详解:从LoRA到权重分解的进化
  • Redis线程模型讨论
  • 修改VSCode远程SSH的PATH
  • JVM字节码文件结构剖析
  • JVM学习日记(十二)Day12
  • 解释 MySQL 中的 EXPLAIN 命令的作用和使用场景
  • 格雷希尔G25F系列电气端口快速封堵接头,解决电池包、电机控制器等壳体的气密性测试难题,提升效率与可靠性,助力新能源汽车生产。
  • ARM--中断
  • 三坐标测量仪攻克深孔检测!破解新能源汽车阀体阀孔测量难题
  • 雷霆战机游戏代码
  • ABS系统专用磁阻式汽车轮速传感器
  • 建设公司如何优化梳理部门职责,提高运作效率?
  • 中烟创新自研【烟草行政许可文书制作系统】纳入“北京市人工智能赋能行业发展典型案例集”
  • 电子电气架构 --- 车载48V系统
  • 如何导入json文件到数据库
  • 生信技能76 - 根据SNP列表提取SNP位点上下游的参考基因组fasta
  • RocksDB关键设计详解
  • MySQL 45讲 16-17
  • 【Linux网络编程】网络层协议 - IP
  • 大模型微调工具LLaMA-Factory的安装流程
  • Git Pull 时遇到 Apply 和 Abort 选项?详解它们的含义与应对策略
  • sqli-labs:Less-16关卡详细解析
  • 数字通信原理--数字通信仿真基础