mybaits-plus 添加自动生成主键
时间: 2023-08-03 10:08:43 浏览: 141
MyBatis-Plus 中可以通过注解 @TableId 来指定主键生成策略。如果要使用自增主键,可以设置主键生成类型为 IDENTITY,如下所示:
```java
@TableId(type = IdType.AUTO)
private Long id;
```
这里 IdType.AUTO 表示使用数据库自增主键,如果是其他类型的主键生成策略,可以根据具体情况进行设置。另外,需要注意的是,自增主键需要在数据库中设置为自增类型,否则会报错。
相关问题
怎么使用Mybaitsplus
<think>嗯,用户想查找MyBatis Plus的使用方法或教程,询问如何用它进行开发。首先,我需要整理MyBatis Plus的基础使用步骤,确保覆盖核心功能。根据提供的引用内容,引用[1]提到MyBatis Plus简化了数据库操作,包括增删改查和分页,所以应该包括这些部分。准备工作需要依赖添加和配置,引用[1]和[2]都提到了Spring Boot集成,所以得先讲如何引入依赖,配置数据源和扫描路径。
接下来是实体类注解,引用[5]提到实体类中的注解,比如@TableName和@TableId,需要说明如何使用这些注解映射数据库表和主键。然后是Mapper层,引用[5]还提到@Mapper和BaseMapper的使用,这里要给出示例代码,展示继承BaseMapper后的基本方法。
服务层方面,引用[5]中的Service层注解如@Service,以及使用MyBatis Plus的IService接口,可能需要说明如何编写Service接口和实现类,结合引用[3]中的SQL注入器,不过用户可能更关注基础使用,所以可以暂时省略高级功能。
控制层,引用[5]中的Controller层,需要展示如何调用Service方法处理HTTP请求,并给出简单的示例代码。分页查询是常见需求,引用[1]和[2]都提到了分页插件,需要详细说明如何配置分页插件,并在Service中使用Page对象进行查询。
另外,引用[4]提到了自增ID的问题,用户可能遇到主键生成的配置问题,所以需要解释如何设置主键策略,比如使用AUTO自增或者ASSIGN_ID雪花算法,确保用户知道如何调整以避免长ID的问题。
最后,生成相关问题时要覆盖用户可能进一步想知道的内容,比如配置优化、高级功能、与Spring Boot的整合、注解使用以及主键策略,这样能帮助用户深入学习和解决问题。需要确保每个步骤都有对应的引用支持,同时语言要简洁明了,结构清晰,方便用户跟随操作。</think>### 如何使用 MyBatis Plus 进行开发?
#### 1. **准备工作**
- **添加依赖**:在项目的 `pom.xml` 中引入 MyBatis Plus 和数据库驱动依赖(如 MySQL):
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>最新版本</version>
</dependency>
```
同时配置数据源信息(`application.yml`):
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/test_db
username: root
password: 123456
```
- **启用 Mapper 扫描**:在启动类添加 `@MapperScan("com.example.mapper")` 注解,自动扫描 Mapper 接口[^2][^5]。
---
#### 2. **实体类映射**
使用注解将 Java 类与数据库表关联:
```java
@TableName("user") // 指定表名
public class User {
@TableId(type = IdType.AUTO) // 主键自增
private Long id;
private String name;
private Integer age;
// getter/setter省略
}
```
- `@TableId` 支持多种主键策略(如 `IdType.ASSIGN_ID` 雪花算法)[^4]。
---
#### 3. **Mapper 接口开发**
继承 `BaseMapper<T>` 可直接使用通用 CRUD 方法:
```java
@Mapper
public interface UserMapper extends BaseMapper<User> {
// 无需编写 XML,默认支持基础操作
}
```
- 例如:`userMapper.selectById(1)` 查询单条记录[^1][^3]。
---
#### 4. **Service 层开发**
MyBatis Plus 提供 `IService` 接口和 `ServiceImpl` 实现类:
```java
public interface UserService extends IService<User> {}
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {}
```
- 通过 `userService.page(page, queryWrapper)` 实现分页查询[^2]。
---
#### 5. **分页查询配置**
需注册分页插件(配置类中):
```java
@Configuration
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
```
- 使用示例:
```java
Page<User> page = new Page<>(1, 10); // 第1页,每页10条
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.like("name", "张");
userService.page(page, wrapper);
```
[^3]
---
#### 6. **自定义 SQL**
若需复杂 SQL,可在 Mapper 接口中定义方法并配合 XML 或注解实现:
```java
@Select("SELECT * FROM user WHERE age > #{age}")
List<User> selectByAge(@Param("age") Integer age);
```
---
#### 7. **主键策略调整**
若需重置自增 ID 起始值(如避免长 ID 问题):
```sql
ALTER TABLE user AUTO_INCREMENT = 1000;
```
并在实体类中配置 `@TableId(type = IdType.AUTO)`[^4]。
---
MybaitsPlus的完整配置
MybatisPlus是Mybatis的增强版,提供了更加强大和方便的功能,如自动生成代码、分页、多租户、逻辑删除等。下面是MybatisPlus的完整配置:
pom.xml中添加依赖:
```
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.3</version>
</dependency>
```
配置文件application.yml中添加配置:
```
# mybatis-plus配置
mybatis-plus:
# mapper.xml所在路径
mapper-locations: classpath:/mapper/*.xml
# 实体类扫描路径
typeAliasesPackage: com.example.entity
global-config:
# 开启驼峰命名转换
db-column-underline: true
id-type: auto
field-strategy: not_empty
# 分页配置
page-size: 10
page-sql-limit: true
# 数据库配置
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
```
其中,mapper-locations指定mapper.xml所在路径,typeAliasesPackage指定实体类扫描路径,db-column-underline开启驼峰命名转换,id-type指定主键生成策略,field-strategy指定自动填充策略,page-size和page-sql-limit指定分页配置,datasource指定数据库配置。
然后,在实体类上使用注解配置:
```
@Data
@TableName("user")
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String email;
@TableLogic
private Integer deleted;
}
```
其中,@TableName指定实体类对应的表名,@TableId指定主键,@TableLogic指定逻辑删除字段。
最后,在Mapper接口上继承BaseMapper即可使用MybatisPlus提供的增删改查方法:
```
@Repository
public interface UserMapper extends BaseMapper<User> {
}
```
以上就是MybatisPlus的完整配置。
阅读全文
相关推荐












