springboot实现启动直接访问项目地址

springboot启动直接访问项目地址

方式一

编辑启动类(我的启动类是ApplicationBootstrap) => 进去后找到图中标记处Add按钮

选中菜单中的 Launch Web Browser => 选择浏览器 => 输入打开网址 => 保存后项目启动

启动项目 . . .

方式二

springboot启动直接访问项目地址

该类放置的位置,比springboot 启动类低一级即可,一般我都会创建一个config 文件夹,然后把它们放到一起

package com.hbsc.config; 
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component; 
 
/**
 * @ClassName MyCommandRunner
 * @Author: hanyong
 * @CreateTime: 2019-01-28
 */
@Component
public class MyCommandRunner implements CommandLineRunner {
    @Override
    public void run(String... args) {
        if(true){
            String cmd = "C:Program Files (x86)GoogleChromeApplicationchrome.exe" + " " + "http://localhost:8082";
            Runtime run = Runtime.getRuntime();
            try{
                run.exec(cmd);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

springboot运行无法访问

学习springBoot 搭建遇到一个问题,在此记录,以便后面自己查看及给遇到相同问题的学习者

创建一个springBoot 项目

在springBoot 的目录中创建了Controler(控制层),Service(逻辑层),Model(实体类),Dao(数据层)

项目配置文件

配置了一个mysql数据库连接信息,访问端口号,编码格式,及mybatis配置

对应的maven中的pom.xml 文件如下:

<?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.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springBoot</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>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.30</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.5</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <!--配置文件的位置-->
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.7</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

springBoot启动类配置

运行,访问http://localhost:8080/getUserById,报错

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

查了半天没有发现问题,后来看了编译后文件发现只有mapper的编译文件,xml 文件不存在,在网上查了下原因,在pom.xml 文件中添加

<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.xml</include>
        </includes>
    </resource>
</resources>

重新运行,编译目标文件夹中xml文件生成。访问成功。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持云海天教程。