SSMP综合案例

案例实现方案分析

实体类开发————使用Lombok快速制作实体类

Dao开发————整合MyBatisPlus,制作数据层测试类

Service开发————基于MyBatisPlus进行增量开发,制作业务层测试类

Controller开发————基于Restful开发,使用PostMan测试接口功能

Controller开发————前后端开发协议制作

页面开发————基于VUE+ElementUI制作,前后端联调,页面数据处理,页面消息处理

列表、新增、修改、删除、分页、查询

项目异常处理

按条件查询————页面功能调整、Controller修正功能、Service修正功能

1.模块创建

  1. 勾选SpringMVC与MySQL坐标

  1. 修改配置文件为yml格式

  1. 设置端口为80方便访问

2.实体类开发

Lombok,一个Java类库,提供了一组注解,简化POJO实体类开发

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency

lombok版本由SpringBoot提供,无需指定版本

常用注解:@Data

@Data
public class Book {
private Integer id;
private String type;
private String name;
private String description;
}

为当前实体类在编译期设置对应的get/set方法,toString方法,hashCode方法,equals方法等

3.数据层面开发

使用技术:MyBatis-plus+Druid

1.导入相对应的MyBatisPlus与Druid对应的starter

<!--mybatis-plus-->
<dependency>
   <groupId>com.baomidou</groupId>
   <artifactId>mybatis-plus-boot-starter</artifactId>
   <version>3.4.3</version>
</dependency>
<!--druid连接池-->
<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid-spring-boot-starter</artifactId>
   <version>1.2.16</version>
</dependency>

2.配置数据源与MyBatisPlus对应的基础配置(id生成策略使用数据库自增策略)

#配置端口号
server:
  port: 80
#配置数据源druid
spring:
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/ssm_db?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
      username: root
      password: 123456
#mp的配置
mybatis-plus:
  global-config:
    db-config:
      table-prefix: tbl_
      #数据库id的自增策略
      id-type: auto
  #配置日志
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

继承BaseMapper并指定泛型

@Mapper
public interface BookDao extends BaseMapper<Book> {
​
}

制作测试

@SpringBootTest
public class BookTest {
    @Autowired
    private BookDao bookDao;
​
    @Test
    public void testById(){
        Book byId = bookDao.selectById(2);
        System.out.println(byId);
    }
​
    @Test
    public void testSave(){
        Book book=new Book();
        book.setName("明朝那些事");
        book.setType("有关历史");
        book.setDescription("nib");
        bookDao.insert(book);
​
    }
​
    @Test
    public void testDelete(){
        bookDao.deleteById(15);
    }
​
    @Test
    public void testUpdata(){
        Book book=new Book();
        book.setId(16);
        book.setName("明朝那些事");
        book.setType("有关历史");
        book.setDescription("nibglss");
        bookDao.updateById(book);
    }
​
    @Test
    public void testGetAll(){
        System.out.println( bookDao.selectList(null));
    }
​
    @Test
    public void testGetPage(){
        //需要拦截器追加sql分页的sql语句
        /*
        * 参数1:当前是第几页
        * 参数2:每页显示的数据条数
        * */
        IPage page=new Page(2,5);
        bookDao.selectPage(page,null);
        System.out.println(page.getCurrent());
        System.out.println(page.getPages());
        System.out.println(page.getSize());
        System.out.println(page.getRecords());
        System.out.println(page.getTotal());
    }
    //按条件查询
    @Test
    public void testGetByCondition1(){
​
        QueryWrapper<Book> queryWrapper=new QueryWrapper<Book>();
        queryWrapper.like("name","spring");//select*from tbl_book where name like%spring%
        System.out.println( bookDao.selectList(queryWrapper));
​
    }
//使用QueryWrapper对象封装查询条件,推荐使用LambdaQueryWrapper对象,所有查询操作封装成方法调用,支持动态拼写查询条件
    @Test
    public void testGetByCondition2(){
        String name="Spring";
        LambdaQueryWrapper<Book> queryWrapper=new LambdaQueryWrapper<Book>();
        //select*from tbl_book where name like%spring%
        queryWrapper.like(name!=null,Book::getName,name);//select*from tbl_book where name like%spring%
        System.out.println( bookDao.selectList(queryWrapper));
​
    }
​
​
}

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

@Configuration//用于指定该类是spring配置类,创建容器时会从该类加载注解
public class MPConfig {
    @Bean//标注该方法的返回值会存储到Spring容器中
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        //拦截器的外壳
        MybatisPlusInterceptor mybatisPlusInterceptor=new MybatisPlusInterceptor();
//        用什么拦截器,加载什么拦截器
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return mybatisPlusInterceptor;
    }
}

4.业务开发层

定义接口

public interface BookService {
    boolean save(Book book);
    boolean delete(Integer id);
    boolean update(Book book);
    Book getById(Integer id);
    List<Book> getAll();
    IPage<Book> getByPage(int currentPage,int pageSize);
}

实现层定义

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao bookDao;
    public Boolean save(Book book) {
        return bookDao.insert(book) > 0;
    }
    public Boolean delete(Integer id) {
        return bookDao.deleteById(id) > 0;
    }
    public Boolean update(Book book) {
        return bookDao.updateById(book) > 0;
    }
}

测试类定义

@SpringBootTest
public class BookServiceTest {
    @Autowired
    private BookService bookService;
    @Test
    void testGetById(){
        bookService.getById(9);
    }
    @Test
    void testGetAll(){
        bookService.getAll();
    }
    @Test
    void testGetByPage(){
        bookService.getByPage(1,5);
    }
​
}

5.业务层———快速开发

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

@Service
public class IBookServiceImpl extends ServiceImpl<BookDao,Book> implements IBookService {
​
}

测试

@SpringBootTest
public class BookServiceTest2 {
    @Autowired
    private IBookServiceImpl bookService;
    @Test
    public void getById(){
        System.out.println( bookService.getById(1));
    }
    @Test
    public void selectAll(){
        System.out.println(bookService.list());
    }
    @Test
    public void delete(){
        bookService.removeById(16);
    }
    @Test
    public void testSave(){
        Book book=new Book();
        book.setName("大名王朝");
        book.setType("有关历史");
        book.setDescription("nib");
        bookService.save(book);
​
    }
    @Test
    public void testUpdata(){
        Book book=new Book();
        book.setId(14);
        book.setName("明朝那些事");
        book.setType("有关历史");
        book.setDescription("nibglss");
        bookService.updateById(book);
    }
    @Test
    public void testGetByPage(){
        IPage<Book> page = new Page<Book>(1,5);
        IPage<Book> byPage = bookService.page(page);
        List<Book> records = byPage.getRecords();
        System.out.println(records);
    }
    
}

总结:

  1. 使用通用接口(ISerivce<T>)快速开发Service

  1. 使用通用实现类(ServiceImpl<M,T>)快速开发ServiceImpl

  1. 可以在通用接口基础上做功能重载或功能追加

  1. 注意重载时不要覆盖原始操作,避免原始提供的功能丢失

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值