构建springboot项目

Spring Boot项目

一、创建项目

1.1 创建一个spring项目

在这里插入图片描述

1.2 添加依赖

在这里插入图片描述

1.3 修改pom.xml文件

因为我们使用的是阿里云的start,要根据这个来编写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.7.18</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>edu.nf</groupId>
    <artifactId>city</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>city</name>
    <description>city</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.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.3.0</version>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- pagehelper分页插件的starter -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.4.6</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <image>
                        <builder>paketobuildpacks/builder-jammy-base:latest</builder>
                    </image>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

二、定义项目结构

2.1 entity

根据需求,定义项目所需的数据模型(实体类)。

2.2 dao

创建数据访问层接口(Repository或Dao),定义数据访问操作,可以使用Spring Data JPA或其他持久化框架。

2.3 service

创建服务层类,封装业务逻辑,可以使用@Autowired注解注入其他依赖的组件。

2.4 controller

创建控制器类,用于处理HTTP请求和返回响应。

2.5 config

存放应用程序的配置类,提供应用程序的配置信息,以便实现灵活的配置和定制。这些配置类可以包含各种类型的配置,如数据源配置、缓存配置、消息队列配置、安全配置等。

2.6 util

存放工具类,封装一些通用的、可重用的功能和方法,以便在整个项目中进行共享和复用。这些工具类通常是一些静态方法,它们提供了一些常见的功能,比如格式转换、日期处理、加密解密、文件操作等。这些工具类可以被项目中的其他类直接调用,来完成相应的功能。

2.6 exception

存放项目中的异常类,主要作用是封装和管理项目中所需的自定义异常类。自定义异常是针对具体业务场景或特定需求而创建的异常类,用于在程序中抛出和处理特定类型的异常情况。

三、编写配置文件

3.1 修改application文件后缀

把application.properties文件修改为application.yml文件

是为了使用 YAML格式来配置应用程序。相对于传统的属性文件(.properties

使用 application.yml 文件可以提供更好的可读性、可维护性和灵活性,尤其适合复杂的配置需求或者需要清晰结构的项目。

3.2 配置相关的属性

# 配置数据源
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/city?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: wyy
    # 配置hikari连接池
    hikari:
      # 最大连接池数量
      maximum-pool-size: 200
      minimum-idle: 10
      connection-timeout: 3000
      idle-timeout: 600000
      connection-test-query: select 1
 # 配置数据源
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/city?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: wyy
    # 配置hikari连接池
    hikari:
      # 最大连接池数量
      maximum-pool-size: 200
      minimum-idle: 10
      connection-timeout: 3000
      idle-timeout: 600000
      connection-test-query: select 1
  servlet:
    # 文件上传
    multipart:
      # 启用文件上传功能
      enabled: true
      # 每个文件限制200MB
      max-file-size: 10MB
      # 单次轻轻找所有文件的总大小
      max-request-size: 100MB

# mybatis的配置
mybatis:
  # 实体别名
  type-aliases-package: edu.nf.content.entity
  # mapper映射配置文件的路径
  mapper-locations: classpath:/mappers/*.xml
  # 输出sql语句
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

# 分页插件配置
pagehelper:
  # 设置数据库方言
  helper-dialect: mysql
  # 启用注解分页参数
  support-methods-arguments: true
  # 分页合理化
  reasonable: true

# 可以设置内嵌容器的端口,默认是8080
server:
  port: 8080
  servlet:
    # 项目访问的上下文路径
    context-path: /
    # 启用字符编码过滤器
    encoding:
      enabled: true
      # 设置编码格式
      charset: UTF-8
      # 转发、重定向都可以使用字符编码
      force: true

# mybatis的配置
mybatis:
  # 实体别名
  type-aliases-package: edu.nf.content.entity
  # mapper映射配置文件的路径
  mapper-locations: classpath:/mappers/*.xml
  # 输出sql语句
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

# 分页插件配置
pagehelper:
  # 设置数据库方言
  helper-dialect: mysql
  # 启用注解分页参数
  support-methods-arguments: true
  # 分页合理化
  reasonable: true

# 可以设置内嵌容器的端口,默认是8080
server:
  port: 8080
  servlet:
    # 项目访问的上下文路径
    context-path: /
    # 启用字符编码过滤器
    encoding:
      enabled: true
      # 设置编码格式
      charset: UTF-8
      # 转发、重定向都可以使用字符编码
      force: true

四、单元测试

添加@ContextConfiguration(classes = Application.class)注解

在Spring Boot中,应用程序通常有一个主要的启动类(通常是标有 @SpringBootApplication 注解的类)。这个主要的启动类定义了应用程序的配置和上下文。在单元测试中,如果我们希望使用与主要应用程序相同的配置和上下文,就可以使用 @ContextConfiguration(classes = Application.class) 注解来加载主要的启动类。
在这里插入图片描述

五、运行项目

5.1 扫描指定路径的Mapper 接口

什么是Mapper接口?

相当于我们定义的dao接口,是用于定义数据库访问操作的接口(数据库的增删改查等操作方法的定义)

在当前项目中的启动类中添加@MapperScan(basePackages = "xxx.xxx.dao")注解

通过在启动类上添加 @MapperScan 注解,指定了要扫描的 Mapper 接口所在的包路径。Spring Boot 在启动过程中会扫描该包下的所有 Mapper 接口,并根据其定义创建相应的 Mapper 实例,然后将其注册为 Spring 的 Bean。

这样,在其他地方需要使用这些 Mapper 接口时,可以使用 @Autowired 或者其他方式进行注入,方便地操作数据库。

在这里插入图片描述

5.2 启动项目

  • 26
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

WyuanY.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值