springboot搭建web项目完整步骤(简易,一看就会!)

1、使用idea工具创建springboot项目
第一步:
在这里插入图片描述
后续直接点next,下一步
直到进入依赖选择页面,Web选择Spring Web,Template Engines选择Thymeleaf(模板引擎若在项目建立之后需要改动,可在application.yml进行配置即可),SQL选择JDBC API,Mybatis,MySQL Driver
在这里插入图片描述
2、建好项目后会自动生成pom.xml并自动添加了相应的maven依赖

<?xml version="1.0" encoding="UTF-8"?>


4.0.0

org.springframework.boot
spring-boot-starter-parent
2.2.4.RELEASE


com.example
demo
0.0.1-SNAPSHOT
demo
Demo project for Spring Boot

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.1</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
3、配置mysql数据源,因为引入了mysql依赖,所以这一步不能省略,使用application.yml配置,需要将application.properties删除:

spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver

4、环境搭建好了,可以正式开始写代码。
HelloController.java

@Controller //可以返回视图
//自动为springboot应用进行配置
//@EnableAutoConfiguration
public class HelloController {

@RequestMapping("/myIndex")
public String index() {
    System.out.println("hello.springboot的第一个controller");
    return "index1";
}

}
-在resources/template路径下创建 index1.html

Title 你好,这是我的第一个springboot页面,thymeleaf

在所有java文件的父路径下创建springboot启动类,DemoApplication.java

//标识为springboot启动类,必须是父路径,其他包路径必须是其子路径
//@ComponentScan(basePackages = “com”)
@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {

    SpringApplication.run(DemoApplication.class, args);
}

}
在这里插入图片描述

5、浏览器输入http://localhost:8080/myIndex,成功访问!
在这里插入图片描述

  • 5
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
搭建一个Spring BootWeb项目,可以按照以下步骤进行操作: 1. 使用IDE(如IntelliJ IDEA)创建一个新的Spring Boot项目。 2. 在创建项目时,选择Spring Web作为依赖项,以支持Web开发。 3. 在创建项目后,确保pom.xml文件中包含了必要的依赖项,如Thymeleaf模板引擎和MySQL数据库驱动程序。 4. 在项目的主类(通常是DemoApplication.java)上添加@SpringBootApplication注解以标识为Spring Boot的启动类。 5. 在主类中添加main方法,使用SpringApplication.run(DemoApplication.class, args)来启动应用程序。 6. 创建一个新的HTML文件(如index.html),并将其放置在resources/templates目录下。在该HTML文件中,可以使用Thymeleaf来渲染动态内容。 7. 在HTML文件中,使用Thymeleaf的语法来插入动态内容,比如使用th:text属性来设置文本内容。 8. 运行应用程序,并在浏览器中访问http://localhost:8080(默认情况下)来查看你的Spring Boot Web项目。 请注意,在创建项目的过程中,你还可以根据需要选择其他依赖项,如数据库访问框架(如MyBatis)或其他功能模块。可以在pom.xml文件中添加相应的依赖项来支持这些功能。 以上是搭建Spring Boot Web项目的基本步骤,希望对你有所帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [springboot搭建web项目完整步骤(简易一看!)](https://blog.csdn.net/weixin_56281219/article/details/117121783)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [springboot搭建web项目完整步骤](https://blog.csdn.net/qq_39946015/article/details/104527057)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值