基于SpringBoot的SSMP整合案例
业务层开发
- Service层接口定义与数据层接口定义具有较大区别,不要混用;
- 制作测试类测试Service功能是否有效。
selectByUserNameAndPassword(String username,String password); // 数据层
login(String username,String password); // 业务层
步骤
1.接口定义
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);
}
2.实现类定义
// @Service 将该类定义为业务层对应的Bean
@Service
public class BookServiceImpl implements BookService {
@Autowired
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<Book>(currentPage,pageSize);
bookDao.selectPage(page,null);
return page;
}
}
3.测试类定义
@SpringBootTest
public class BookServiceTestCase {
@Autowired
private BookService bookService;
@Test
void testGetById(){
System.out.println(bookService.getById(4));
}
@Test
void testGetPage(){
IPage<Book> page = bookService.getPage(1, 2);
System.out.println(page.getCurrent());
System.out.println(page.getSize());
System.out.println(page.getPages());
System.out.println(page.getTotal());
System.out.println(page.getRecords());
}
}
业务层快速开发
- 使用MyBatisPlus提供有业务层通用接口(IService<T>)与业务层通用实现类(ServiceImpl<M,T>)
- 在通用类基础上做功能重载或功能追加
- 注意重载时不要覆盖原始操作,避免原始提供的功能丢失
接口定义:
public interface IBookService extends IService<Book> {
// 可以追加操作
}
实现类定义:
@Service
public class BookServiceImpl extends ServiceImpl<BookDao, Book> implements IBookService {
// 可以实现接口中追加的操作
}
表现层开发
- 基于Restful进行表现层接口开发
- 使用Postman测试表现层接口功能
@RestController // 标志位Controller类
@RequestMapping("/books") // 定义好映射路径
public class BookController {
@Autowired
private IBookService bookService;
@GetMapping
public List<Book> getAll(){
return bookService.list();
}
@GetMapping("{id}")
public Book getById(@PathVariable Integer id){
return bookService.getById(id);
}
@PostMapping
public Boolean save(@RequestBody Book book){ // 增
// save 操作使用异步提交方式的话,是通过请求体传json数据
return bookService.save(book);
}
@PutMapping
public Boolean update(@RequestBody Book book){ // 改
return bookService.updateById(book);
}
@GetMapping("{currentPage}/{pageSize}")
public IPage<Book> getPage(@PathVariable int currentPage,@PathVariable int pageSize){
return bookService.getPage(currentPage,pageSize);
}
}
1.基于Restful制作表现层接口
- 新增:POST
- 删除:DELETE
- 修改:PUT
- 查询:GET
2.接受参数
- 实体数据:@RequestBody
- 路径变量:@PathVariable
表现层一致性处理
需统一格式
设计表现层返回结果的模型类,用于后端与前端进行数据格式统一,也称为前后端数据协议
@Data
public class R {
private Boolean flag;
private Object data;
public R(){}
public R(Boolean flag){
this.flag = flag;
}
public R(Boolean flag,Object data){
this.flag = flag;
this.data = data;
}
}
表现层接口统一返回值类型结果
@RestController
@RequestMapping("/books")
public class BookController {
@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){ // 增
// save 操作使用异步提交方式的话,是通过请求体传json数据
// R r = new R();
// boolean flag = bookService.save(book);
// r.setFlag(flag);
return new R(bookService.save(book));
}
@PutMapping
public R update(@RequestBody Book book){ // 改
return new R(bookService.updateById(book));
}
@DeleteMapping("{id}")
public R delete(@PathVariable Integer id){ // 改
return new R(bookService.delete(id));
}
@GetMapping("{currentPage}/{pageSize}")
public R getPage(@PathVariable int currentPage,@PathVariable int pageSize){
return new R(true,bookService.getPage(currentPage,pageSize));
}
}
1.设计统一的返回值结果类型便于前端开发读取数据
2.返回值结果类型可以根据需求自行设定,没有固定格式
3.返回值结果模型类用于后端与前端进行数据格式统一,也称为前后端数据协议
前后端协议联调
前后端分离结构设计中页面归属前端服务器
单体工程中页面放置在resource目录下的static目录中(建议执行clean)
整个应用写在一个项目里是单体应用,微服务是多个项目组成一个应用
异常消息处理
后台代码BUG导致数据格式不统一性
解决:对异常进行统一的格式处理,出现异常后,返回指定信息
//作为springmvc的异常处理器(数组层和业务层的异常最终都会抛到表现层来的,所以只需对表现层做处理即可)
//@ControllerAdvice
@RestControllerAdvice
public class ProjectExceptionAdvice {
// 拦截指定的异常信息
@ExceptionHandler(Exception.class)
public R doException(Exception ex){
//记录日志
//通知运维
//通知开发
ex.printStackTrace();
return new R("服务器故障,请稍后再试!");
}
}
删除功能维护
对查询结果进行校验,如果当前页码值大于总页码值,使用最大页码值作为当前页码值重新进行查询。
@GetMapping("{currentPage}/{pageSize}")
public R getPage(@PathVariable int currentPage,@PathVariable int pageSize){
IPage<Book> page = bookService.getPage(currentPage, pageSize);
// 如果当前页码值大于总页码值,那么重新执行查询操作,使用最大页码值作为当前页码值
if (currentPage > page.getPages()){
page = bookService.getPage((int)page.getPages(),pageSize);
}
return new R(true,page);
}