spring boot整合mybatisplus做增删改查结合 IService<User>
时间: 2024-06-09 09:05:01 浏览: 115
好的,下面是一个简单的示例代码:
首先,需要在pom.xml文件中添加mybatis-plus和mysql数据库驱动的依赖:
```
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.7.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
```
然后,在application.yml中配置数据源和mybatis-plus的相关配置:
```
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
# mybatis-plus配置
mybatis-plus:
mapper-locations: classpath*:mapper/*.xml
```
接着,定义一个User实体类:
```java
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("user")
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String email;
}
```
定义一个UserService接口,继承IService<User>:
```java
public interface UserService extends IService<User> {
}
```
实现UserService接口:
```java
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
```
最后,定义一个UserMapper接口,使用Mybatis-plus的Mapper接口:
```java
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
```
这样就可以使用mybatis-plus进行增删改查了。例如,查询所有用户:
```java
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
@Override
public List<User> getAll() {
return list();
}
}
```
更多mybatis-plus的使用可以参考官方文档:https://mybatis.plus/guide/
阅读全文