使用spring boot 整合mybatis 完成接口的开发 要保存的数据: 书籍信息:编号,书名,出版社,出版日期,页数,价格 编写接口: GET http://host:port/book/1 查询书籍信息 POST http://host:port/book 添加书籍信息 ```json { "name": "spring boot 实战", "publish": "xx出版社", "publishDate": "2020-12-03", "pages": 500, "price": 99.00 } ``` POST http://host:port/book/update 修改书籍信息 ```json { "id": 1, "name": "spring boot 实战", "publish": "xx出版社", "publishDate": "2020-12-03", "pages": 500, "price": 99.00 } ``` DELETE http://host:port/book 删除书籍信息 ```json { "id": 1 } ``` 项目要开启web和mapper的日志,日志保存到文件中(日志级别不分类),要提测试过程中生成的日志文件。
时间: 2023-07-05 17:03:52 浏览: 127
首先,需要在pom.xml文件中引入spring-boot-starter-web和mybatis-spring-boot-starter依赖。
```xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
</dependencies>
```
然后,需要在application.properties文件中配置数据源和mybatis的配置文件位置。
```properties
# datasource
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# mybatis
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.demo.entity
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
```
其中,mapper/*.xml表示mybatis的mapper文件存放在classpath:mapper目录下,com.example.demo.entity表示实体类所在的包名。
接着,创建Book实体类。
```java
public class Book {
private Integer id;
private String name;
private String publish;
private Date publishDate;
private Integer pages;
private Double price;
// getter and setter
}
```
然后,创建BookMapper接口。
```java
@Mapper
public interface BookMapper {
Book selectById(Integer id);
void insert(Book book);
void update(Book book);
void delete(Integer id);
}
```
在接口上添加@Mapper注解,表示该接口是mybatis的mapper接口。
下面是BookMapper.xml文件的内容。
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.BookMapper">
<resultMap id="bookResultMap" type="com.example.demo.entity.Book">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="publish" property="publish"/>
<result column="publish_date" property="publishDate"/>
<result column="pages" property="pages"/>
<result column="price" property="price"/>
</resultMap>
<select id="selectById" resultMap="bookResultMap">
SELECT * FROM book WHERE id = #{id}
</select>
<insert id="insert">
INSERT INTO book(name, publish, publish_date, pages, price)
VALUES(#{name}, #{publish}, #{publishDate}, #{pages}, #{price})
</insert>
<update id="update">
UPDATE book SET name = #{name}, publish = #{publish}, publish_date = #{publishDate},
pages = #{pages}, price = #{price} WHERE id = #{id}
</update>
<delete id="delete">
DELETE FROM book WHERE id = #{id}
</delete>
</mapper>
```
该文件定义了BookMapper接口中的方法对应的SQL语句。
最后,创建BookController类。
```java
@RestController
@RequestMapping("/book")
public class BookController {
@Autowired
private BookMapper bookMapper;
@GetMapping("/{id}")
public Book selectById(@PathVariable Integer id) {
return bookMapper.selectById(id);
}
@PostMapping
public void insert(@RequestBody Book book) {
bookMapper.insert(book);
}
@PostMapping("/update")
public void update(@RequestBody Book book) {
bookMapper.update(book);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable Integer id) {
bookMapper.delete(id);
}
}
```
其中,@Autowired注解表示自动注入BookMapper接口的实现类。在各个方法上,通过BookMapper接口调用对应的SQL操作。
最后,需要在application.properties文件中配置日志输出到文件中。
```properties
# logging
logging.level.root=info
logging.file.name=mylog.log
logging.file.path=./logs
```
其中,logging.level.root表示输出日志的级别为info,logging.file.name表示日志文件的名称,logging.file.path表示日志文件存放的路径。
测试过程中生成的日志文件会存放在./logs/mylog.log路径下。
阅读全文