基于SpringBoot的SSMP整合(数据层)https://blog.csdn.net/weixin_51882166/article/details/128693739?spm=1001.2014.3001.5502
标准CRUD
Service层接口定义与数据层接口定义具有较大差距。
定义Service接口:
package com.example.ssmp_demo.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.ssmp_demo.domain.Book;
import java.util.List;
public interface BookService {
Boolean save(Book book);
Boolean update(Book book);
Boolean delete(Integer id);
Book getById(Integer id);
List<Book> getAll();
IPage<Book>getPage(int currentPage,int pageSize);
}
接口写完定义实现类:
package com.example.ssmp_demo.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ssmp_demo.dao.BookDao;
import com.example.ssmp_demo.domain.Book;
import com.example.ssmp_demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import java.io.*;
@Service//定义成业务层bean
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
@Override
public Boolean save(Book book) {
return bookDao.insert(book) > 0;
}
@Override
public Boolean update(Book book) {
return bookDao.updateById(book) >0;
}
@Override
public Boolean delete(Integer id) {
return bookDao.deleteById(id) > 0;
}
@Override
public Book getById(Integer id) {
return bookDao.selectById(id);
}
@Override
public List<Book> getAll() {
return bookDao.selectList(null);
}
@Override
public IPage<Book> getPage(int currentPage, int pageSize) {
IPage page = new Page(currentPage, pageSize);
return bookDao.selectPage(page, null);
}
}
MyBatisPlus快速开发
定义接口:
package com.example.ssmp_demo.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.ssmp_demo.domain.Book;
public interface IBookService extends IService<Book> {
}
定义实现类:
package com.example.ssmp_demo.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ssmp_demo.dao.BookDao;
import com.example.ssmp_demo.domain.Book;
import com.example.ssmp_demo.service.BookService;
import com.example.ssmp_demo.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service//定义成业务层bean
public class BookServiceImpl extends ServiceImpl<BookDao,Book>implements IBookService {
}
测试:
package com.example.ssmp_demo.dao.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ssmp_demo.domain.Book;
import com.example.ssmp_demo.service.IBookService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class test_demo {
@Autowired
private IBookService bookService;
@Test
void test_getById(){
System.out.println(bookService.getById(4));
}
@Test
void test_save(){
Book b=new Book();
b.setId(11);
b.setName("n11");
b.setDescription("d11");
b.setType("t11");
bookService.save(b);
}
@Test
void test_getAll(){
bookService.list();
}
@Test
void delete(){
bookService.removeById(4);
}
@Test
void test_page(){
IPage<Book>page=new Page<Book>(1,4);
bookService.page(page);
}
}
原始数据集:
保存操作:
通过Id得到数据:
分页操作:
第一页前四行数据。
所有数据:
如果在业务层接口,继续添加方法,注意写@Override查看是否报错,如果不报错就是命名相同,会把之前的方法给覆盖掉。
快速开发
使用MyBatisPlus提供有业务层通用接口(IService<T>)与业务层通用实现类(ServiceImp<M,T>)
在通用类基础上做功能重载或功能追加
注意重载不要覆盖元素操作,避免原始提供功能丢失。
表现层标准开发
创建包和类:
注意项目结构。
package com.example.ssmp_demo.controller;
import com.example.ssmp_demo.domain.Book;
import com.example.ssmp_demo.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;
import java.io.*;
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired //自动装配
private IBookService bookService;//调用业务层
@GetMapping//查询是GET请求
public List<Book>getAll(){
return bookService.list();
}
}
启动Application:
服务器启动后,使用postman发送get请求:
得到所有数据。
将剩余方法放入Controller:
package com.example.ssmp_demo.controller;
import com.example.ssmp_demo.domain.Book;
import com.example.ssmp_demo.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired //自动装配
private IBookService bookService;//调用业务层
@GetMapping//查询是GET请求
public List<Book>getAll(){
return bookService.list();
}
@PostMapping
public Boolean save(@RequestBody Book book){
return bookService.save(book);
}
@PutMapping
public Boolean update(@RequestBody Book book){
return bookService.saveOrUpdate(book);
}
@GetMapping("{id}")
public Book getById(@PathVariable Integer id){
return bookService.getById(id);
}
@DeleteMapping("{id}")
public Boolean delete(@PathVariable Integer id){
return bookService.removeById(id);
}
}
后期也可以在Conroller里添加方法。
使用Postman测试:
添加使用POST,并且使用请求体:
执行结果为true,查询所有,判断是否添加。
存在,说明添加成功。
测试删除,删除是通过路径传参,最后通过get查询是否存在。
无内容说明删除成功。
增添分页操作,在接口里添加方法:
package com.example.ssmp_demo.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.ssmp_demo.domain.Book;
public interface IBookService extends IService<Book> {
IPage<Book> getPage(int currentPage, int pageSize);
}
在其实现类里实现:
package com.example.ssmp_demo.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ssmp_demo.dao.BookDao;
import com.example.ssmp_demo.domain.Book;
import com.example.ssmp_demo.service.BookService;
import com.example.ssmp_demo.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service//定义成业务层bean
public class BookServiceImpl extends ServiceImpl<BookDao,Book>implements IBookService {
@Autowired
private BookDao bookDao;
@Override
public IPage<Book> getPage(int currentPage, int pageSize) {
IPage<Book> page = new Page(currentPage, pageSize);
bookDao.selectPage(page,null);
return page;
}
}
在原有Controller里添加方法:
@GetMapping("{currentPage}/{pageSize}")
public IPage<Book> getPage(@PathVariable int currentPage, @PathVariable int pageSize){
return bookService.getPage(currentPage, pageSize);
}
在PostMan里测试查询第2页三条数据:
新增:POST
删除:DELETE
修改:PUT
查询:GET
接收参数:
实体数据:@RequestBody
路径变量:@PathVariable
表现层数据一致性处理
由于增删改查返回的数据不一致,需要统一格式,用于前后端数据格式统一,也称为前后端数据协议。
新建一个工具包和类:
package com.example.ssmp_demo.controller.utils;
import lombok.Data;
@Data
public class R {
private Boolean flag;//操作是否完成
private Object data;//适用于任何数据
public R(Boolean flag){
this.flag = flag;
}
public R(){}
public R(Boolean flag,Object data){
this.flag = flag;
this.data = data;
}
}
然后再新建一个Controller:
package com.example.ssmp_demo.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.ssmp_demo.controller.utils.R;
import com.example.ssmp_demo.domain.Book;
import com.example.ssmp_demo.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;
import java.io.*;
@RestController
@RequestMapping("/books")
public class BookController_demo {
@Autowired //自动装配
private IBookService bookService;//调用业务层
@GetMapping//查询是GET请求
public R getAll(){
return new R(true,bookService.list());
}
@PostMapping
public R save(@RequestBody Book book){
R r=new R(bookService.save(book));
return r;
}
@PutMapping
public R update(@RequestBody Book book){
R r=new R(bookService.saveOrUpdate(book));
return r;
}
@GetMapping("{id}")
public R getById(@PathVariable Integer id){
return new R(true,bookService.getById(id));
}
@DeleteMapping("{id}")
public R delete(@PathVariable Integer id){
R r=new R(bookService.removeById(id));
return r;
}
@GetMapping("{currentPage}/{pageSize}")
public R getPage(@PathVariable int currentPage, @PathVariable int pageSize){
return new R(true,bookService.getPage(currentPage, pageSize));
}
}
启动之前注意把之前的Controller去掉,或更改RequestMapping路径.。
查询全部:
分页,1页两数据:
剩余操作可自行测试。