• [黑马程序员SpringBoot2]——基础篇2


    目录:

    1. 模块创建
    2. 实体类快速开发(lombok)
    3. 数据层标准开发(基础CRUD)
    4. 开启MP运行日志
    5. 分页
    6. 数据层标准开发(条件查询)
    7. 业务层标准开发(基础CRUD)
    8. 业务层标准开发(基于MyBatisPlus构建)
    9. 表现层标准开发
    10. 表现层数据一致性处理(R对象)
    11. 前后端调用(axios发送异步请求)
    12. 列表功能
    13. 添加功能
    14. 删除功能
    15. 修改功能
    16. 异常消息处理
    17. 分页
    18. 条件查询
    19. 基础篇完结

    1.模块创建

    pom.xml(把相应的依赖下载好)

    1. "1.0" encoding="UTF-8"?>
    2. "http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. 4.0.0
    5. org.springframework.boot
    6. spring-boot-starter-parent
    7. 3.1.5
    8. com.example
    9. _20231024_ssmp
    10. 0.0.1-SNAPSHOT
    11. _20231024_ssmp
    12. _20231024_ssmp
    13. 17
    14. org.springframework.boot
    15. spring-boot-starter-web
    16. com.mysql
    17. mysql-connector-j
    18. runtime
    19. org.projectlombok
    20. lombok
    21. true
    22. org.springframework.boot
    23. spring-boot-starter-test
    24. test
    25. com.baomidou
    26. mybatis-plus-boot-starter
    27. 3.5.2
    28. com.alibaba
    29. druid-spring-boot-starter
    30. 1.2.9
    31. org.springframework.boot
    32. spring-boot-maven-plugin
    33. org.projectlombok
    34. lombok

    2.实体类快速开发(lombok)

     

    Book.class

    1. package com.example.domain;
    2. import lombok.Data;
    3. @Data
    4. public class Book {
    5. private Integer id;
    6. private String type;
    7. private String name;
    8. private String description;
    9. }

     小结:

    • 实体类制作
    • 使用lombok简化开发
      • 导入lombok无需指定版本,由SpringBoot提供版本
      • Data注解

     3.数据层标准开发(基础CRUD)

    技术实现方案

    • MyBatisPlus
    • Druid 

    导入MyBatisPlus与Druid对应的starter

     继承BaseMapper并指定泛型

    制作测试类测试结果


    BookDao.interface

    1. package com.example.dao;
    2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    3. import com.example.domain.Book;
    4. import org.apache.ibatis.annotations.Mapper;
    5. @Mapper
    6. public interface BookDao extends BaseMapper {
    7. }

     Book.class

    1. package com.example.domain;
    2. import lombok.Data;
    3. @Data
    4. public class Book {
    5. private Integer id;
    6. private String type;
    7. private String name;
    8. private String description;
    9. }

     application.yml

    1. server:
    2. port: 80
    3. spring:
    4. datasource:
    5. druid:
    6. driver-class-name: com.mysql.cj.jdbc.Driver
    7. url: jdbc:mysql://localhost:3308/test_db
    8. username: root
    9. password: 666666
    10. mybatis-plus:
    11. global-config:
    12. db-config:
    13. table-prefix: tbl_
    14. id-type: auto

    BookDaoTest

    1. package com.example.dao;
    2. import com.example.domain.Book;
    3. import org.junit.jupiter.api.Test;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.boot.test.context.SpringBootTest;
    6. @SpringBootTest
    7. class BookDaoTest {
    8. @Autowired
    9. public BookDao bookDao;
    10. @Test
    11. void testGetById() {
    12. System.out.println(bookDao.selectById(1));
    13. }
    14. @Test
    15. void testSave() {
    16. Book book = new Book();
    17. book.setType("测试数据123");
    18. book.setName("测试数据123");
    19. book.setDescription("测试数据123");
    20. int ret = bookDao.insert(book);
    21. if (ret == 0) {
    22. System.out.println("新增失败!");
    23. } else {
    24. System.out.println("新增成功!");
    25. }
    26. }
    27. @Test
    28. void testUpdate() {
    29. Book book = new Book();
    30. book.setId(13);
    31. book.setType("aaaa");
    32. book.setName("aaaa");
    33. book.setDescription("aaaa");
    34. int ret = bookDao.updateById(book);
    35. if (ret == 0) {
    36. System.out.println("修改失败!");
    37. } else {
    38. System.out.println("修改成功!");
    39. }
    40. }
    41. @Test
    42. void testDelete() {
    43. int ret = bookDao.deleteById(13);
    44. if (ret == 0) {
    45. System.out.println("删除失败!");
    46. } else {
    47. System.out.println("删除成功!");
    48. }
    49. }
    50. @Test
    51. void testGetAll() {
    52. System.out.println(bookDao.selectList(null));
    53. }
    54. }

    运行结果:

    小结: 

    1. 手工导入starter坐标(2个)
    2. 配置数据源与MyBatisPlus对应的配置
    3. 开发Dao接口(继承BaseMapper)
    4. 制作测试类测试Dao功能是否有效 

    4.开启MP运行日志 

    application.yml

    1. server:
    2. port: 80
    3. spring:
    4. datasource:
    5. druid:
    6. driver-class-name: com.mysql.cj.jdbc.Driver
    7. url: jdbc:mysql://localhost:3308/test_db
    8. username: root
    9. password: 666666
    10. mybatis-plus:
    11. global-config:
    12. db-config:
    13. table-prefix: tbl_
    14. id-type: auto
    15. configuration:
    16. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

     运行结果:

    为方便调试可以开启MyBatisPlus的日志
     

    小结:

    • 使用配置方式开启日志,设置日志输出方式为标准输出

    5.分页 

    pom.xml

    1. "1.0" encoding="UTF-8"?>
    2. "http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. 4.0.0
    5. org.springframework.boot
    6. spring-boot-starter-parent
    7. 2.7.18-SNAPSHOT
    8. com.example
    9. _20231024_ssmp1
    10. 0.0.1-SNAPSHOT
    11. _20231024_ssmp1
    12. _20231024_ssmp1
    13. 1.8
    14. org.springframework.boot
    15. spring-boot-starter-web
    16. com.mysql
    17. mysql-connector-j
    18. runtime
    19. org.projectlombok
    20. lombok
    21. true
    22. org.springframework.boot
    23. spring-boot-starter-test
    24. test
    25. com.baomidou
    26. mybatis-plus-boot-starter
    27. 3.5.2
    28. com.alibaba
    29. druid-spring-boot-starter
    30. 1.2.9
    31. org.springframework.boot
    32. spring-boot-maven-plugin
    33. org.projectlombok
    34. lombok
    35. <id>spring-milestones
    36. Spring Milestones
    37. https://repo.spring.io/milestone
    38. false
    39. <id>spring-snapshots
    40. Spring Snapshots
    41. https://repo.spring.io/snapshot
    42. false
    43. <id>spring-milestones
    44. Spring Milestones
    45. https://repo.spring.io/milestone
    46. false
    47. <id>spring-snapshots
    48. Spring Snapshots
    49. https://repo.spring.io/snapshot
    50. false

    MPConfig.class

    1. package com.example.config;
    2. import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
    3. import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
    4. import org.springframework.context.annotation.Bean;
    5. import org.springframework.context.annotation.Configuration;
    6. @Configuration
    7. public class MPConfig {
    8. @Bean
    9. public MybatisPlusInterceptor mybatisPlusInterceptor() {
    10. MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    11. interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
    12. return interceptor;
    13. }
    14. }

     BookDao.interface

    1. package com.example.dao;
    2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    3. import com.example.domain.Book;
    4. import org.apache.ibatis.annotations.Mapper;
    5. @Mapper
    6. public interface BookDao extends BaseMapper {
    7. }

    Book.class

    1. package com.example.domain;
    2. import lombok.Data;
    3. @Data
    4. public class Book {
    5. private Integer id;
    6. private String type;
    7. private String name;
    8. private String description;
    9. }

    applicaion.yml

    1. server:
    2. port: 80
    3. spring:
    4. datasource:
    5. druid:
    6. driver-class-name: com.mysql.cj.jdbc.Driver
    7. url: jdbc:mysql://localhost:3308/test_db
    8. username: root
    9. password: 666666
    10. mybatis-plus:
    11. global-config:
    12. db-config:
    13. table-prefix: tbl_
    14. id-type: auto
    15. configuration:
    16. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

    BookDaoTest

    1. package com.example.dao;
    2. import com.baomidou.mybatisplus.core.metadata.IPage;
    3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    4. import com.example.domain.Book;
    5. import org.junit.jupiter.api.Test;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.boot.test.context.SpringBootTest;
    8. @SpringBootTest
    9. class BookDaoTest {
    10. @Autowired
    11. public BookDao bookDao;
    12. @Test
    13. void testGetById() {
    14. System.out.println(bookDao.selectById(1));
    15. }
    16. @Test
    17. void testSave() {
    18. Book book = new Book();
    19. book.setType("测试数据123");
    20. book.setName("测试数据123");
    21. book.setDescription("测试数据123");
    22. int ret = bookDao.insert(book);
    23. if (ret == 0) {
    24. System.out.println("新增失败!");
    25. } else {
    26. System.out.println("新增成功!");
    27. }
    28. }
    29. @Test
    30. void testUpdate() {
    31. Book book = new Book();
    32. book.setId(13);
    33. book.setType("aaaa");
    34. book.setName("aaaa");
    35. book.setDescription("aaaa");
    36. int ret = bookDao.updateById(book);
    37. if (ret == 0) {
    38. System.out.println("修改失败!");
    39. } else {
    40. System.out.println("修改成功!");
    41. }
    42. }
    43. @Test
    44. void testDelete() {
    45. int ret = bookDao.deleteById(13);
    46. if (ret == 0) {
    47. System.out.println("删除失败!");
    48. } else {
    49. System.out.println("删除成功!");
    50. }
    51. }
    52. @Test
    53. void testGetAll() {
    54. System.out.println(bookDao.selectList(null));
    55. }
    56. @Test
    57. void testGetPage() {
    58. IPage page = new Page(2, 5);
    59. bookDao.selectPage(page, null);
    60. System.out.println(page.getCurrent());
    61. System.out.println(page.getSize());
    62. System.out.println(page.getTotal());
    63. System.out.println(page.getPages());
    64. System.out.println(page.getRecords());
    65. }
    66. }

    项目结构:

    运行结果:

    分页操作需要设定分页对象IPage


    IPage对象中封装了分页操作中的所有数据

    • 数据
    • 当前页码值
    • 每页数据总量
    • 最大页码值
    • 数据总量

    分页操作是在MyBatisPlus的常规操作基础上增强得到,内部是动态的拼写SQL语句,因此需要增强对应的功能,使用MyBatisPlus拦截器实现

    小结: 

    1. 使用IPage封装分页数据
    2. 分页操作依赖MyBatisPlus分页拦截器实现功能
    3. 借助MyBatisPlus日志查阅执行sQL语句

    6.数据层标准开发(条件查询) 

    使用QueryWrapper对象封装查询条件,推荐使用LambdaQueryWrapper对象,所有查询操作封装成方法调用

    支持动态拼写查询条件
     

    代码示例:

    BookDaoTest

    1. package com.example.dao;
    2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    4. import com.baomidou.mybatisplus.core.metadata.IPage;
    5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    6. import com.example.domain.Book;
    7. import org.junit.jupiter.api.Test;
    8. import org.springframework.beans.factory.annotation.Autowired;
    9. import org.springframework.boot.test.context.SpringBootTest;
    10. @SpringBootTest
    11. class BookDaoTest {
    12. @Autowired
    13. public BookDao bookDao;
    14. @Test
    15. void testGetById() {
    16. System.out.println(bookDao.selectById(1));
    17. }
    18. @Test
    19. void testSave() {
    20. Book book = new Book();
    21. book.setType("测试数据123");
    22. book.setName("测试数据123");
    23. book.setDescription("测试数据123");
    24. int ret = bookDao.insert(book);
    25. if (ret == 0) {
    26. System.out.println("新增失败!");
    27. } else {
    28. System.out.println("新增成功!");
    29. }
    30. }
    31. @Test
    32. void testUpdate() {
    33. Book book = new Book();
    34. book.setId(13);
    35. book.setType("aaaa");
    36. book.setName("aaaa");
    37. book.setDescription("aaaa");
    38. int ret = bookDao.updateById(book);
    39. if (ret == 0) {
    40. System.out.println("修改失败!");
    41. } else {
    42. System.out.println("修改成功!");
    43. }
    44. }
    45. @Test
    46. void testDelete() {
    47. int ret = bookDao.deleteById(13);
    48. if (ret == 0) {
    49. System.out.println("删除失败!");
    50. } else {
    51. System.out.println("删除成功!");
    52. }
    53. }
    54. @Test
    55. void testGetAll() {
    56. System.out.println(bookDao.selectList(null));
    57. }
    58. @Test
    59. void testGetPage() {
    60. IPage page = new Page(2, 5);
    61. bookDao.selectPage(page, null);
    62. System.out.println(page.getCurrent());
    63. System.out.println(page.getSize());
    64. System.out.println(page.getTotal());
    65. System.out.println(page.getPages());
    66. System.out.println(page.getRecords());
    67. }
    68. @Test
    69. void testGetBy() {
    70. QueryWrapper qw = new QueryWrapper<>();
    71. qw.like("name", "Spring");
    72. bookDao.selectList(qw);
    73. }
    74. @Test
    75. void testGetBy2() {
    76. String name = "Spring";
    77. LambdaQueryWrapper lqw = new LambdaQueryWrapper<>();
    78. lqw.like(name != null, Book::getName, name);
    79. bookDao.selectList(lqw);
    80. }
    81. }

    运行结果:

    小结:

    1. 使用Querywrapper对象封装查询条件
    2. 推荐使用LambdaQuerywrapper对象
    3. 所有查询操作封装成方法调用
    4. 查询条件支持动态条件拼装 

    7.业务层标准开发(基础CRUD) 

     BookService.interface

    1. package com.example.service;
    2. import com.baomidou.mybatisplus.core.metadata.IPage;
    3. import com.example.domain.Book;
    4. import java.util.List;
    5. public interface BookService {
    6. Boolean save(Book book);
    7. Boolean update(Book book);
    8. Boolean delete(Integer id);
    9. Book getById(Integer id);
    10. List getAll();
    11. IPage getPage(int currentPage, int pageSize);
    12. }

    BookServiceImpl.class

    1. package com.example.service.impl;
    2. import com.baomidou.mybatisplus.core.metadata.IPage;
    3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    4. import com.example.dao.BookDao;
    5. import com.example.domain.Book;
    6. import com.example.service.BookService;
    7. import org.springframework.beans.factory.annotation.Autowired;
    8. import org.springframework.stereotype.Service;
    9. import java.util.List;
    10. @Service
    11. public class BookServiceImpl implements BookService {
    12. @Autowired
    13. private BookDao bookDao;
    14. @Override
    15. public Boolean save(Book book) {
    16. return bookDao.insert(book) > 0;
    17. }
    18. @Override
    19. public Boolean update(Book book) {
    20. return bookDao.updateById(book) > 0;
    21. }
    22. @Override
    23. public Boolean delete(Integer id) {
    24. return bookDao.deleteById(id) > 0;
    25. }
    26. @Override
    27. public Book getById(Integer id) {
    28. return bookDao.selectById(id);
    29. }
    30. @Override
    31. public List getAll() {
    32. return bookDao.selectList(null);
    33. }
    34. @Override
    35. public IPage getPage(int currentPage, int pageSize) {
    36. IPage page = new Page(currentPage, pageSize);
    37. return bookDao.selectPage(page, null);
    38. }
    39. }

    BookServiceTest.class

    1. package com.example.service;
    2. import com.baomidou.mybatisplus.core.metadata.IPage;
    3. import com.example.domain.Book;
    4. import org.junit.jupiter.api.Test;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.boot.test.context.SpringBootTest;
    7. @SpringBootTest
    8. class BookServiceTest {
    9. @Autowired
    10. private BookService bookService;
    11. @Test
    12. void save() {
    13. Book book = new Book();
    14. book.setType("测试数据123");
    15. book.setName("测试数据123");
    16. book.setDescription("测试数据123");
    17. Boolean ret = bookService.save(book);
    18. if (ret == false) {
    19. System.out.println("新增失败!");
    20. } else {
    21. System.out.println("新增成功!");
    22. }
    23. }
    24. @Test
    25. void update() {
    26. Book book = new Book();
    27. book.setId(13);
    28. book.setType("aaaa");
    29. book.setName("aaaa");
    30. book.setDescription("aaaa");
    31. Boolean ret = bookService.update(book);
    32. if (ret == false) {
    33. System.out.println("修改失败!");
    34. } else {
    35. System.out.println("修改成功!");
    36. }
    37. }
    38. @Test
    39. void delete() {
    40. Boolean ret = bookService.delete(16);
    41. if (ret == false) {
    42. System.out.println("删除失败!");
    43. } else {
    44. System.out.println("删除成功!");
    45. }
    46. }
    47. @Test
    48. void getById() {
    49. System.out.println(bookService.getById(4));
    50. }
    51. @Test
    52. void getAll() {
    53. System.out.println(bookService.getAll());
    54. }
    55. @Test
    56. void getPage() {
    57. IPage page = bookService.getPage(2, 5);
    58. System.out.println(page.getCurrent());
    59. System.out.println(page.getSize());
    60. System.out.println(page.getTotal());
    61. System.out.println(page.getPages());
    62. System.out.println(page.getRecords());
    63. }
    64. }

    项目结构:

    Service层接口定义与数据层接口定义具有较大区别,不要混用

    • selectByUserNameAndPassword( String username, string password );
    • login(String username , string password ) ;

    接口定义


    实现类定义

    测试类定义
     

    小结:

    • Service接口名称定义成业务名称,并与Dao接口名称进行区分
    • 制作测试类测试service功能是否有效

    8.业务层标准开发(基于MyBatisPlus构建) 

    IBookService.class

    1. package com.example.service;
    2. import com.baomidou.mybatisplus.extension.service.IService;
    3. import com.example.domain.Book;
    4. public interface IBookService extends IService {
    5. boolean insert(Book book);
    6. boolean modify(Book book);
    7. boolean delete(Integer id);
    8. Book get(Integer id);
    9. }

    BookServiceImpl1.class

    1. package com.example.service.impl;
    2. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    3. import com.example.dao.BookDao;
    4. import com.example.domain.Book;
    5. import com.example.service.IBookService;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.stereotype.Service;
    8. @Service
    9. public class BookServiceImpl1 extends ServiceImpl implements IBookService {
    10. @Autowired
    11. private BookDao bookDao;
    12. @Override
    13. public boolean insert(Book book) {
    14. return bookDao.insert(book) > 0;
    15. }
    16. @Override
    17. public boolean modify(Book book) {
    18. return bookDao.updateById(book) > 0;
    19. }
    20. @Override
    21. public boolean delete(Integer id) {
    22. return bookDao.deleteById(id) > 0;
    23. }
    24. @Override
    25. public Book get(Integer id) {
    26. return bookDao.selectById(id);
    27. }
    28. }

    IBookServiceTest.class

    1. package com.example.service;
    2. import com.baomidou.mybatisplus.core.metadata.IPage;
    3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    4. import com.example.domain.Book;
    5. import org.junit.jupiter.api.Test;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.boot.test.context.SpringBootTest;
    8. @SpringBootTest
    9. class IBookServiceTest {
    10. @Autowired
    11. private IBookService bookService;
    12. @Test
    13. void save() {
    14. Book book = new Book();
    15. book.setType("测试数据123");
    16. book.setName("测试数据123");
    17. book.setDescription("测试数据123");
    18. Boolean ret = bookService.save(book);
    19. if (ret == false) {
    20. System.out.println("新增失败!");
    21. } else {
    22. System.out.println("新增成功!");
    23. }
    24. }
    25. @Test
    26. void update() {
    27. Book book = new Book();
    28. book.setId(18);
    29. book.setType("aaaa");
    30. book.setName("aaaa");
    31. book.setDescription("aaaa");
    32. Boolean ret = bookService.updateById(book);
    33. if (ret == false) {
    34. System.out.println("修改失败!");
    35. } else {
    36. System.out.println("修改成功!");
    37. }
    38. }
    39. @Test
    40. void delete1() {
    41. Boolean ret = bookService.removeById(17);
    42. if (ret == false) {
    43. System.out.println("删除失败!");
    44. } else {
    45. System.out.println("删除成功!");
    46. }
    47. }
    48. @Test
    49. void getById() {
    50. System.out.println(bookService.getById(4));
    51. }
    52. @Test
    53. void getAll() {
    54. System.out.println(bookService.list());
    55. }
    56. @Test
    57. void getPage() {
    58. IPage page = new Page(2, 5);
    59. bookService.page(page);
    60. System.out.println(page.getCurrent());
    61. System.out.println(page.getSize());
    62. System.out.println(page.getTotal());
    63. System.out.println(page.getPages());
    64. System.out.println(page.getRecords());
    65. }
    66. // ------------------------追加的功能-----------------------------------
    67. @Test
    68. void insert() {
    69. Book book = new Book();
    70. book.setType("测试数据123");
    71. book.setName("测试数据123");
    72. book.setDescription("测试数据123");
    73. Boolean ret = bookService.insert(book);
    74. if (ret == false) {
    75. System.out.println("新增失败!");
    76. } else {
    77. System.out.println("新增成功!");
    78. }
    79. }
    80. @Test
    81. void modify() {
    82. Book book = new Book();
    83. book.setId(18);
    84. book.setType("aaaa");
    85. book.setName("aaaa");
    86. book.setDescription("aaaa");
    87. Boolean ret = bookService.modify(book);
    88. if (ret == false) {
    89. System.out.println("修改失败!");
    90. } else {
    91. System.out.println("修改成功!");
    92. }
    93. }
    94. @Test
    95. void delete() {
    96. Boolean ret = bookService.delete(17);
    97. if (ret == false) {
    98. System.out.println("删除失败!");
    99. } else {
    100. System.out.println("删除成功!");
    101. }
    102. }
    103. @Test
    104. void get() {
    105. System.out.println(bookService.get(4));
    106. }
    107. }

     项目结构:

    快速开发方案

    • 使用MyBatisPlus提供有业务层通用接口(ISerivce)与业务层通用实现类(ServiceImpl)
    • 在通用类基础上做功能重载或功能追加
    • 注意重载时不要覆盖原始操作,避免原始提供的功能丢失

    接口定义


     

    实现类定义


    实现类追加功能


     

    小结:

    1. 使用通用接口(ISerivce)快速开发service
    2. 使用通用实现类(ServiceImpl)快速开发ServiceImpl
    3. 可以在通用接口基础上做功能重载或功能追加
    4. 注意重载时不要覆盖原始操作,避免原始提供的功能丢失 

    9.表现层标准开发 

    getAll

    getById

    delete

    save

    update

    getPage

    BookController.class

    1. package com.example.controller;
    2. import com.baomidou.mybatisplus.core.metadata.IPage;
    3. import com.example.domain.Book;
    4. import com.example.service.IBookService;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.web.bind.annotation.*;
    7. import java.util.List;
    8. @RestController
    9. @RequestMapping("/books")
    10. public class BookController {
    11. @Autowired
    12. private IBookService bookService;
    13. @GetMapping
    14. public List getAll() {
    15. return bookService.list();
    16. }
    17. @PostMapping
    18. public Boolean save(@RequestBody Book book) {
    19. return bookService.save(book);
    20. }
    21. @PutMapping
    22. public Boolean update(@RequestBody Book book) {
    23. return bookService.modify(book);
    24. }
    25. @DeleteMapping("{id}")
    26. public Boolean delete(@PathVariable Integer id) {
    27. return bookService.delete(id);
    28. }
    29. @GetMapping("{id}")
    30. public Book getById(@PathVariable Integer id) {
    31. return bookService.getById(id);
    32. }
    33. @GetMapping("{currentPage}/{pageSize}")
    34. public IPage getPage(@PathVariable int currentPage, @PathVariable int pageSize) {
    35. return bookService.getPage(currentPage, pageSize);
    36. }
    37. }

    IBookService.interface

    1. package com.example.service;
    2. import com.baomidou.mybatisplus.core.metadata.IPage;
    3. import com.baomidou.mybatisplus.extension.service.IService;
    4. import com.example.domain.Book;
    5. public interface IBookService extends IService {
    6. boolean insert(Book book);
    7. boolean modify(Book book);
    8. boolean delete(Integer id);
    9. Book get(Integer id);
    10. IPage getPage(int currentPage, int pageSize);
    11. }

     BookServiceImpl1

    1. package com.example.service.impl;
    2. import com.baomidou.mybatisplus.core.metadata.IPage;
    3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    5. import com.example.dao.BookDao;
    6. import com.example.domain.Book;
    7. import com.example.service.IBookService;
    8. import org.springframework.beans.factory.annotation.Autowired;
    9. import org.springframework.stereotype.Service;
    10. @Service
    11. public class BookServiceImpl1 extends ServiceImpl implements IBookService {
    12. @Autowired
    13. private BookDao bookDao;
    14. @Override
    15. public boolean insert(Book book) {
    16. return bookDao.insert(book) > 0;
    17. }
    18. @Override
    19. public boolean modify(Book book) {
    20. return bookDao.updateById(book) > 0;
    21. }
    22. @Override
    23. public boolean delete(Integer id) {
    24. return bookDao.deleteById(id) > 0;
    25. }
    26. @Override
    27. public Book get(Integer id) {
    28. return bookDao.selectById(id);
    29. }
    30. @Override
    31. public IPage getPage(int currentPage, int pageSize) {
    32. IPage page = new Page(currentPage, pageSize);
    33. bookDao.selectPage(page, null);
    34. return page;
    35. }
    36. }

    项目结构:

    小结:

    • 基于Restfu1制作表现层接口
      • 新增:POST
      • 删除:DELETE
      • 修改:PUT
      • 查询:GET
    • 接收参数
      • 实体数据:@RequestBody
      • 路径变量:@PathVariable 

    10. 表现层数据一致性处理(R对象)

     R.class

    1. package com.example.utils;
    2. import lombok.Data;
    3. @Data
    4. public class R {
    5. private Boolean flag;
    6. private Object data;
    7. public R() {
    8. }
    9. public R(Boolean flag) {
    10. this.flag = flag;
    11. }
    12. public R(Boolean flag, Object data) {
    13. this.flag = flag;
    14. this.data = data;
    15. }
    16. }

     BookController1.class

    1. package com.example.controller;
    2. import com.example.domain.Book;
    3. import com.example.service.IBookService;
    4. import com.example.utils.R;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.web.bind.annotation.*;
    7. @RestController
    8. @RequestMapping("/books")
    9. public class BookController1 {
    10. @Autowired
    11. private IBookService bookService;
    12. @GetMapping
    13. public R getAll() {
    14. return new R(true, bookService.list());
    15. }
    16. @PostMapping
    17. public R save(@RequestBody Book book) {
    18. return new R(bookService.save(book));
    19. }
    20. @PutMapping
    21. public R update(@RequestBody Book book) {
    22. return new R(bookService.modify(book));
    23. }
    24. @DeleteMapping("{id}")
    25. public R delete(@PathVariable Integer id) {
    26. return new R(bookService.delete(id));
    27. }
    28. @GetMapping("{id}")
    29. public R getById(@PathVariable Integer id) {
    30. return new R(true, bookService.getById(id));
    31. }
    32. @GetMapping("{currentPage}/{pageSize}")
    33. public R getPage(@PathVariable int currentPage, @PathVariable int pageSize) {
    34. return new R(true, bookService.getPage(currentPage, pageSize));
    35. }
    36. }

    项目结构:

    postman测试结果:

    getAll

    getById 

     delete

    update

            

    save

    getPage

     设计表现层返回结果的模型类, 用于后端与前端进行数据格式统一,也称为前后端数据协议

    表现层接口统一返回值类型结果


    小结:

    1. 设计统─的返回值结果类型便于前端开发读取数据
    2. 返回值结果类型可以根据需求自行设定,没有固定格式
    3. 返回值结果模型类用于后端与前端进行数据格式统一,也称为前后端数据协议

    11.前后端调用(axios发送异步请求)

    项目结构:(项目地址:JavaEE: 练习的代码 - Gitee.com

    运行结果:

    前后端分离结构设计中页面归属前端服务器
    单体工程中页面放置在resources目录下的static目录中(建议执行clean)

    前端发送异步请求,调用后端接口


    小结:

    • 单体项目中页面放置在resources/static目录下
    • created钩子函数用于初始化页面时发起调用
    • 页面使用axios发送异步请求获取数据后确认前后端是否联通 

    12.列表功能 

     列表页

     运行结果:

    小结:

    • 将查询数据返回到页面,利用前端数据双向绑定进行数据展示

    13.添加功能

    弹出添加窗口


     

    清除数据


     

    添加


     

    取消添加


      

    小结:

    1. 请求方式使用POST调用后台对应操作
    2. 添加操作结束后动态刷新页面加载数据
    3. 根据操作结果不同,显示对应的提示信息
    4. 弹出添加Div时清除表单数据  

    14.删除功能 

    删除

     

    小结:

    1. 请求方式使用Delete调用后台对应操作
    2. 删除操作需要传递当前行数据对应的id值到后台
    3. 删除操作结束后动态刷新页面加载数据
    4. 根据操作结果不同,显示对应的提示信息
    5. 删除操作前弹出提示框避免误操作

    15.修改功能

    弹出修改窗口

    删除消息维护


     

    小结:

    • 加载要修改数据通过传递当前行数据对应的id值到后台查询数据
    • 利用前端数据双向绑定将查询到的数据进行回显

    修改


     

    取消添加和修改


     

    小结:

    • 请求方式使用PUT调用后台对应操作
    • 修改操作结束后动态刷新页面加载数据(同新增)
    • 根据操作结果不同,显示对应的提示信息(同新增) 

    16.异常消息处理

    业务操作成功或失败返回数据格式

    后台代码BUG导致数据格式不统一性

    对异常进行统一处理,出现异常后,返回指定信息


     

    修改表现层返回结果的模型类,封装出现异常后对应的信息

    • flag: false
    • Data: null
    • 消息(msg): 要显示信息

     

    页面消息处理,没有传递消息加载默认消息,传递消息后加载指定消息


     

    可以在表现层Controller中进行消息统一处理


     

    目的:国际化
     

    页面消息处理

     

    小结:

    • 使用注解@RestcontrollerAdvice定义SpringMVC异常处理器用来处理异常的
    • 异常处理器必须被扫描加载,否则无法生效
    • 表现层返回结果的模型类中添加消息属性用来传递消息到页面 

    17.分页 

    页面使用el分页组件添加分页功能

    定义分页组件需要使用的数据并将数据绑定到分页组件


     

    替换查询全部功能为分页功能


     

    分页查询


     

    使用路径参数传递分页数据或封装对象传递数据
     

    加载分页数据


     

    分页页码值切换


     

    小结:

    • 使用el分页组件
    • 定义分页组件绑定的数据模型
    • 异步调用获取分页数据
    • 分页数据页面回显 

    对查询结果进行校验,如果当前页码值大于最大页码值,使用最大页码值作为当前页码值重新查询

     

    小结:

    • 基于业务需求维护删除功能

    18.条件查询 

    查询条件数据封装

    • 单独封装
    • 与分页操作混合封装

     

    页面数据模型绑定

    组织数据成为get请求发送的数据


     

    条件参数组织可以通过条件判定书写的更简洁

    Controller接收参数

    业务层接口功能开发


     

    Controller调用业务层分页条件查询接口


     

    页面回显数据


     

    小结:

    • 定义查询条件数据模型(当前封装到分页数据模型中)
    • 异步调用分页功能并通过请求参数传递数据到后台 

    19.基础篇完结:

     基于SpringBoot的SSMP整合案例

    1. pom. xml                        配置起步依赖
    2. application. yml             设置数据源、端口、框架技术相关配置等
    3. dao                                继承BaseMapper、设置@Mapper
    4. dao测试类
    5. service                           调用数据层接口或MyBatis-Plus提供的接口快速开发
    6. service测试类
    7. controller                         基于Restful开发,使用Postman测试跑通功能
    8. 页面                                放置在resources目录下的static目录中

    项目地址:JavaEE: 练习的代码 - Gitee.com

  • 相关阅读:
    [AUTOSAR][诊断管理][$11] 复位服务
    1、Flowable基础
    微信小程序 - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
    Linux 线程互斥
    JavaWeb开发之——MySQL数据模型(04)
    vue 多环境文件配置(开发,测试,生产)
    后端开发怎么做得更优秀?记住这15个好习惯
    mysql 存储过程详解
    Javascript——js中数组、对象的解构
    案例部署——GlusterFS分布式文件系统群集
  • 原文地址:https://blog.csdn.net/qq_56444564/article/details/133932369