java使用mybatisplus插入
时间: 2023-11-11 19:07:27 浏览: 86
MybatisPlus插件
好的,针对您的问题,使用MybatisPlus插入数据可以按照以下步骤进行:
1. 首先,需要在pom.xml文件中添加MybatisPlus的依赖:
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
```
2. 在application.yml文件中配置MybatisPlus的相关信息,例如数据库连接信息、实体类所在包等:
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
mapper-locations: classpath*:mapper/*.xml
type-aliases-package: com.example.demo.entity
```
3. 创建实体类,并使用MybatisPlus的注解进行配置,例如:
```java
@Data
@TableName("user")
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
}
```
4. 创建Mapper接口,并继承BaseMapper接口,例如:
```java
public interface UserMapper extends BaseMapper<User> {
}
```
5. 在Service层中调用Mapper接口的insert方法进行插入操作,例如:
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public boolean save(User user) {
return userMapper.insert(user) > 0;
}
}
```
阅读全文