SpringBoot整合SSMP

SpringBoot整合ssmp案例

  • 案例实现方案分析
    • 实体类开发——使用Lombok快速制作实体类
    • Dao开发——整合MyBatisPlus,制作数据层测试类
    • Service开发——基于MyBatisPlus进行增量开发,制作业务层测试类
    • Controller开发——基于Restful开发,使用PostMan测试接口功能
    • Controller开发——前后端开发协议制作
    • 页面开发——基于VUE+ElementUI制作,前后端联调,页面数据处理,页面消息处理
      • 列表、新增、修改、删除、分页、查询
    • 项目异常处理
    • 按条件查询——页面功能调整、Controller修正功能、Service修正功能
(1)、导入maven坐标
<!--        mybatis-plus手动添加坐标-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>
<!--        lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
<!--        druid-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.16</version>
        </dependency>
(2)、application.yml配置
# druid 数据源配置
spring:
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql:///ssm
      username: root
      password: 1234
      
#mybatis-puls配置
mybatis-plus:
  global-config:
    db-config:
      #设置数据库表的前缀
      table-prefix: tbl_  
      #设置id的增长类型 默认为雪花算法 不设置可能会在save操作中报错id类型不匹配
      id-type: auto 
  #开启MP日志
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl  
(3)、Book实体类(数据层)
package com.itheima.domain;

import lombok.Data;

// lombok 技术
@Data  内置了GetterSetterpublic class Book {
    private Integer id;
    private String type;
    private String name;
    private String description;
}

(4)、BookDao接口类(数据层)
package com.itheima.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.domain.Book;
import org.apache.ibatis.annotations.Mapper;

@Mapper // 使用@Mapper将sDAO接口交给Spring进行管理
public interface BookDao extends BaseMapper<Book> {

}

####(5)、Test测试类(数据层)

package com.itheima.dao;

import com.itheima.SsmpApplication;
import com.itheima.domain.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(classes = {SsmpApplication.class})
public class BookDaoTestCase {
    @Autowired
    private BookDao bookDao;

    // 按Id查询单个
    @Test
    void getById(){
        System.out.println(bookDao.selectById(1));
    }
    // 保存书籍数据
    @Test
    void save(){
        // 没有设Id,在yml文件中设置了id-type:auto 按id属性自增
        Book book = new Book();
        book.setName("java基础");
        book.setType("java");
        book.setDescription("一本讲解java开发的书籍");
        bookDao.insert(book);
    }
    // 修改操作
    @Test
    void update(){
        Book book = new Book();
        book.setId(22);
        book.setName("java基础ac");
        book.setType("java");
        book.setDescription("一本讲解java开发的书籍");
        bookDao.updateById(book);
    }
	// 删除操作
    @Test
    void delete(){
        bookDao.deleteById(22);
        bookDao.deleteById(23);
    }
	// 查询全部
    @Test
    void getAll(){
        for (Book book : bookDao.selectList(null)) {
            System.out.println(book);
        }
    }
    // 分页查询
    @Test
   	void getPage(){
        IPage page = new Page(1,2);
        bookDao.selectPage(page,null);
    }
}
    // 按条件查询
    @Test
    void getBy(){
        QueryWrapper<Book> qw = new QueryWrapper<Book>();
        qw.like("name","Spring");
        bookDao.selectList(qw);
    }
    // Lambda 条件查询
    @Test
    void getBy2(){
        String name = null;
        LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<Book>();
        // 通用写法:判断条件是否为空
        // if(name != null) lqw.like(Book::getName,name);
        // Lambda 写法   
        lqw.like(Strings.isNotEmpty(name),Book::getName,name);
        bookDao.selectList(lqw);
    }
- 分页查询配置类
package com.itheima.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MPConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor Interceptor = new MybatisPlusInterceptor();
        Interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return Interceptor;
    }
}
(6)、Service接口类(业务层)
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.domain.Book;

import java.util.List;

public interface BookService {
    boolean save(Book book);
    boolean delete(Integer id);
    boolean update(Book book);
    Book getById(Integer id);
    List<Book> getAll();

    IPage<Book> selectPage(int current,int size);
}
- 使用MP简写
package com.itheima.service.impl;

import com.baomidou.mybatisplus.extension.service.IService;
import com.itheima.domain.Book;

public interface IBookService extends IService<Book> {
    // 自己增加分页查询
    IPage<Book> page(int current,int size);
}

(7)、Service实现类(业务层)
package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dao.BookDao;
import com.itheima.domain.Book;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao bookDao;
    @Override
    public boolean save(Book book) {
        return bookDao.insert(book)>0;
    }

    @Override
    public boolean delete(Integer id) {
        return bookDao.deleteById(id)>0;
    }

    @Override
    public boolean update(Book book) {
        return bookDao.updateById(book)>0;
    }

    @Override
    public Book getById(Integer id) {
        return bookDao.selectById(id);
    }

    @Override
    public IPage<Book> selectPage(int current, int size) {
        IPage<Book> page = new Page<Book>(current,size);
        IPage<Book> page1 = bookDao.selectPage(page, null);
        return page1;
    }

    @Override
    public List<Book> getAll() {
        return bookDao.selectList(null);
    }
}
- 使用MP简写
package com.itheima.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dao.BookDao;
import com.itheima.domain.Book;
import org.springframework.stereotype.Service;

@Service
public class BookServiceImpl extends ServiceImpl<BookDao, Book> implements IBookService {
    // 自己增加的分页查询
    @Autowired
    private BookDao bookDao;
    @Override
    public IPage<Book> page(int current, int size) {
        IPage<Book> page = new Page<Book>(current,size);
        bookDao.selectPage(page,null);
        return page;
    }
}
(8)、Test测试类(业务层)
package com.itheima.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
package com.itheima.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.SsmpApplication;
import com.itheima.domain.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(classes = {SsmpApplication.class})
public class BookServiceTest {
    @Autowired
    private BookService bookService;
    @Test
    void getById(){
        System.out.println(bookService.getById(4));
    }
    @Test
    void save(){
        Book book = new Book();
        book.setName("java基础");
        book.setType("java");
        book.setDescription("一本讲解java开发的书籍");
        bookService.save(book);
    }
    @Test
    void update(){
        Book book = new Book();
        book.setId(5);
        book.setName("java基础ac");
        book.setType("java");
        book.setDescription("一本讲解java开发的书籍");
        bookService.update(book);
    }

    @Test
    void delete(){
        bookService.delete(7);
    }

    @Test
    void getAll(){
        bookService.getAll();
    }
    @Test
    void getPage(){
        IPage<Book> page = bookService.selectPage(1, 2);
        System.out.println(page.getSize());
        System.out.println(page.getPages());
        System.out.println(page.getCurrent());
        System.out.println(page.getTotal());
    }
}

####(9)、Controller(表现层)

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.domain.Book;
import com.itheima.service.impl.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private IBookService bookService;
    @GetMapping
    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.updateById(book);
    }
    @DeleteMapping("{id}")
    public Boolean delete(@PathVariable Integer id){
        return bookService.removeById(id);
    }

    @GetMapping("{id}")
    public Book getById(@PathVariable Integer id){
        return bookService.getById(id);
    }
    @GetMapping("{current}/{size}")
    public IPage<Book> getPage(@PathVariable int current,@PathVariable int size){
        return bookService.page(current,size);
    }
}

表现层接口用Postman测试工具进行测试

(10)、前后端数据协议

#####-Result类

import lombok.Data;

@Data
public class Result {


    private Boolean flag;
    private Object data;

    public Result(Boolean flag, Object data) {
        this.flag = flag;
        this.data = data;
    }

    public Result(){}
    public Result(Boolean flag) {
        this.flag = flag;
    }
}
-对Controller(表现层)进行修改
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.controller.utils.Result;
import com.itheima.domain.Book;
import com.itheima.service.impl.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private IBookService bookService;
    @GetMapping
    public Result getAll(){
        return new Result(true,bookService.list());
    }
    @PostMapping
    public Result save(@RequestBody Book book){
        return new Result(bookService.save(book));
    }
    @PutMapping
    public Result update(@RequestBody Book book){
        return new Result(bookService.updateById(book));
    }
    @DeleteMapping("{id}")
    public Result delete(@PathVariable Integer id){
        return new Result(bookService.removeById(id));
    }

    @GetMapping("{id}")
    public Result getById(@PathVariable Integer id){
        return new Result(true,bookService.getById(id));
    }
    @GetMapping("{current}/{size}")
    public Result getPage(@PathVariable int current,@PathVariable int size){
        return new Result(true,bookService.page(current,size));
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

零星_AagT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值