【黑马程序员】SSMP整合案例(p32-42)

Springboot的SSMP整合案例制作分析(p32-p42)

模块创建

  1. 整合第三方技术(这里不整合mybatis,而是自己去添加mybatis-plus的坐标))

  2. 在pom文件中添加druid,lombok的starter和mybatis-plus(下面简称mp)的坐标

    • **Lombok是一个Java库,能自动插入编辑器并构建工具,简化Java开发。通过添加注解的方式,不需要为类编写getter或eques方法,同时可以自动化日志变量。**简化了pojo类开发(不用去写get和set方法等)
    		<!--mp-->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.5.3.1</version>
            </dependency>
            
            <!--druid-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>1.2.8</version>
            </dependency>
            
            <!--lombok-->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
    
    
  3. 刷新maven,检查依赖项

  4. 修改properties文件改为yml,和将端口号改为80

实现类标准开发

  1. 准备表数据,这里我以老杜mybatis课程时候的表为例子

  2. 开始写pojo(domain,bean)

    @Data   //@lombok注解(=@Get + @Set)
    public class Stu {
        @TableId
        private Integer sid;
        private String sname;
        private Integer cid;
    }
    

数据层快速开发(mp)

  1. 配置yml文件 带括号中内容需要(个人情况)改变

    spring:
      datasource:
        druid:
         driver-class-name: com.mysql.jdbc.Driver
         url: jdbc:mysql://localhost:3306/(mybatis)?serverTimezone=UTC
         username: (root)
         password: (root)
    
    mybatis-plus:
      global-config:
        db-config:
          table-prefix: t_    //数据库前缀声明
          id-type: auto       //id自增走数据库,而不是根据雪花算法给id赋值
    
  2. 开发Dao接口

    • 在这里补充一下mp一个知识点: 我的数据库的表名是t_stu (不是student), pojo类命名的实体类为Stu
    • 在yml配置中我已经注明了数据库的表名都有前缀t_(table-prefix: t_)
    • 而mp会根据你的实体类的命名而向下去寻找前缀+实体名的表
    • 如果在test类中命名和数据库不一样,可以在实体类的id主键上进行***@TableId***注解标注
      • @TableId注解是专门用在主键上的注解,如果数据库中的主键字段名和实体中的属性名,不一样且不是驼峰之类的对应关系,可以在实体中表示主键的属性上加@Tableid注解,并指定@Tableid注解的value属性值为表中主键的字段名既可以对应上。
    @Mapper
    public interface StuDao extends BaseMapper<Stu> {
    }
    
    • ​ BaseMapper是mp框架开发的接口,继承此接口
  3. 在test中测试dao接口功能(这里仅示范根据id查询的测试)

    @Test
        void testSelectById() {
            System.out.println(stuDao.selectById(4));
        }
    

开始MP运行日志

  • 在yml配置中开启mp运行日志

      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    
  • 效果

分页(粗糙) 具体看p37

  1. 创建config包,接着MPconfig拦截器的类(分页拦截器)

    @Configuration
    public class MPConfig {
        @Bean
        public MybatisPlusInterceptor mybatisPlusInterceptor() {
            MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
            mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
            return mybatisPlusInterceptor;
        }
    }
    
  2. 在测试类中运行

    @Test
        void testGetPage() {
            IPage page = new Page(1, 5);
            stuDao.selectPage(page, null);
        }
    

模糊查询(粗糙)p38

@Test
    void testGetBy() {
        LambdaQueryWrapper<Stu> stuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        stuLambdaQueryWrapper.like(Stu::getCid, 1000);
        stuDao.selectList(stuLambdaQueryWrapper);
    }

业务层快速开发(mp)

  1. 创建service包,在其下面建立service接口StuService继承IService(有mp提供的快速开发接口)

    public interface StuService extends IService<Stu> {
    
    }
    
  2. 实现接口,创建类StuServiceImpl实现StuService接口,继承ServiceImpl<StuDao, Stu>(mp提供)

    @Service
    public class StuServiceImpl extends ServiceImpl<StuDao, Stu> implements StuService {
    }
    
    • 接口IService泛型中填入实体类名

    • ServiceImpl类的泛型左边填数据层的接口,右边填实体类名

    • 快捷键ctrl+f12可以查看mp提供的方法

    • 在Test中注入的是以接口方式注入(面向接口)

      @Autowired
          private StuService service;
      

表示层标准开发

  • 调业务层的service方法,service去调用数据层的方法,层层调用

    @RestController
    @RequestMapping("/students")
    public class StuController2 {
    
        //@Autowired
        private StuService service;
    
        @GetMapping
        public List<Stu> getAll() {
            return service.list();
        }
    
        @PostMapping
        public Boolean save(@RequestBody Stu stu) {
            return service.save(stu);
        }
    
        @PutMapping
        public Boolean update(@RequestBody Stu stu) {
            return service.updateById(stu);
        }
    
        @DeleteMapping("{id}")
        public Boolean delete(@PathVariable Integer id) {
            return service.removeById(id);
        }
    
        @GetMapping("{id}")
        public Stu getById(@PathVariable Integer id) {
            return service.getById(id);
        }
    }
    
  • 课程中老师采用postman程序测试(没试过),我个人使用apifox(国产,free)

    R标准数据传输(实现前后端数据传输协议)
    1. 在controller包下建立utils的子包,创建R类

    2. @RestController
      @RequestMapping("/students")
      public class StuController {
      
          @Autowired
          private StuService service;
      
          @GetMapping
          public R getAll() {
              R r = new R();
              r.setFlag(true);
              r.setData(service.list());
              return r;
          }
      
          @PostMapping
          public R save(@RequestBody Stu stu) {
              Boolean flag = service.save(stu);
              R r = new R();
              r.setFlag(flag);
              return r;
          }
      
      
      
          @DeleteMapping("{id}")
          public R delete(@PathVariable Integer id) {
             Boolean flag = service.removeById(id);
             R r = new R();
             r.setFlag(flag);
             return r;
          }
      
          @GetMapping("{id}")
          public R getById(@PathVariable Integer id) {
              R r = new R();
              r.setFlag(true);
              r.setData(service.getById(id));
              return r;
          }
      }
      
      • 这里都没写分页和模糊查询功能
      • 最后附上apifox测试效果

本文只写了后端的代码,因为还没有学习vue,就先去学vue再去学后面的知识了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值