基于Spring Boot+MyBatis实现图书信息的新增、修改、删除、查询功能,并实现MySQL数据库的操作。
MySQL数据库创建图书表(t_book),图书表有主键、图书名称、图书类别、作者、出版社、简介信息。
创建t_book表
create table t_book(
id varchar(32),
name varchar(50),
category varchar(20),
author varchar(50),
press varchar(50),
content varchar(200)
);
初始化数据
insert into t_book values('1001','西游记','文学名著','吴承恩','人民文学','师徒四人取经');
insert into t_book values('1002','水浒传','文学名著','施耐庵','人民文学','梁山108好汉');
创建Spring Boot项目,项目名称为springboot-book01。
pom文件如下所示:
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.3.0
com.mysql
mysql-connector-j
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
resources目录下创建application.yml。
# 配置端口号
server:
port: 8090
# 配置数据源
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/student
username: root
password: root
# 配置MyBatis
mybatis:
mapper-locations: classpath*:mapper/**/*Mapper.xml
type-aliases-package: com.wfit
com.wfit.boot.commons目录下创建Constants.java。
public class Constants {
// 默认成功码
public static final int SUCCESS_CODE = 200;
public static final String SUCCESS_MSG = "操作成功";
// 默认失败码
public static final int ERROR_CODE = 500;
public static final String ERROR_MSG = "系统异常";
}
com.wfit.boot.commons目录下创建Result.java。
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {
//响应码
private int code;
//响应消息
private String msg;
//响应结果
private T data;
public static Result success(T data){
return new Result<>(Constants.SUCCESS_CODE,Constants.SUCCESS_MSG,data);
}
public static Result error(T data){
return new Result<>(Constants.ERROR_CODE,Constants.ERROR_MSG,data);
}
}
com.wfit.boot.model目录下创建Book.java。
@Data
public class Book {
private String id;
private String name;
private String category;
private String author;
private String press;
private String content;
}
com.wfit.boot.controller目录下创建BookController.java。
@RestController //@Controller和@ResponseBody两个注解的结合
@RequestMapping("/book")
public class BookController {
@Autowired
private BookService bookService;
/**
* 新增图书信息
*/
@PostMapping("/add") //@RequestMapping(method = {RequestMethod.POST})
public Result addBook(@RequestBody Book book){
bookService.saveBook(book);
return Result.success("新增成功!");
}
/**
* 修改图书信息
*/
@PostMapping("/update")
public Result updateBook(@RequestBody Book book){
bookService.updateBook(book);
return Result.success("修改成功!");
}
/**
* 删除图书信息
*/
@GetMapping("/del") //@RequestMapping(method = {RequestMethod.GET})
public Result delBook(String id){
bookService.delBook(id);
return Result.success("删除成功!");
}
/**
* 查询图书信息
*/
@GetMapping("/query")
public Result queryBook(){
List bookList = bookService.queryBook();
return Result.success(bookList);
}
}
com.wfit.boot.service目录下创建BookService.java。
public interface BookService {
public void saveBook(Book book);
public void updateBook(Book book);
public void delBook(String id);
public List queryBook();
}
com.wfit.boot.service.impl目录下创建BookServiceImpl.java。
@Service
public class BookServiceImpl implements BookService {
@Resource
private BookMapper bookMapper;
@Override
public void saveBook(Book book) {
bookMapper.saveBook(book);
}
@Override
public void updateBook(Book book) {
bookMapper.updateBook(book);
}
@Override
public void delBook(String id) {
bookMapper.delBook(id);
}
@Override
public List queryBook() {
return bookMapper.queryBook();
}
}
com.wfit.boot.mapper目录下创建BookMapper.java。
@Mapper
public interface BookMapper {
public void saveBook(Book book);
public void updateBook(Book book);
public void delBook(String id);
public List queryBook();
}
resources.mapper目录下创建BookMapper.xml。
insert into t_book values(
#{id},
#{name},
#{category},
#{author},
#{press},
#{content}
)
update t_book
set name = #{name}, category = #{category}
where id = #{id}
delete from t_book where id = #{id}