zoukankan      html  css  js  c++  java
  • SpringBoot 入门 Demo

    SpringBoot   入门 Demo

    Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

    特点

      1. 创建独立的Spring应用程序
      2. 嵌入的Tomcat,无需部署WAR文件
      3. 简化Maven配置
      4. 自动配置Spring
      5. 提供生产就绪型功能,如指标,健康检查和外部配置
      6. 绝对没有代码生成和对XML没有要求配置

    springBoot 简介地址:

      https://baike.baidu.com/item/Spring%20Boot/20249767?fr=aladdin#1

    1.学习SpringBoot 需要有 ssm 的基础知识 

    2. SpringBoot 是在Maven 环境下开发的 需要有Maven 知识做铺垫

    3.开发工具  (IDEA  , eclipse  )

    4. 实际开发步骤:

      1.) 创建一个   maven  工程

      2.) Maven  是 web 项目

      3.) 完善maven 创建的目录结构

      

        

      4.) 修改   pom.xml   文件

     1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
     3   <modelVersion>4.0.0</modelVersion>
     4   <groupId>dt55</groupId>
     5   <artifactId>springboot1</artifactId>
     6   <packaging>war</packaging>
     7   <version>0.0.1-SNAPSHOT</version>
     8   <name>springboot1 Maven Webapp</name>
     9   <url>http://maven.apache.org</url>
    10      
    11      <!-- 配置spring boot所需的依赖 -->
    12     <parent>
    13         <groupId>org.springframework.boot</groupId>
    14         <artifactId>spring-boot-starter-parent</artifactId>
    15         <version>1.5.10.RELEASE</version>
    16     </parent>
    17     
    18     <dependencies>
    19         <!-- spring boot的相关启动 -->
    20         <dependency>
    21             <groupId>org.springframework.boot</groupId>
    22             <artifactId>spring-boot-starter-web</artifactId>
    23         </dependency>
    24         
    25         <!-- 热部署 -->
    26         <dependency>
    27             <groupId>org.springframework.boot</groupId>
    28             <artifactId>spring-boot-devtools</artifactId>
    29             <optional>true</optional>
    30             <scope>true</scope>
    31         </dependency>
    32         
    33         <!-- log4j -->
    34         <dependency>
    35             <groupId>org.springframework.boot</groupId>
    36             <artifactId>spring-boot-starter-log4j2</artifactId>
    37         </dependency>
    38         
    39         <!-- 使用Jasper引擎解析JSP -->
    40         <dependency>
    41             <groupId>org.apache.tomcat.embed</groupId>
    42             <artifactId>tomcat-embed-jasper</artifactId>
    43             <scope>provided</scope>
    44         </dependency>
    45         
    46         <!--jsp支持-->
    47         <!-- servlet 依赖. -->
    48         <dependency>
    49           <groupId>javax.servlet</groupId>
    50           <artifactId>javax.servlet-api</artifactId>
    51           <scope>provided</scope>
    52         </dependency>
    53         <dependency>
    54           <groupId>javax.servlet</groupId>
    55           <artifactId>jstl</artifactId>
    56         </dependency>
    57         
    58         <!-- spring boot整合mybatis -->
    59         <dependency>
    60             <groupId>org.mybatis.spring.boot</groupId>
    61             <artifactId>mybatis-spring-boot-starter</artifactId>
    62             <version>1.3.0</version>
    63         </dependency>
    64 
    65         <!-- MySQL -->
    66         <dependency>
    67             <groupId>mysql</groupId>
    68             <artifactId>mysql-connector-java</artifactId>
    69         </dependency>
    70      
    71   </dependencies>
    72   
    73   <build>
    74       <plugins>
    75             <plugin>
    76                 <groupId>org.springframework.boot</groupId>
    77                 <artifactId>spring-boot-maven-plugin</artifactId>
    78                 <configuration>
    79                     <!-- 没有该配置,devtools 不生效 -->
    80                     <fork>true</fork>
    81                 </configuration>
    82             </plugin>
    83         </plugins>
    84     <finalName>springboot1</finalName>
    85   </build>
    86 </project>

       5.) 修改端口号 创建一个 application.properties 文件

                 1 server.port=8888 

       6.) 配置数据源信息  在application.properties 文件中配置数据源

         

    1 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    2 spring.datasource.url=jdbc:mysql:///dt55
    3 spring.datasource.username=root
    4 spring.datasource.password=root

     7.) 测试代码:

     1 /**
     2  * Project Name:springboot1
     3  * File Name:FrontController.java
     4  * Package Name:cn.java.controller.front
     5  * Date:下午5:21:57
     6  * Copyright (c) 2018, bluemobi All Rights Reserved.
     7  *
     8 */
     9 
    10 package cn.java.controller.front;
    11 
    12 import java.util.List;
    13 import java.util.Map;
    14 
    15 import org.mybatis.spring.annotation.MapperScan;
    16 import org.springframework.beans.factory.annotation.Autowired;
    17 import org.springframework.boot.SpringApplication;
    18 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    19 import org.springframework.context.annotation.ComponentScan;
    20 import org.springframework.stereotype.Controller;
    21 import org.springframework.web.bind.annotation.RequestMapping;
    22 import org.springframework.web.bind.annotation.ResponseBody;
    23 
    24 import cn.java.service.FrontService;
    25 
    26 /**
    27  * Description: <br/>
    28  * Date: 下午5:21:57 <br/>
    29  * 
    30  * @author 朱开心
    31  * @version
    32  * @see
    33  */
    34 @Controller
    35 @EnableAutoConfiguration
    36 @ComponentScan(value = { "cn.java.controller.*", "cn.java.service.impl" })
    37 @MapperScan(value = "cn.java.mapper")
    38 public class FrontController {
    39 
    40     @Autowired
    41     private FrontService frontService;
    42 
    43     @RequestMapping("/index")
    44     @ResponseBody
    45     public String getLogin() {
    46         return "Hello World !!!!!";
    47     }
    48 
    49     public static void main(String[] args) {
    50         // 启动springboot框架
    51         SpringApplication.run(FrontController.class, args);
    52     }
    53 
    54 }

      

     就是这么的简单的操作 ,比 ssm  简单的多啦  

    道友们试试吧  有问题 @我

      

  • 相关阅读:
    [CF1469D] Ceil Divisions
    [CF632D] Longest Subsequence
    [CF1215E] Marbles
    [CF689D] Friends and Subsequences
    [CF707D] Persistent Bookcase
    [CF10D] LCIS
    [CF713C] Sonya and Problem Wihtout a Legend
    [CF1114E] Arithmetic Progression
    [CF1404B] Tree Tag
    [CF710E] Generate a String
  • 原文地址:https://www.cnblogs.com/zhukaixin/p/9182333.html
Copyright © 2011-2022 走看看