MyBatis-Plus 提供了丰富的增强版的 CRUD 方法,使得开发者能够更简洁、高效地进行数据库操作。以下是如何使用 MyBatis-Plus 自带的增强版 CRUD 方法的基本步骤:
- 添加依赖
首先,确保你的 Maven 项目中已经添加了 MyBatis-Plus 的相关依赖,包括核心依赖和数据库驱动依赖。
xml复制代码
| <!-- MyBatis-Plus -->  | |
| <dependency>  | |
| <groupId>com.baomidou</groupId>  | |
| <artifactId>mybatis-plus-boot-starter</artifactId>  | |
| <version>最新版本</version>  | |
| </dependency>  | |
| <!-- 数据库驱动,例如 MySQL -->  | |
| <dependency>  | |
| <groupId>mysql</groupId>  | |
| <artifactId>mysql-connector-java</artifactId>  | |
| <version>最新版本</version>  | |
| </dependency> | 
- 配置 MyBatis-Plus
在 application.properties 或 application.yml 文件中配置数据库连接信息,并设置 MyBatis-Plus 的相关配置。
properties复制代码
| # application.properties 示例  | |
| spring.datasource.url=jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC  | |
| spring.datasource.username=your_username  | |
| spring.datasource.password=your_password  | |
| spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver  | |
| # MyBatis-Plus 配置  | |
| mybatis-plus.mapper-locations=classpath:/mapper/*.xml  | |
| mybatis-plus.type-aliases-package=com.yourpackage.entity | 
- 创建实体类
创建一个与数据库表对应的实体类,并使用 MyBatis-Plus 提供的注解来配置字段和主键等。
java复制代码
| import com.baomidou.mybatisplus.annotation.IdType;  | |
| import com.baomidou.mybatisplus.annotation.TableId;  | |
| import com.baomidou.mybatisplus.annotation.TableName;  | |
| import lombok.Data;  | |
| @Data  | |
| @TableName("your_table_name") // 指定数据库表名  | |
| public class YourEntity {  | |
| @TableId(value = "id", type = IdType.AUTO) // 指定主键字段和生成策略  | |
| private Long id;  | |
| private String name;  | |
| // 其他字段...  | |
| } | 
- 创建 Mapper 接口
创建一个继承 BaseMapper 的 Mapper 接口,MyBatis-Plus 会自动实现该接口中定义的 CRUD 方法。
java复制代码
| import com.baomidou.mybatisplus.core.mapper.BaseMapper;  | |
| import com.yourpackage.entity.YourEntity;  | |
| import org.apache.ibatis.annotations.Mapper;  | |
| @Mapper  | |
| public interface YourEntityMapper extends BaseMapper<YourEntity> {  | |
| // 可以定义额外的自定义方法,如果需要的话  | |
| } | 
- 使用 Mapper 进行 CRUD 操作
在 Service 层或 Controller 层注入 Mapper 接口,然后调用其提供的 CRUD 方法。
java复制代码
| import com.yourpackage.entity.YourEntity;  | |
| import com.yourpackage.mapper.YourEntityMapper;  | |
| import org.springframework.beans.factory.annotation.Autowired;  | |
| import org.springframework.stereotype.Service;  | |
| @Service  | |
| public class YourEntityService {  | |
| @Autowired  | |
| private YourEntityMapper yourEntityMapper;  | |
| // 插入记录  | |
| public int insert(YourEntity entity) {  | |
| return yourEntityMapper.insert(entity);  | |
| }  | |
| // 查询单个记录  | |
| public YourEntity selectById(Long id) {  | |
| return yourEntityMapper.selectById(id);  | |
| }  | |
| // 查询列表  | |
| public List<YourEntity> selectList() {  | |
| return yourEntityMapper.selectList(null); // 传入条件构造器进行复杂查询  | |
| }  | |
| // 更新记录  | |
| public int updateById(YourEntity entity) {  | |
| return yourEntityMapper.updateById(entity);  | |
| }  | |
| // 删除记录  | |
| public int deleteById(Long id) {  | |
| return yourEntityMapper.deleteById(id);  | |
| }  | |
| } | 
- 自定义方法
如果你需要实现自定义的 CRUD 方法,可以在 Mapper 接口中定义方法,并在对应的 XML 文件中编写 SQL 语句。MyBatis-Plus 提供了条件构造器 QueryWrapper 和 UpdateWrapper,使得编写条件查询和更新操作更为方便。
通过以上步骤,你就可以使用 MyBatis-Plus 自带的增强版 CRUD 方法进行数据库操作了。MyBatis-Plus 还提供了很多其他功能,如分页插件、性能