MyBatis-Plus是一款优秀的MyBatis增强工具,提供了丰富的注解和配置,简化了CRUD操作和数据库访问。本文将介绍MyBatis-Plus常见的注解及其配置,帮助开发者高效使用这一工具。

一、常见注解

1. @TableName

@TableName用于指定实体类对应的数据库表名。

@TableName("user")
public class User {
    private Long id;
    private String name;
    private Integer age;
    // getters and setters
}

2. @TableId

@TableId用于标识主键字段,可以指定主键类型,如自增、UUID等。

@TableId(type = IdType.AUTO)
private Long id;

3. @TableField

@TableField用于标识非主键字段,常用于字段名称与数据库列名不同的情况。

@TableField("user_name")
private String name;

4. @TableLogic

@TableLogic用于实现逻辑删除。

@TableLogic
private Integer deleted;

5. @Version

@Version用于标识乐观锁字段。

@Version
private Integer version;

二、配置

1. 数据源配置

在 application.yml中配置数据源。

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

2. MyBatis-Plus配置

在 application.yml中配置MyBatis-Plus。

mybatis-plus:
  mapper-locations: classpath:/mapper/*.xml
  type-aliases-package: com.example.demo.entity
  global-config:
    db-config:
      id-type: auto
      logic-delete-value: 1
      logic-not-delete-value: 0

3. 配置分页插件

MyBatis-Plus提供分页插件,简化分页查询。

@Configuration
public class MybatisPlusConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

4. 配置全局主键策略

在 application.yml中设置全局主键策略。

mybatis-plus:
  global-config:
    db-config:
      id-type: auto

三、示例代码

以下是一个完整的示例,包括实体类、Mapper接口和Service类。

1. 实体类

import com.baomidou.mybatisplus.annotation.*;

@TableName("user")
public class User {
    @TableId(type = IdType.AUTO)
    private Long id;

    @TableField("user_name")
    private String name;

    private Integer age;

    @TableLogic
    private Integer deleted;

    @Version
    private Integer version;

    // getters and setters
}

2. Mapper接口

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

public interface UserMapper extends BaseMapper<User> {
}

3. Service类

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class UserService extends ServiceImpl<UserMapper, User> {
}

4. 控制器类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public List<User> list() {
        return userService.list();
    }

    @PostMapping("/add")
    public boolean add(@RequestBody User user) {
        return userService.save(user);
    }

    @PutMapping("/update")
    public boolean update(@RequestBody User user) {
        return userService.updateById(user);
    }

    @DeleteMapping("/delete/{id}")
    public boolean delete(@PathVariable Long id) {
        return userService.removeById(id);
    }
}

四、思维导图

graph TB
A[MyBatis-Plus] --> B[常见注解]
A --> C[配置]
B --> D[@TableName]
B --> E[@TableId]
B --> F[@TableField]
B --> G[@TableLogic]
B --> H[@Version]
C --> I[数据源配置]
C --> J[MyBatis-Plus配置]
C --> K[分页插件]
C --> L[全局主键策略]

五、总结

MyBatis-Plus通过丰富的注解和配置,极大地简化了MyBatis的使用,提高了开发效率。通过掌握常见注解和配置方法,可以更高效地进行数据库操作,提升项目的整体质量和性能。希望本文内容对您理解和应用MyBatis-Plus有所帮助。