【JavaWeb】初始化 SpringBoot 项目

本文介绍了SpringBoot的基本概念,如起步依赖和自动配置,以及如何使用Maven创建SpringBoot工程和定义请求处理类。重点展示了如何利用@SpringBootApplication注解简化项目搭建。
摘要由CSDN通过智能技术生成

创建 SringBoot 项目方法

  1. GitHub 现成代码直接拉取
  2. SpringBoot 官方的模板生成器 spring
  3. IDEA 集成

IDEA 集成 SpringBoot

前置知识

  • spring 依赖注入框架,管理java 对象,集成一些其他内容
  • springmvc web框架,提供接口访问,restful接口等能力
  • mybatis 操作数据库orm,对 jdbc 封装
  • mybatis plus 不用 sql 也可以操作 数据库
  • springboot 不用自己管理spring 配置,不用整合各种框架,快速启动项目

概述

SpringBoot 是 Spring 提供的一个子项目,用于快速构建 Spring 应用程序。

在这里插入图片描述

而 SpringBoot 主要用于项目构建。

SpringBoot 特性:

  • 起步依赖:一个 Maven 坐标,整合了完成一个功能需要的所有坐标。
  • 自动配置:boot 程序启动后,一些 bean 对象自动注入ioc 容器
  • 内嵌 Tomcat,Jetty(无需部署 WAR 文件)
  • 外部化配置
  • 不需要 xml 配置

1. 创建 SpringBoot 工程

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

  • SpringBoot Dev Tools 热更新,有更改自动重启 java 项目
  • lombok 注解工具 自动生成 getter setter
  • Spring Configuration Processor 读取属性配置文件
  • spring web 提供 web 访问能力
  • mybatis 操作数据库
  • mysql driver 驱动

pom.xml 是 maven 管理依赖的文件。

2. 连接 MySQL

在这里插入图片描述

整合mybatis plus

mybatis-plus

先创建一个库 schema,然后在 console 里执行 sql 初始化语句:

在这里插入图片描述

官网示例:

CREATE TABLE `user`
(
	id BIGINT NOT NULL COMMENT '主键ID',
	name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
	age INT NULL DEFAULT NULL COMMENT '年龄',
	email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
	PRIMARY KEY (id)
);

INSERT INTO `user` (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');

在 pom.xml 里面添加依赖:

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.7</version>
</dependency>

然后在 application.yml 添加配置:

spring:
  application:
    name: user_demo
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/userdb
    username: root
    password: root
server:
  port: 8080

在启动类添加注解:

@SpringBootApplication
@MapperScan("com.heo.user_demo.mapper")
public class UserDemoApplication {

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

}

然后添加数据库模型(字段一定要和数据库表中的字段一一对应):

在这里插入图片描述

lombok 的一个注解,@Data 自动生成 get、set方法

@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

编写 mapper:

public interface UserMapper extends BaseMapper<User> {

}

编写测试类:

@SpringBootTest
public class SampleTest {

    @Resource
    private UserMapper userMapper;

    @Test
    public void testSelect() {
        System.out.println(("----- selectAll method test ------"));
        List<User> userList = userMapper.selectList(null);
        Assert.isTrue(5 == userList.size(), "");
        userList.forEach(System.out::println);
    }

}
  • Resource 默认先按照 javabean 的名称注入
  • Autowired 按照类型注入

这里可以引入一下 junit 依赖:junit ,用来做单元测试,当然这里使用 junit 和 junit.jupiter 都可以,但是要注意依赖和依赖包中的方法和注解对应。

import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
public class SampleTest {

    @Resource
    private UserMapper userMapper;

    @Test
    public void testSelect() {
        System.out.println(("----- selectAll method test ------"));
        List<User> userList = userMapper.selectList(null);
        Assertions.assertEquals(5, userList.size());
        userList.forEach(System.out::println);
    }

}

todo: 这里后续好像有 bug O.o?

在这里插入图片描述

3. 定义请求处理类

在这里插入图片描述

package com.itheima.controller;
import org.springframework.web.bind.annotation.*;

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        System.out.println("Hello World ~");
        return "Hello World ~";
    }
}    
创建一个基于Spring BootJava Web项目通常涉及以下步骤: 1. **环境准备**:确保你的开发环境中安装了Java开发工具包(JDK)和Maven或Gradle等构建工具。推荐使用IntelliJ IDEA或Eclipse等集成开发环境(IDE)来简化开发过程。 2. **项目初始化**: - 使用Spring Initializr(https://start.spring.io/)来快速生成项目结构。选择Maven或Gradle作为构建工具,选择Web、JPA、Thymeleaf等所需依赖,然后生成项目。 - 或者,如果你使用命令行工具,可以使用Spring Boot CLI来创建项目骨架。 3. **构建项目结构**: - 在项目根目录下创建必要的包结构,例如`com.example.project`,然后在包下创建对应的类和资源文件。 - 配置`application.properties`或`application.yml`文件,设置应用的端口号、数据库连接等参数。 4. **编写控制器**: - 在适当的包下创建一个或多个控制器类(使用`@Controller`或`@RestController`注解),并编写方法来处理HTTP请求。 - 使用`@RequestMapping`、`@GetMapping`、`@PostMapping`等注解来映射URL路径和处理方法。 5. **业务逻辑处理**: - 创建服务类(使用`@Service`注解)来处理业务逻辑。 - 如果需要与数据库交互,创建数据访问对象(使用`@Repository`注解),并使用Spring Data JPA或MyBatis等来操作数据库。 6. **视图层处理**: - 如果使用模板引擎(如Thymeleaf),创建HTML模板文件,并在控制器中返回对应的视图名称。 - 如果是RESTful风格的Web服务,则不需要视图层,直接返回数据即可。 7. **依赖注入与事务管理**: - 使用`@Autowired`注解来实现依赖注入。 - 如果需要事务管理,可以在方法上使用`@Transactional`注解。 8. **运行和测试应用**: - 在IDE中运行应用或使用命令行工具执行`mvn spring-boot:run`或`gradle bootRun`命令。 - 通过浏览器或API测试工具(如Postman)来测试应用的URL。 9. **项目打包与部署**: - 使用Maven或Gradle的打包命令将应用打包为一个可执行的jar或war文件。 - 将打包好的文件部署到Web服务器或使用云平台进行部署。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

秀秀_heo

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

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

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

打赏作者

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

抵扣说明:

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

余额充值