流程如下:
image.png
1,构建springboot项目,手动增加mybatis-plus和druid坐标,略
2,实体类开发
增加lombok坐标
image.png
代码如下:
package com.example.ssmp_001.damain;
import jdk.jfr.DataAmount;
import lombok.Data;
@Data
public class Book {
private Integer id;
private String name;
private Integer age;
private String email;
}
2,数据层开发dao接口:
配置数据库链接方式和mybatis-plus:
image.png image.png
@Mapper
public interface BookDao extends BaseMapper<Book> {
}
//继承BaseMapper,把需要哪个模块泛型传给BaseMapper
3,设置mybatis-plus日志输出方式:
image.png
4,设置分页功能的拦截插件
image.png
代码如下:
@Configuration
public class MPconfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
5,业务层快速开发:
创建业务层接口:
image.png
继承MP的Iservicve,并传入实体类的泛型。
public interface IBookService extends IService<Book> {
}
然后创建业务层的实现类:
image.png
@Service
public class BookServiceImpl extends ServiceImpl<BookDao, Book> implements IBookService {
}
6,表现层开发
image.png image.png@RestController
@RequestMapping("/books")
public class BookControaller2 {
@Autowired //自动装配
private IBookService iBookService;
@GetMapping //设置请求方式
public List<Book> getAll(){ //获取全部数据
return iBookService.list();
}
@PostMapping
public boolean sava(@RequestBody Book book){//保存数据 ,@RequestBody是指讲请求参数放在请求体中
return iBookService.save(book)
}
@DeleteMapping("{id}") //rest风格,简化路径 即 localhost/books/1
public boolean deleteByid(@PathVariable int id){//删除数据 @PathVariable是指路径变量
return iBookService.removeById(id)
}
@PutMapping
public boolean updateByid(@RequestBody int id){//修改数据
return iBookService.updateById(id)
}
@GetMapping("{currentPage}/{pageSize}") //传递2个参数,第几页,以及每页显示的数量
public IPage<Book> getPage(@PathVariable int currentPage,@PathVariable int pageSize){
return iBookService.getPage(currentPage,pageSize);
}
}
分页:
配置类在第四步已经配置
public interface IBookService extends IService<Book> {
IPage<Book> getPage(int currentPage, int pageSize); //需要2个参数,第几页和每页显示的数量
}
实现一下:
image.png
@Service
public class BookServiceImpl extends ServiceImpl<BookDao, Book> implements IBookService {
@Autowired
private BookDao bookDao;
@Override
public IPage<Book> getPage(int currentPage,int pageSize){
IPage page = new Page(currentPage,pageSize);
bookDao.selectPage(page,null);
return page;
}
}
表现层调用即可
image.png
到这里,基本就开发完毕,但是通过调试,可以发现,接口返回值的格式比较乱,需要限定接口返回格式:
1,编写一个返回结果的模型类:
image.png@Data
public class R { //用于变现层返回结果的模型类,用于后端和前端进行数据格式统一,也称为后端数据协议
private Boolean flag;
private Object data;
// public R(Boolean flag,Object data){
// this.data = data;
// this.flag = flag;
// }
public R() {
}
public R(Boolean flag, Object data) {
this.flag = flag;
this.data = data;
}
public R(Boolean flag) {
this.flag = flag;
}
}
2,修改Controaller中的返回值,将返回值设置为模型类的对象
image.png@RestController
@RequestMapping("/books") //rest风格开发标准命名方式
public class BookControaller {
@Autowired
private IBookService bookService;
@GetMapping
public R getAll() {
return new R(true, bookService.list());
}
@GetMapping("{id}")
public R getById(@PathVariable Integer id) {
return new R(true,bookService.getById(id));
}
@PostMapping
public R save(@RequestBody Book book) {
return new R(bookService.save(book));
}
@PutMapping("{id}")
public R updateById(@PathVariable Book book) {
return new R(true,bookService.updateById(book));
}
@DeleteMapping("{id}")
public R deleterById(@PathVariable Integer id){
return new R(true,bookService.removeById(id));
}
}
image.png
网友评论