前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot 入门

springboot 入门

作者头像
用户5640963
发布2019-07-26 14:07:24
3090
发布2019-07-26 14:07:24
举报
文章被收录于专栏:卯金刀GG卯金刀GG
  • 使用maven构建project项目, 配置aliyun仓库, 不赘述
  • springboot 版本需要: jdk1.7+, maven3.2+ , gradle2.9+
  • 配置文件
  1. 引入父包, 放在<project>标签内, 期内包含大量配置好的依赖, 在自己项目中添加这些一来是不需要写<version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <!--!这里版本不能使用properties配置--> <version>1.5.2.RELEASE</version> </parent> 有时候我们自己公司内有父pom, 不能直接添加parent, 可以通过以下方式 <dependencyManagement> <dependencies> <dependency> <!-- Import dependency management from Spring Boot --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
  2. 引入web启动包, 因为我们开发的是web工程,所以需要在pom.xml中引入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. 加入仓库配置, 如果是release版本不需要 (you don't need this if you are using a .RELEASE version) <repositories> <repository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> <snapshots><enabled>true</enabled></snapshots> </repository> <repository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories>
  4. 直接使用Main启动spring, plugin需要添加, 如果使用 spring-boot:run 不需要此配置 <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin </artifactId> </plugin> </plugins> </build>
  5. 程序主入口
  6. @RestController //springmvc 注解, 返回给调用者字符串 @SpringBootApplication // 自动给程序添加默认属性 public class App { @RequestMapping("/") public String hello(){ return "Hello world!"; } public static void main(String[] args) { SpringApplication.run(App.class, args); } }
  7. 启动: 运行main函数
    1.   run as : spring-boot:run
    2.   右键, run as java Application

配置pom.xml文件, 使用maven-compiler-plugins插件锁定jdk版本和maven-surefire-plugins插件配置maven-test

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wenbronk</groupId>
    <artifactId>SpringBootTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>

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

    <!-- Additional lines to be added here... -->
     <dependencies>
         <!-- web 启动的jar包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
    <!--jpa的jar包 ,操作数据库的,类似hibernate-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        
        <!--mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        
        <!--thymeleaf模板jar,是很不错的html数据传递取值,类似jsp的jstl-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

    <!--maven的插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        
    <!-- 配置java版本 不配置的话默认父类配置的是1.6-->
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
            <source>1.7</source>
            <target>1.7</target>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

</project>

然后运行maven dependecy:tree, 下载依赖到本地仓库中

注意:

  • java.version属性
    •   spring 默认 使用jdk1.6, 如果想使用1.8, 需要如下配置 <properties> <java.version>1.8</java.version> </properties>
    • 添加spring-boot-starter-web依赖

    spring通过spring-boot-starter-*来支持具体的某个功能

  具体请参照: http://docs.spring.io/spring-boot/docs/1.2.3.RELEASE/reference/html/using-boot-build-systems.html#using-boot-starter-poms

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档