Skip to content

create spring boot project

yangyp8110 edited this page Jan 17, 2018 · 1 revision

新建项目

新建一个maven quickstart项目:

image

项目结构图:

image

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>

  <groupId>mybatis-generator-demo</groupId>
  <artifactId>mybatis-generator</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>mybatis-generator</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

配置项目

一、打开settings.xml,配置本地仓库:

<localRepository>C:\Users\yangyp\.m2\libs</localRepository>

二、在profiles下配置远程仓库(或自建仓库)地址:

  <profiles>
  
   <profile>
      <id>nexus</id>
      <repositories>
        <repository>
          <id>central</id>
          <name></name>
          <url>http://repo1.maven.org/maven2</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>false</enabled></snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <name></name>
          <url>http://repo1.maven.org/maven2</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>false</enabled></snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
	
  </profiles>

http://repo1.maven.org/maven2 为美国ip,速度比较慢

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

    <groupId>mybatis-generator-demo</groupId>
    <artifactId>mybatis-generator</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>mybatis-generator</name>
    <url>http://maven.apache.org</url>

    <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <java.version>1.8</java.version>
    </properties>

    <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.4.0.RELEASE</version>
    </parent>

    <repositories>
      <repository>
        <id>xxx</id>
        <name>xxx</name>
        <url>http://maven.repo.xxx.com/nexus/content/groups/public/</url>
      </repository>
    </repositories>

    <distributionManagement>
      <repository>
        <id>xxx</id>
        <name>xxx</name>
        <url>http://maven.repo.xxx.com/nexus/content/repositories/releases/</url>
      </repository>
      <snapshotRepository>
        <id>xxx</id>
        <name>xxx</name>
        <url>http://maven.repo.xxx.com/nexus/content/repositories/snapshots/</url>
      </snapshotRepository>
    </distributionManagement>

    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
      </dependency>
    </dependencies>

    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.3</version>
          <configuration>
            <source>1.8</source>
            <target>1.8</target>
          </configuration>
        </plugin>
      </plugins>
    </build>
  </project>

四、Application入口:

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

Reimport项目后:

image

controller:

@RestController
@RequestMapping("/")
public class HelloController {

    @RequestMapping("/hello")
    public String PrintHello(){
        return "hello world !";
    }

    @RequestMapping("/printMsg/{msg}")
//    public String PrintPathVariable(@PathVariable String msg){
//        return msg;
//    }
    public String PrintPathVariable(@PathVariable("msg") String paramMsg){
        return paramMsg;
    }


    @RequestMapping("/requestParams")
    public String PrintRequestParams(@RequestParam(name = "inputMsg",required = true) String inputMsg,@RequestParam(value = "username",required = false)String username){
        String msg = "inputMsg :"+inputMsg+",username :"+username;
        return msg;
    }
    
    
    @RequestMapping("/saveUser")
    public String SaveUser(@RequestBody UserDto userDto){
        return userDto.toString();
    }
}

运行测试:

@PathVariable传参:

http://localhost:8080/printMsg/welcome

image

@RequestParam传参:

image

运行成功!

spring.boot服务构建成功!

Clone this wiki locally