SpringBoot创建一个简单的Web项目

300 阅读2分钟

1、maven需要导入的依赖

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wzl</groupId>
    <artifactId>springboot_study</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.15</version>
    </parent>
    <dependencies>
        <!--引入Web场景依赖启动器-->
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <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>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.23</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.23</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

2、启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.wzl")
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class);
    }
}

屏幕截图 2023-08-27 215531.png 这里的ComponentScan("com.wzl")我有点疑惑,我是将启动类和要扫描的包放在了同级目录,但是还是需要我指明要扫描的包的路径,不然容器就没把我要注册的bean扫描到。当我注解的参ComponentScan("com.wzl.mapper")的包时正式环境可以扫描到,但是测试类下就获取不到,需ComponentScan("com.wzl")才能正式环境和测试类都能正常扫描的到要注入的bean

3、配置文件

server:
  port: 8123
  servlet:
    context-path: "/springboot"
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/druid_test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      #SpringBoot因为默认是hikari数据源所以对于其他数据源默认是不注入这些属性的,需要手动配置
      #druid数据源专有配置
      maxPoolPreparedStatementPerConnectionSize: 20
      useGlobalDataSourceStat: true
      initialSize: 5
      minIdle: 5
      maxActive: 10
      maxWait: 60000 
      timeBetweenEvictionRunsMillis: 1800000 
     

4、本次踩到的坑


import com.wzl.entity.po.User;
import com.wzl.service.GetProgramInfoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

//踩坑 -> 使用了@RestController("/path") 或 @Controller("/path")
//正确的匹配路径应该使用 -> @RequestMapping("/path")
//@RestController 和 @Controller 区别 -> 前者表示返回值是字符串,后者是返回MCV里对应的view(视图)
//一般SpringBoot启动后匹配不到路径,一个是启动类的位置问题,导致扫描不到,一个是上面的这个问题
@RestController
@RequestMapping("/api")
public class GetInFoController {

    @Resource
    private GetProgramInfoService getProgramInfoService;

    @GetMapping("/hello")
    public String getInfo() {
        return "hello, SpringBoot!";
    }

    @GetMapping("/test")
    public int insertInfo(User user) {
        return getProgramInfoService.insertInfo(user);
    }
}
// -> 测试类的目录结构需要和启动类的一样的,不然无法获取容器里的对象
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

@SpringBootTest
public class MyTest {
    @Resource
    private GetInFoController get;
    @Test
    public void testGetO() {
        String info = get.getInfo();
        System.out.printf(info);
    }
}