Spring project configuration

1. Create a project

2. Modify the encoding format to UTF-8

3. Check or configure the code auto-completion function

4. Check or configure automatic package import

5. Check or configure Maven, you can use domestic warehouse mirroring

6. IDEA recognizes the Maven project: In the Notifications view, it will prompt Load Maven Project, just click it.

7. Turn on hot deployment

   a. Confirm that dependencies have been referenced in pom.xml

!-- dependencies 标签下检查是否引⼊spring-boot-devtools依赖,如果没有加⼊如下引⽤ --> 
 <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <scope>runtime</scope>
   <optional>true</optional>
 </dependency>

b. Check the following options in the corresponding column of Settings

8. Select YAML or Properties file

In the project, YAML and Properties files can be selected as configuration files (can exist at the same time), and the application.yml file is used here

# application.yml
# Spring 全局配置 
spring:
  application:
    name: java_forum  # 项目名称
  output:
    ansi:
      enabled: ALWAYS # 控制台输出彩色日志
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/java_forum?characterEncoding=utf8&useSSL=false # 数据库连接串
    username: root # 数据库用户名
    password:      # 数据库密码
    driver-class-name: com.mysql.jdbc.Driver # 数据库驱动类
  mvc:
    pathmatch:
      matching-strategy: ANT_PATH_MATCHER #Springfox-Swagger兼容性配置
  # JSON序列化配置
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss # 日期格式
    default-property-inclusion: NON_NULL # 不为null时序列化

server:
  port: 58080 # 修改Tomcat的默认端口号

# 日志配置
logging:
  pattern:
    date format: yyyy-MM-dd HH:mm:ss
  level:
    root: info # 默认日志级别
    com.example.demo: debug # 指定包的日志级别
  file:
    path: D:\git_file\java_forum  # 日志保存目录

# mybatis 相关配置,单独配置,顶格写
mybatis:
  mapper-locations: classpath:mapper/**/*.xml # 指定 xxxMapper.xml的扫描路径

# 项目自定义相关配置
java-forum:
  login:
    url: sign-in.html  # 未登录状况下强制跳转页面
  index: # 首页配置节点
    board-num: 9  # 首页中显示的版块个数

9. Environmental testing

Create a controller package at the same level as JavaForumApplication.java, and create TestController.java under the controller package

// Controller注解,返回的对象⽤JSON形式表⽰
@RestController
// 指定测试根路径映射地址前缀
@RequestMapping("/test")
public class TestController {

    @ApiOperation(("测试打印"))
    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }
}

10. Test Maven

     Add the following configuration under the properties tag of the pom.xml file:

<?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.6</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>java_forum</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>java_forum</name>
	<description>基于 Spring 前后端分离版本的论坛系统</description>
	<properties>
		<!-- 编译环境JDK版本 -->
		<maven.compiler.source>${java.version}</maven.compiler.source>
		<!-- 运⾏环境JVM版本 -->
		<maven.compiler.target>${java.version}</maven.compiler.target>
		<!-- 构建项⽬指定编码集 -->
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<!-- 管理依赖版块号-->
        <!-- mysql-connector 数据库链接驱动包 -->
		<mysql-connector.version>5.1.49</mysql-connector.version>
		<!-- mybatis -->
		<mybatis-starter.version>2.3.0</mybatis-starter.version>
		<!-- 数据源,阿里出的数据源工具 -->
		<druid-starter.version>1.2.16</druid-starter.version>
		<!-- mybatis生成器 -->
		<mybatis-generator-plugin-version>1.4.1</mybatis-generator-plugin-version>
		<!-- springfox - Swagger -->
		<springfox-boot-starter.version>3.0.0</springfox-boot-starter.version>
		<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-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</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>
<!--		数据库驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql-connector.version}</version>
		</dependency>
		<!-- mybatis 依赖
 其中已经包含了spring-jdbc不再重复引⽤,
 此项⽬中使⽤spring-jdbc提供的HikariCP做为数据源, 相关配置在yml⽂件中
-->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>${mybatis-starter.version}</version>
		</dependency>

		<!-- 阿⾥巴巴druid数据源,如果使⽤SpringBoot默认的数据源,删除或注释这个依赖即可 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>${druid-starter.version}</version>
		</dependency>
		<!-- API文档生成,基于swagger2 -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-boot-starter</artifactId>
			<version>${springfox-boot-starter.version}</version>
		</dependency>
		<!-- SpringBoot健康监控 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

		<!-- 编码解码加密工具包-->
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<version>3.1.0</version>
				<configuration>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>

			<!-- mybatis ⽣成器插件 -->
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>${mybatis-generator-plugin-version}</version>
				<executions>
					<execution>
						<id>Generate MyBatis Artifacts</id>
                        <!--指定Maven中的执行阶段 -->
						<phase>deploy</phase>
						<goals>
							<goal>generate</goal>
						</goals>
					</execution>
				</executions>
				<!-- 相关配置 -->
				<configuration>
					<!-- 打开⽇志 -->
					<verbose>true</verbose>
					<!-- 允许覆盖 -->
					<overwrite>true</overwrite>
					<!-- 配置⽂件路径 -->
					<configurationFile>
						src/main/resources/mybatis/generatorConfig.xml
					</configurationFile>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

11. In the Maven column, double-click the package to run the command

     Use a browser to access: http://localhost:58080/test/hello , and the following results show that the operation is successful.

12. Push to the remote warehouse through Git

      View the current status and list the files added after unmodification: git status

      Add the modified file to the temporary storage area, run git status again, and the above file will turn green: git add .

      Submit to the local warehouse: git commit -m 'remarks'

      Push to remote warehouse: git push origin master

Guess you like

Origin blog.csdn.net/crazy_tan/article/details/132687002