一、案例需求
基于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好汉');
三、代码实现
1. 创建Spring Boot项目
创建Spring Boot项目,项目名称为springboot-book01。
2. 选择依赖
pom文件如下所示:
<dependencies><!--spring boot web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--mybatis--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.3.0</version></dependency><!--mysql--><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
</dependencies>
3. 创建配置文件
resources目录下创建application.yml。
# 配置端口号
server:port: 8090# 配置数据源
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/studentusername: rootpassword: root# 配置MyBatis
mybatis:mapper-locations: classpath*:mapper/**/*Mapper.xmltype-aliases-package: com.wfit
4. 创建Constants常量类
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 = "系统异常";
}
5. 创建Result类
com.wfit.boot.commons目录下创建Result.java。
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {//响应码private int code;//响应消息private String msg;//响应结果private T data;public static <T> Result<T> success(T data){return new Result<>(Constants.SUCCESS_CODE,Constants.SUCCESS_MSG,data);}public static <T> Result<T> error(T data){return new Result<>(Constants.ERROR_CODE,Constants.ERROR_MSG,data);}}
6. 创建Book实体类
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;}
7. 创建BookController类
com.wfit.boot.controller目录下创建BookController.java。
@RestController //@Controller和@ResponseBody两个注解的结合
@RequestMapping("/book")
public class BookController {@Autowiredprivate 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<Book> bookList = bookService.queryBook();return Result.success(bookList);}
}
8. 创建BookService接口
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<Book> queryBook();}
9. 创建BookServiceImpl类
com.wfit.boot.service.impl目录下创建BookServiceImpl.java。
@Service
public class BookServiceImpl implements BookService {@Resourceprivate BookMapper bookMapper;@Overridepublic void saveBook(Book book) {bookMapper.saveBook(book);}@Overridepublic void updateBook(Book book) {bookMapper.updateBook(book);}@Overridepublic void delBook(String id) {bookMapper.delBook(id);}@Overridepublic List<Book> queryBook() {return bookMapper.queryBook();}
}
10. 创建BookMapper接口
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<Book> queryBook();
}
11. 创建BookMapper.xml文件
resources.mapper目录下创建BookMapper.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.wfit.boot.mapper.BookMapper"><!--新增图书信息--><insert id="saveBook" parameterType="com.wfit.boot.model.Book">insert into t_book values(#{id},#{name},#{category},#{author},#{press},#{content})</insert><!--修改图书信息--><update id="updateBook" parameterType="com.wfit.boot.model.Book">update t_bookset name = #{name}, category = #{category}where id = #{id}</update><!--删除图书信息--><delete id="delBook" parameterType="java.lang.String">delete from t_book where id = #{id}</delete><!--查询图书信息--><select id="queryBook" resultType="com.wfit.boot.model.Book">select *from t_book</select>
</mapper>
12. 项目结构
四、案例测试
1. 测试新增图书信息
2. 测试修改图书信息
3. 测试删除图书信息
4. 测试查询图书信息