一、配置 Mapper 扫描在 Spring Boot 启动类上添加MapperScan注解指定 Mapper 接口所在的包路径javaimport org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; SpringBootApplication MapperScan(com.seazon.cloud.mapper) // 替换成你实际的 Mapper 包路径 public class DriverAnalysisApplication { public static void main(String[] args) { SpringApplication.run(DriverAnalysisApplication.class, args); } }或者你也可以在每个 Mapper 接口上单独使用Mapper注解不推荐每个都要写比较繁琐。二、编写 Entity 实体类javaimport com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.IdType; import lombok.Data; import java.time.LocalDateTime; Data TableName(driver_info) // 对应数据库表名 public class DriverInfo { TableId(type IdType.AUTO) // 主键自增策略 private Long id; private String driverName; private String driverNo; private Integer age; private LocalDateTime createTime; private LocalDateTime updateTime; }常用注解说明注解作用TableName指定数据库表名TableId指定主键字段TableField指定字段映射非主键TableLogic逻辑删除字段三、编写 Mapper 接口javaimport com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Mapper; import com.seazon.cloud.entity.DriverInfo; Mapper public interface DriverInfoMapper extends BaseMapperDriverInfo { // 继承 BaseMapper 后已经拥有了基础的 CRUD 方法 // insert、deleteById、updateById、selectById、selectList 等 // 如果有自定义复杂查询可以在这里写方法并配合 XML 或注解实现 // 例如 // Select(SELECT * FROM driver_info WHERE driver_no #{driverNo}) // DriverInfo selectByDriverNo(String driverNo); }BaseMapper提供了哪些方法方法作用insert(T entity)插入一条记录deleteById(Serializable id)根据 ID 删除updateById(T entity)根据 ID 更新selectById(Serializable id)根据 ID 查询selectList(WrapperT queryWrapper)条件查询列表selectPage(IPageT page, WrapperT queryWrapper)分页查询四、Service 层使用方式一直接注入 Mapper简单场景javaimport org.springframework.stereotype.Service; import org.springframework.beans.factory.annotation.Autowired; import com.seazon.cloud.mapper.DriverInfoMapper; import com.seazon.cloud.entity.DriverInfo; Service public class DriverService { Autowired private DriverInfoMapper driverInfoMapper; public DriverInfo getDriverById(Long id) { return driverInfoMapper.selectById(id); } public boolean saveDriver(DriverInfo driver) { return driverInfoMapper.insert(driver) 0; } }方式二使用 MyBatis-Plus 的 Service 封装推荐MyBatis-Plus 提供了更强大的 Service 层封装支持更多高级功能。1. 定义 Service 接口javaimport com.baomidou.mybatisplus.extension.service.IService; import com.seazon.cloud.entity.DriverInfo; public interface IDriverService extends IServiceDriverInfo { // 可以在这里定义额外的业务方法 DriverInfo getByDriverNo(String driverNo); }2. 实现 Service 接口javaimport com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.stereotype.Service; import com.seazon.cloud.mapper.DriverInfoMapper; import com.seazon.cloud.entity.DriverInfo; Service public class DriverServiceImpl extends ServiceImplDriverInfoMapper, DriverInfo implements IDriverService { Override public DriverInfo getByDriverNo(String driverNo) { // 使用 QueryWrapper 构造查询条件 return this.lambdaQuery() .eq(DriverInfo::getDriverNo, driverNo) .one(); } }3. 在 Controller 或其他地方使用 ServicejavaRestController RequestMapping(/api/driver) public class DriverController { Autowired private IDriverService driverService; GetMapping(/{id}) public DriverInfo getDriver(PathVariable Long id) { return driverService.getById(id); } PostMapping public boolean saveDriver(RequestBody DriverInfo driver) { return driverService.save(driver); } }五、MyBatis-Plus 常用查询示例javaimport com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; // 1. 简单条件查询 LambdaQueryWrapperDriverInfo wrapper new LambdaQueryWrapper(); wrapper.eq(DriverInfo::getAge, 25) .like(DriverInfo::getDriverName, 张); ListDriverInfo list driverInfoMapper.selectList(wrapper); // 2. 分页查询 PageDriverInfo page new Page(1, 10); // 第1页每页10条 IPageDriverInfo result driverInfoMapper.selectPage(page, null); // 3. 多条件查询 LambdaQueryWrapperDriverInfo wrapper new LambdaQueryWrapper(); wrapper.between(DriverInfo::getAge, 20, 30) .orderByDesc(DriverInfo::getCreateTime); ListDriverInfo list driverInfoMapper.selectList(wrapper);六、配置 MyBatis-Plus可选在application.yml或application.properties中添加配置yamlmybatis-plus: # 实体类扫描包可以不用写因为 TableName 已经指定了表名 # type-aliases-package: com.seazon.cloud.entity # Mapper XML 文件位置如果使用 XML 方式 mapper-locations: classpath:mapper/*.xml # 全局配置 global-config: db-config: # 逻辑删除字段名 logic-delete-field: deleted # 逻辑删除值已删除 logic-delete-value: 1 # 逻辑未删除值未删除 logic-not-delete-value: 0 # 主键类型自增 id-type: auto configuration: # 开启驼峰命名转换 map-underscore-to-camel-case: true # 开启 SQL 日志开发环境方便调试 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl七、验证是否生效启动项目观察日志中是否有 MyBatis-Plus 相关的初始化信息。写一个简单的测试接口或单元测试调用 Mapper 或 Service 方法确认能正常操作数据库。javaSpringBootTest public class DriverTest { Autowired private DriverInfoMapper driverInfoMapper; Test public void testSelect() { ListDriverInfo list driverInfoMapper.selectList(null); System.out.println(数据条数 list.size()); } }总结步骤关键操作1在启动类添加MapperScan2编写 Entity使用TableName、TableId等注解3编写 Mapper 接口继承BaseMapperT4在 Service 中注入 Mapper 或继承ServiceImpl5可选在application.yml中配置 MyBatis-Plus按照以上步骤操作后MyBatis-Plus 就能在你的项目中正常工作了。