1、環境準備
1. 環境準備
1. 引入依賴
在Spring Boot項目的pom.xml
文件中引入MyBatis-Plus及其數據庫驅動的依賴。這里以MySQL為例:
<!-- Spring Boot Starter Web -->
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> <!-- MyBatis-Plus Starter -->
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>你的版本號</version>
</dependency> <!-- MySQL 驅動 -->
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope>
</dependency> <!-- Lombok(可選,用于簡化實體類) -->
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional>
</dependency>
2. 配置數據庫
在application.yml
或application.properties
中配置數據庫連接信息:
spring: datasource: url: jdbc:mysql://localhost:3306/your_database?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC username: root password: your_password driver-class-name: com.mysql.cj.jdbc.Driver mybatis-plus: mapper-locations: classpath:/mappers/*.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. 創建實體類
使用Lombok簡化代碼,例如:
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data; @Data
@TableName("user")
public class User { @TableId private Long id; private String name; private Integer age; // 其他字段...
}
4. 創建Mapper接口
繼承BaseMapper
來簡化開發:
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.User; public interface UserMapper extends BaseMapper<User> { // 這里可以添加自定義的Mapper方法,如果不需要可以留空
}
5. 創建Service層
如果你需要,可以創建一個Service層來處理業務邏輯:
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper; public class UserService extends ServiceImpl<UserMapper, User> { // 這里可以添加自定義的業務邏輯方法
}
6. 創建Controller層
創建RESTful API來處理HTTP請求:
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; @RestController
@RequestMapping("/users")
public class UserController { @Autowired private UserService userService; @GetMapping("/") public List<User> listAllUsers() { return userService.list(); } @PostMapping("/") public User createUser(@RequestBody User user) { userService.save(user); return user; } @GetMapping("/{id}") public User getUserById(@PathVariable Long id) { return userService.getById(id); } @PutMapping("/{id}") public User updateUser(@PathVariable Long id, @RequestBody User user) { user.setId(id); userService.updateById(user); return user; }
2、增加
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/users")
public class UserController { @Autowired private UserService userService; @PostMapping("/") public User createUser(@RequestBody User user) { userService.save(user); // 調用Service層的save方法,實際調用的是MyBatis-Plus的插入操作 return user; }
}
3、刪除
// 刪除單個用戶
@DeleteMapping("/{id}")
public String deleteUser(@PathVariable Long id) { userService.removeById(id); // 或者使用userMapper.deleteById(id)直接調用Mapper return "User deleted successfully";
} // 批量刪除用戶(示例)
@DeleteMapping("/batch")
public String deleteUsers(@RequestBody List<Long> ids) { userService.removeByIds(ids); // 批量刪除 return "Users deleted successfully";
}
4、修改
// 更新用戶信息
@PutMapping("/{id}")
public User updateUser(@PathVariable Long id, @RequestBody User user) { user.setId(id); // 確保用戶ID被設置 userService.updateById(user); // 調用Service層的updateById方法 return user;
}
5、查找
// 查詢單個用戶
@GetMapping("/{id}")
public User getUserById(@PathVariable Long id) { return userService.getById(id); // 調用Service層的getById方法
} // 查詢所有用戶
@GetMapping("/")
public List<User> getAllUsers() { return userService.list(); // 調用Service層的list方法,實際調用的是MyBatis-Plus的查詢所有操作
} // 根據條件查詢用戶(示例,需要自定義Mapper或Service方法)
@GetMapping("/search")
public List<User> searchUsers(@RequestParam String name) { // 這里僅為示例,實際中可能需要自定義Mapper接口中的方法 // return userService.searchByName(name); // 假設有這樣一個自定義方法 // ... 實際實現中,你可能需要編寫一個自定義的Mapper方法并使用@Select注解來定義SQL查詢
}