您当前的位置: 首页 >  小志的博客 ide

idea创建maven项目和编写一个主程序并简化部署

小志的博客 发布时间:2019-06-14 11:03:10 ,浏览量:4

1、idea创建一个maven工程
1)File------>new------->Project
2)maven------->选择自己的jdk版本(1.7以上)--------->next
3)GroupId填写自己项目包名称
Artifactld填写项目名称
点击【Next】
4)Project name 填写第3步输入的项目名称
Project location 填写该项目保存路径
5)上一步完成,项目就创建好了,点击右下角的Enable Auto-Import,自动导入所需要的依赖包。
6)在pom.xml文件中导入spring boot相关的依赖

   
        org.springframework.boot
        spring-boot-starter-parent
        1.5.9.RELEASE
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
    

2、编写一个主程序;启动Spring Boot应用
1)在java下创建com.rf包
在com.rf包下创建主启动类

package com.rf;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 *  @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
 */
@SpringBootApplication
public class HelloWorldMainApplication {
    public static void main(String[] args) {
        // Spring应用启动起来
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

2)编写相关的Controller、Service
在com.rf包下创建HelloController类

package com.rf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {
    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        return "hello world !";
    }
}

3)运行主程序测试
进入主程序类,右键点击 【run HelloWorldMainApplication 】
4)在浏览器中访问 localhost:8080/hello

3、简化部署
1)在pom.xml中引入maven插件

    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

2)点击【Maven Projects】------>【Lifecycle】------>【packeage】,在左侧target下会生成项目的jar包
3)把jar包复制到桌面

4)打开dos窗口,进入桌面目录
5)java -jar 命令启动
6)浏览器中输入 localhost:8080/hello 访问如下图

关注
打赏
查看更多评论

小志的博客

暂无认证

  • 4浏览

    0关注

    1100博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录