mybatis plus 怎么用
时间: 2023-10-06 14:12:18 浏览: 80
MyBatis Plus 是 MyBatis 的增强工具,提供了很多增强功能,简化了开发。
下面简单介绍 MyBatis Plus 的使用流程:
1. 引入 MyBatis Plus 依赖:
```xml
<!-- MyBatis Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
```
2. 配置数据源和 MyBatis:
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
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. 创建实体类:
```java
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
```
4. 创建 Mapper 接口:
```java
public interface UserMapper extends BaseMapper<User> {
}
```
5. 使用 MyBatis Plus 提供的 API 进行数据库操作:
```java
@Autowired
private UserMapper userMapper;
@Test
public void testInsert() {
User user = new User();
user.setName("Tom");
user.setAge(20);
user.setEmail("tom@qq.com");
int result = userMapper.insert(user);
System.out.println(result);
}
@Test
public void testSelect() {
List<User> userList = userMapper.selectList(null);
System.out.println(userList);
}
@Test
public void testUpdate() {
User user = new User();
user.setId(1L);
user.setName("Jerry");
int result = userMapper.updateById(user);
System.out.println(result);
}
@Test
public void testDelete() {
int result = userMapper.deleteById(1L);
System.out.println(result);
}
```
以上就是 MyBatis Plus 的简单使用流程,更多详细内容可以查看官方文档。
阅读全文