学习spring+vue笔记四-springboot集成mybatis-plus、集成swagger.

工程代码接上一篇笔记

springboot集成mybatis-plus

带有plus字眼的都很厉害,我们百度mybatis-plus,登录mybatis-plus的官网,点击快速开始,然后点击左侧的安装项。
在这里插入图片描述辅助springboot 2下面的maven 代码到自己的porn文件中,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zhuoaninfo</groupId>
    <artifactId>vueDemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>vueDemo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <!--            <scope>provided</scope>-->
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.opentracing.contrib</groupId>
            <artifactId>opentracing-spring-web</artifactId>
            <version>0.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.3.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.2</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>manager</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.7.5</version>
                <configuration>
                    <fork>true</fork>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>aliyun-repos</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>central</id>
            <url>https://maven.aliyun.com/repository/central</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>aliyun-plugin</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
</project>

我把mybatis的依赖去掉了。
然后把原来的代码做一些优化,引入mybatis-plus,需要在启动程序中加mapperscan(自己的mapper.class所有的文件夹)注释。
程序的主入口代码为:

package com.zhuoaninfo.vueDemo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.zhuoaninfo.vueDemo.mapper")
public class VueDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(VueDemoApplication.class, args);

    }
}

优化userMapper.class代码,去掉@Mapper注释和相关代码内容,增加BaseMapper的继承,代码如下:

package com.zhuoaninfo.vueDemo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhuoaninfo.vueDemo.entity.User;
public interface UserMapper extends BaseMapper<User> {
}

代码简单多了,然后删除/resources/mapper/userMapper.xml文件。
再优化下userService.class的代码,继承Serviceiml.

package com.zhuoaninfo.vueDemo.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuoaninfo.vueDemo.entity.User;
import com.zhuoaninfo.vueDemo.mapper.UserMapper;
import org.springframework.stereotype.Service;

@Service
public class UserService extends ServiceImpl<UserMapper,User> {
  
}

在UerController.class中修改相应的调用代码,把原来UserMapper的一些方法调用都调整为了UserService的嗲用。,代码如下:

package com.zhuoaninfo.vueDemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuoaninfo.vueDemo.entity.User;
import com.zhuoaninfo.vueDemo.mapper.UserMapper;
import com.zhuoaninfo.vueDemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Controller
@RequestMapping("/vueDemo")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;

    @RequestMapping("/")
    public String index() {
        return "welcome to springboot!";
    }

    @RequestMapping("/users")
    public List<User> getUsers() {
        return userService.list();
    }

    @PostMapping("/user")
    public Boolean addUser(@RequestBody User vo) {

        return userService.saveOrUpdate(vo);
    }

    @PutMapping("/user")
    public Boolean updateUser(@RequestBody User vo) {
        return  userService.updateById(vo);
    }

    @DeleteMapping("/{id}")
    public Boolean deleteUser(@PathVariable Integer id) {
        return userService.removeById(id);
    }

        @RequestMapping("/getPages")
    public IPage<User> getPages(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String name, @RequestParam String real_name) {
            Page<User> page=new Page<User>(pageNum, pageSize);
            QueryWrapper<User> query = new QueryWrapper();
            query.like("name",name);
            IPage<User> iPage=userService.page(page,query);
        return iPage;
    }
}

启动项目,进行测试。
在这里插入图片描述数据分页查询成功。
这里一般测试接口大多使用POSTMAN,昨天学到了swagger,也顺便记录一下。

springboot集成swagger3

首先在porn中添加依赖,

  <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

然后在程序入口增加@EnableOpenApi的注释

package com.zhuoaninfo.vueDemo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.oas.annotations.EnableOpenApi;

@EnableOpenApi
@SpringBootApplication
@MapperScan("com.zhuoaninfo.vueDemo.mapper")
public class VueDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(VueDemoApplication.class, args);

    }
}

在com/zhuoaninfo/vueDemo/config新建SwaggerConfig类,代码如下:

package com.zhuoaninfo.vueDemo.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .select()
.apis(RequestHandlerSelectors.basePackage("com.zhuoaninfo.vueDemo.controller"))
                .build();
    }
}

上面的代码basePackage(“com.zhuoaninfo.vueDemo.controller”))里面的路径修改为自己项目中对应的controller文件夹的路径,格式不变,我之前写成了com/zhuoaninfo/vueDemo/controller,结果启动项目后访问Swagger页面,找不到controller。
正常已经完成了SWagger的配置,我们启动一下项目。
启动项目报错,

The method’s class, org.springframework.plugin.core.PluginRegistry, is available from the following locations:
jar:file:/C:/Users/Administrator/.m2/repository/org/springframework/plugin/spring-plugin-core/1.2.0.RELEASE/spring-plugin-core-1.2.0.RELEASE.jar!/org/springframework/plugin/core/PluginRegistry.class

It was loaded from the following location:
file:/C:/Users/Administrator/.m2/repository/org/springframework/plugin/spring-plugin-core/1.2.0.RELEASE/spring-plugin-core-1.2.0.RELEASE.jar

百度查询了一下需要强制升级spring-plugin-core为2.0。我按照百度的思路,增加porn文件依赖,升级spring-plugin-core。代码如下:

        <dependency>
            <groupId>org.springframework.plugin</groupId>
            <artifactId>spring-plugin-core</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.plugin</groupId>
            <artifactId>spring-plugin-metadata</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>

重新启动项目成功。
访问: http://localhost:8181/swagger-ui/,这里的小坑是swagger-ui/后面的这个/一定要带,要不还是出来页面。
成功访问。
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

cngm110

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

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

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

打赏作者

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

抵扣说明:

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

余额充值