基于 Spring Boot 的 SSM 环境整合二:创建 demo 项目

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xz2001/article/details/83380822

1、创建项目

   第一步,创建一个maven项目  

   

    第二步,选择“Create a simple project”:

   

    第三步,填写项目参数。我选择1.8(compiler level)时点击"finksh“没有反应,不确定是不是我环境问题,我这里先选择1.6,可以正常完成。

    

     第四步,创建完成 项目后打开pom.xml,修改其中的1.6为1.8:

    

    第五步,修改项目统一编码为utf-8:

    

     然后再pom.xml中增加如下配置:

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

2、创建 demo

2.1 修改 pom.xml 以增加配置和引入资源

    1)、引入spring-boot-start-parent,spring官方的叫stater poms,它可以提供dependency management,也就是依赖管理,引入以后在声明其它dependency的时候就不需要version了。

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

    2)、引入spring-boot-starter-web,spring官方解释spring-boot-start-web包含了spring webmvc和tomcat等web开发的特性。

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>

    3)、增加 main 启动方式时的必要配置,但若使用maven的spring-boot:run方式启动的话则需要此配置

      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
        	<!-- 设置主类入口 -->
        	<mainClass>com.whowii.main.AppBoot</mainClass>
        	<!-- 设置classpath -->
        	<addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
        </configuration>
      </plugin>

    最后,完整的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>com.whowii</groupId>
  <artifactId>website_java4</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <!-- 引入spring-boot-start-parent 以提供dependency management,也就是依赖管理,引入以后在声明其它dependency的时候就不需要version了 -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
  </parent>
  
  <dependencies>
  	<!-- 引入spring-boot-starter-web 以包含spring webmvc和tomcat等web开发的特性 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- 日志工具 -->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-logging-juli</artifactId>
        <version>8.0.23</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>website_java4</finalName>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <!-- 增加main方式启动的配置,若使用maven的spring-boot:run方式启动则不需要此配置 -->
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
        	<!-- 设置主类入口 -->
        	<mainClass>com.whowii.main.AppBoot</mainClass>
        	<!-- 设置classpath -->
        	<addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
        </configuration>
      </plugin>
    </plugins>
  </build>
  
  <!-- 设置编码 -->
  <properties>
  	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
</project>

2.2 创建启动类

第一步,在src/main/java下创建包:com.whowii.main

第二步,创建类AppBoot.java,内容如下:

package com.whowii.main;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class AppBoot {
	@RequestMapping("/demo")
	String home() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		return "Hello World!,当前时间是[" + df.format(new Date()) + "]";
	}

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

2.3 编译

默认myeclipse已经编码了你的java项目,现在需要打包相关的jar

每当你修改了pom.xml,增加资源坐标时,都需要执行这个操作。

2.4 编译和发布

编译和测试有如下几种方式:

方法一:在AppBoot类上点右键,依次选择“Run As"->"Java Application":

打开浏览器访问:http://127.0.0.1:8080/demo

方法二:在项目上右键依次点击“Run As"->"Maven build":

在弹出的窗口 Boals 项中填入“spring-boot:run”,然后点击 Run,最后打开浏览器访问:http://127.0.0.1:8080/demo

方法三:先右键项目,选择“Run As“->"Maven clean":

再右键项目,点击“Run As”->"Maven install":

发现报错“ Fatal error compiling: 无效的目标发行版: 1.8”

需要修改下MyEclise中jdk默认项,Window-->prefrences-->java-->Installed JREs 勾选jdk8:

再次执行 Maven install后,刷新下target目录,发现多了个website_java4.jar文件。

然后打开cmd窗口,并转到target目录下,执行“java -jar website_java4.jar”:

启动完成后如下:

最后打开浏览器访问“http://127.0.0.1:8080/demo”,内容如下:

猜你喜欢

转载自blog.csdn.net/xz2001/article/details/83380822
今日推荐