工程POM引入mybatis-plus-boot-starter后
📅 2026/7/23 4:35:54
👁️ 阅读次数
📝 编程学习
一、配置 Mapper 扫描
在 Spring Boot 启动类上添加@MapperScan注解,指定 Mapper 接口所在的包路径:
java
import 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 实体类
java
import 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 接口
java
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Mapper; import com.seazon.cloud.entity.DriverInfo; @Mapper public interface DriverInfoMapper extends BaseMapper<DriverInfo> { // 继承 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(Wrapper<T> queryWrapper) | 条件查询列表 |
selectPage(IPage<T> page, Wrapper<T> queryWrapper) | 分页查询 |
四、Service 层使用
方式一:直接注入 Mapper(简单场景)
java
import 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 接口:
java
import com.baomidou.mybatisplus.extension.service.IService; import com.seazon.cloud.entity.DriverInfo; public interface IDriverService extends IService<DriverInfo> { // 可以在这里定义额外的业务方法 DriverInfo getByDriverNo(String driverNo); }2. 实现 Service 接口:
java
import 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 ServiceImpl<DriverInfoMapper, DriverInfo> implements IDriverService { @Override public DriverInfo getByDriverNo(String driverNo) { // 使用 QueryWrapper 构造查询条件 return this.lambdaQuery() .eq(DriverInfo::getDriverNo, driverNo) .one(); } }3. 在 Controller 或其他地方使用 Service:
java
@RestController @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 常用查询示例
java
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; // 1. 简单条件查询 LambdaQueryWrapper<DriverInfo> wrapper = new LambdaQueryWrapper<>(); wrapper.eq(DriverInfo::getAge, 25) .like(DriverInfo::getDriverName, "张"); List<DriverInfo> list = driverInfoMapper.selectList(wrapper); // 2. 分页查询 Page<DriverInfo> page = new Page<>(1, 10); // 第1页,每页10条 IPage<DriverInfo> result = driverInfoMapper.selectPage(page, null); // 3. 多条件查询 LambdaQueryWrapper<DriverInfo> wrapper = new LambdaQueryWrapper<>(); wrapper.between(DriverInfo::getAge, 20, 30) .orderByDesc(DriverInfo::getCreateTime); List<DriverInfo> list = driverInfoMapper.selectList(wrapper);
六、配置 MyBatis-Plus(可选)
在application.yml或application.properties中添加配置:
yaml
mybatis-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 方法,确认能正常操作数据库。
java
@SpringBootTest public class DriverTest { @Autowired private DriverInfoMapper driverInfoMapper; @Test public void testSelect() { List<DriverInfo> list = driverInfoMapper.selectList(null); System.out.println("数据条数:" + list.size()); } }总结
| 步骤 | 关键操作 |
|---|---|
| 1 | 在启动类添加@MapperScan |
| 2 | 编写 Entity,使用@TableName、@TableId等注解 |
| 3 | 编写 Mapper 接口,继承BaseMapper<T> |
| 4 | 在 Service 中注入 Mapper 或继承ServiceImpl |
| 5 | (可选)在application.yml中配置 MyBatis-Plus |
按照以上步骤操作后,MyBatis-Plus 就能在你的项目中正常工作了。
编程学习
技术分享
实战经验