微信公众号
打赏作者
Web项目开发教程

下载安装Maven,使用开发工具配置Maven,创建Maven项目,然后修改配置文件及代码使用变成Springboot。

当然工具也有自带的maven,不过最好还是自已安装一个。

1. 下载安装Maven

官网:https://maven.apache.org/download.cgi

因Maven默认的仓库在国外,下载jar包非常慢,所以需配置阿里云仓库,找到 安装目录/conf/settings.xml

<mirror>
    <id>alimaven</id>
    <name>aliyun maven</name>
    <url>
        http://maven.aliyun.com/nexus/content/groups/public/
    </url>
    <mirrorOf>central</mirrorOf>        
</mirror>

2. 工具配置Maven

IntelliJ IDEA则是"文件"->"设置","构建、执行、部署"->"构建工具"->"Maven", 配置你安装的Maven

Eclipse则是"Window"->"Preferences","Maven"->"Installations", 点击"Add",添加你安装的Maven

比如 IntelliJ IDEA:

Maven主路径:D:/program/apache-maven-3.6.3

用户设置文件:D:\program\apache-maven-3.6.3\conf\settings.xml  重写要勾上

本地仓库:D:\java\maven\repository  重写要勾上

3. 创建Maven项目

新建maven项目,输入名称,组ID,工件ID,选择jdk,archetypes选择 maven-archetype-quickstart 

4. 修改pom.xml

可以直接使用如下内容:

<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>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.4</version>
    <relativePath/>
  </parent>

  <groupId>com.wmksj</groupId>
  <artifactId>cv</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>cv</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven-jar-plugin.version>3.1.1</maven-jar-plugin.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.1.4</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.2</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.13</version>
    </dependency>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>

5. 修改入口函数

package com.wmksj;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 
 *
 */
@SpringBootApplication
public class App
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class, args);
    }
}

6. 创建资源文件 

src/main/resources/application.yml

server:
  port: 80
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2b8
    username: xxx
    password: xxx
注意:mysql的驱动与driver-class-name要一致

mysql-connector-java:5.x  则使用  com.mysql.jdbc.Driver

mysql-connector-java:8.x  则使用  com.mysql.cj.jdbc.Driver

7. 常用注解

controller类:
@RestController
@Resource
@Autowired
@GetMapping("/")

entity类:
@Data
@NoArgsConstructor
@AllArgsConstructor

mapper类:
@Mapper


联系我们