基于SpringBoot+Mybatis-Plus的快速入门案例
图书管理系统 源码已上传Gitee 源码地址
参考视频 : 黑马程序员SpringBoot
1. 技术栈和环境配置
- SpringBoot 2.6.7
- Mybatis-Plus 3.4.3
- Lombok 1.18
- mysql 5.6
- JDK 8
2.案例效果演示
-
2.1添加书籍 -
2.2 删除书籍 -
2.3 修改书籍 -
2.4 按条件查询书籍 -
2.5分页
3.案例分析
3.1实体类
使用Lombok快速制作实体类
@Data
@TableName("t_book")
public class Books {
private Integer id;
private String type;
private String name;
private String description;
}
3.2数据层
整合MybatisPlus
application.yml文件
server:
port: 80
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db4?serverTimezone=UTC
username: root
password:
mybatis-plus:
global-config:
db-config:
id-type: auto
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
配置MP的分页拦截器
@Configuration
public class MPConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
MySQL数据表——t_book
CREATE TABLE t_book(
id INT NOT NULL AUTO_INCREMENT,
TYPE VARCHAR(20),
NAME VARCHAR(20),
description VARCHAR(20),
PRIMARY KEY (id)
)ENGINE=INNODB DEFAULT CHARSET=utf8;
3.3业务层/服务层
基于MyBatisPlus进行增量开发
IBookService接口类
注:此接口继承MybatisPlus提供的IService接口,默认实现了许多业务操作
public interface IBookService extends IService<Books> {
boolean saveBook(Books books);
boolean modify(Books books);
boolean delete(Integer id);
IPage<Books> getPage(int currentPage, int pageSize);
IPage<Books> getPage(int currentPage, int pageSize,Books books);
}
BookServiceImpl实现类
注:此类继承ServiceImpl<BookDao,Books>类,参数为接口和实体类,代表映射关系
@Service
public class BookServiceImpl extends ServiceImpl<BookDao,Books> implements IBookService {
@Autowired
private BookDao bookDao;
@Override
public boolean saveBook(Books books) {
return bookDao.insert(books) > 0;
}
@Override
public boolean modify(Books books) {
return bookDao.updateById(books) > 0;
}
@Override
public boolean delete(Integer id) {
return bookDao.deleteById(id) > 0;
}
@Override
public IPage<Books> getPage(int currentPage, int pageSize) {
IPage page = new Page(currentPage,pageSize);
bookDao.selectPage(page,null);
return page;
}
@Override
public IPage<Books> getPage(int currentPage, int pageSize, Books books) {
LambdaQueryWrapper<Books> lqw = new LambdaQueryWrapper<>();
lqw.like(Strings.isNotEmpty(books.getType()), Books::getType,books.getType());
lqw.like(Strings.isNotEmpty(books.getName()), Books::getName,books.getName());
lqw.like(Strings.isNotEmpty(books.getDescription()), Books::getDescription,books.getDescription());
IPage page = new Page(currentPage,pageSize);
bookDao.selectPage(page,lqw);
return page;
}
}
3.4表现层/控制层
基于Restful风格
BookController类
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private IBookService bookService;
@GetMapping
public ResultMessage getAll() {
return new ResultMessage(true,bookService.list());
}
@PostMapping
public ResultMessage save(@RequestBody Books books) {
return new ResultMessage(bookService.saveBook(books));
}
@DeleteMapping("{id}")
public ResultMessage delete(@PathVariable Integer id) {
return new ResultMessage(bookService.delete(id));
}
@PutMapping
public ResultMessage update(@RequestBody Books books) {
return new ResultMessage(bookService.modify(books));
}
@GetMapping("{id}")
public ResultMessage getById(@PathVariable Integer id) {
return new ResultMessage(true,bookService.getById(id));
}
@GetMapping("{currentPage}/{pageSize}")
public ResultMessage getPage(@PathVariable int currentPage, @PathVariable int pageSize, Books books) {
IPage<Books> page = bookService.getPage(currentPage, pageSize,books);
if (currentPage > page.getPages()){
page = bookService.getPage((int) page.getPages(),pageSize,books);
}
return new ResultMessage(true, page);
}
}
表现层消息一致性处理,前后端数据协议
ResultMessage类,消息统一处理
@Data
public class ResultMessage {
private Boolean flag;
private Object data;
private String message;
public ResultMessage(){
}
public ResultMessage(Boolean flag){
this.flag = flag;
}
public ResultMessage(Boolean flag, Object data){
this.flag = flag;
this.data = data;
}
public ResultMessage(String message){
this.flag = flag;
this.message = message;
}
public ResultMessage(Boolean flag, String message){
this.flag = flag;
this.message = message;
}
}
异常处理
@RestControllerAdvice
public class ExceptionAdvice {
@ExceptionHandler
public ResultMessage doException(Exception e){
e.printStackTrace();
return new ResultMessage("服务器故障,稍后再试");
}
}
3.5页面开发
基于VUE+ElementUI
页面数据处理、页面消息处理
books.html (部分代码)
<script>
var vue = new Vue({
el: '#app',
data: {
dataList: [],
dialogFormVisible: false,
dialogFormVisible4Edit: false,
formData: {},
rules: {
type: [{required: true, message: '图书类别为必填项', trigger: 'blur'}],
name: [{required: true, message: '图书名称为必填项', trigger: 'blur'}]
},
pagination: {
currentPage: 1,
pageSize: 10,
total: 0,
type:"",
name:"",
description:""
}
},
created() {
this.getAll();
}
})
</script>
|