已知数据库book,要求使用MyBatis-Plus和Spring Boot框架完成服务器端应用的开发,能够给客户端提供符合RESTful规 范的JSON数据接口,并用在idea中用postman进行相应测 试
时间: 2023-06-19 16:08:33 浏览: 124
本项目主要使用SpringBoot整合MyBatis-Plus来进行开发
好的,以下是简单的步骤:
1. 创建Spring Boot项目,添加MyBatis-Plus和相关依赖:
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.x.x</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
```
2. 配置数据库连接和MyBatis-Plus:
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
# MyBatis-Plus配置
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.book.entity
```
3. 创建实体类和Mapper接口:
```java
@Data
public class Book {
private Long id;
private String name;
private String author;
private BigDecimal price;
}
public interface BookMapper extends BaseMapper<Book> {
}
```
4. 创建控制器类,实现RESTful接口:
```java
@RestController
@RequestMapping("/api/books")
public class BookController {
@Autowired
private BookMapper bookMapper;
@GetMapping("")
public List<Book> list() {
return bookMapper.selectList(null);
}
@GetMapping("/{id}")
public Book get(@PathVariable Long id) {
return bookMapper.selectById(id);
}
@PostMapping("")
public boolean add(@RequestBody Book book) {
return bookMapper.insert(book) > 0;
}
@PutMapping("/{id}")
public boolean update(@PathVariable Long id, @RequestBody Book book) {
book.setId(id);
return bookMapper.updateById(book) > 0;
}
@DeleteMapping("/{id}")
public boolean delete(@PathVariable Long id) {
return bookMapper.deleteById(id) > 0;
}
}
```
5. 使用Postman进行测试,例如:
- GET请求:http://localhost:8080/api/books
- POST请求:http://localhost:8080/api/books,请求体为:
```json
{
"name": "Java编程思想",
"author": "Bruce Eckel",
"price": 99.99
}
```
以上就是使用MyBatis-Plus和Spring Boot框架完成服务器端应用开发的简单步骤。
阅读全文