zoukankan      html  css  js  c++  java
  • Spring Boot 入门

    一:pom文件

     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>com.bw30</groupId>
     5   <artifactId>test02</artifactId>
     6   <packaging>war</packaging>
     7   <version>0.0.1-SNAPSHOT</version>
     8   <name>test02</name>
     9   <url>http://maven.apache.org</url>
    10 
    11   <parent>
    12       <groupId>org.springframework.boot</groupId>
    13       <artifactId>spring-boot-starter-parent</artifactId>
    14       <version>1.5.3.RELEASE</version>
    15   </parent>
    16 
    17   <dependencies>
    18     <dependency>
    19       <groupId>org.springframework.boot</groupId>
    20       <artifactId>spring-boot-starter-web</artifactId>
    21     </dependency>
    22     
    23     <dependency>
    24         <groupId>org.springframework.boot</groupId>
    25         <artifactId>spring-boot-starter-tomcat</artifactId>
    26         <scope>provided</scope>
    27     </dependency>
    28         
    29     <dependency>
    30         <groupId>mysql</groupId>
    31         <artifactId>mysql-connector-java</artifactId>
    32     </dependency>
    33     
    34      <dependency>
    35         <groupId>org.springframework.boot</groupId>
    36         <artifactId>spring-boot-starter-data-jpa</artifactId>
    37      </dependency>
    38   </dependencies>
    39   <build>
    40         <finalName>test02</finalName>
    41         <plugins>
    42             <plugin>
    43                 <groupId>org.springframework.boot</groupId>
    44                 <artifactId>spring-boot-maven-plugin</artifactId>
    45             </plugin>
    46         </plugins>
    47   </build>
    48   
    49 </project>
    View Code

    (1)<parent>标签是继承的父项目,继承父项目后<dependency>中就不用写<version></version>,默认加载相应的版本。

    (2)spring-boot-starter-tomcat中<scope>是指:

      * compile,范围指的是编译范围有效,在编译和打包时都会将依赖存储进去。 
        * provided,在编译和测试的过程有效,最后生成war包时不会加入,诸如:servlet-api,因为servlet-apitomcatweb服务器已经存在了,如果再打包会冲突 。
        * runtime,在运行的时候依赖,在编译的时候不依赖,如JDBC驱动,适用运行和测试阶段。 
        * test,test范围指的是测试范围有效,在编译和打包时都不会使用这个依赖。 

    (3)

    二:入口类App.java

     1 package com.bw30;
     2 
     3 import org.springframework.boot.SpringApplication;
     4 import org.springframework.boot.autoconfigure.SpringBootApplication;
     5 import org.springframework.web.bind.annotation.RestController;
     6 
     7 
     8 @RestController  
     9 @SpringBootApplication
    10 public class App {
    11         
    12       public static void main(String[] args) {  
    13         SpringApplication.run(App.class, args);  
    14       }  
    15 }

    (1)@RestController 

       @RestController注解相当于@ResponseBody + @Controller合在一起的作用。

      (1.1)如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,配置的视图解析器InternalResourceViewResolver不起作用,返回的内容就是  Return 里的内容。

    例如:本来应该到success.jsp页面的,则其显示success.

      (1.2)如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
      (1.3)如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解

    (2)@SpringBootApplication

         @SpringBootApplication继承了@Configuration、@EnableAutoConfiguration、@ComponentScan,并具有他们的默认属性值。默认扫描的包是以SpringApplication.run(Springrun.class, args) 参数springrun.class所在的包作为base package。
    注意:如果想要启动整个程序,这个类必须要放到根包里面去。

    三:不使用内嵌的tomcat运行

    (1)项目用war打包,pom配置中<packaging>war</packaging>

    (2)pom配置中spring-boot-starter-tomcat的<scope>provided</scope>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <scope>provided</scope>
    </dependency>

    (3)App.java同目录下添加ServletInitializer类

      

    package com.bw30;
    
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.support.SpringBootServletInitializer;
    
    public class ServletInitializer extends SpringBootServletInitializer{
    
        @Override  
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {  
            return application.sources(App.class);  
        }  
    }

    四:mysql数据库连接和Spring Data JPA配置

    #MySQL
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/wsdb?useUnicode=true
    spring.datasource.username=****
    spring.datasource.password=****
    
    #Spring Data JPA
    spring.jpa.database=MYSQL
    spring.jpa.show-sql=true
    spring.jpa.hibernate.ddl-auto=update
    spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
  • 相关阅读:
    【Python】异常处理
    【Python】写入文件
    【Python】从文件中读取数据
    【Python】导入类
    【Python】继承
    【Python】使用类和实例
    SQL HAVING
    SQL GROUP BY
    SQL ORDER BY
    SQL CREATE INDEX
  • 原文地址:https://www.cnblogs.com/fdzfd/p/6802884.html
Copyright © 2011-2022 走看看