spring-boot-starter-jdbc

It is written that
Spring Boot is a new framework provided by the Pivotal team, which is designed to simplify the initial construction and development process of new Spring applications. The framework uses a specific way to configure, so that developers no longer need to define boilerplate configuration. In this way, Boot aims to be a leader in the burgeoning field of rapid application development.
You can build the project with Maven | Gradle | Ant | Starters, refer to: http://start.spring.io/ You can choose Maven or Gradle to generate the Demo, and the Spring boot microservice architecture runs in combination with the Docker container.
  For software version running requirements, please refer to the official website: Spring boot official website
  This example software version: JDK1.7 + Spring boot 1.3.5 + Spring 4.2.6
  The commonly used starters and their uses can be listed as follows:
spring-boot-starter: This is the core Spring Boot The starter, which provides most of the basic functionality, other starters depend on, so there is no need to define it explicitly.
spring-boot-starter-actuator: mainly provides functions for monitoring, managing and reviewing applications.
spring-boot-starter-jdbc: This starter provides support for JDBC operations, including connecting to the database, operating the database, and managing database connections, etc.
spring-boot-starter-data-jpa: The JPA starter provides dependency libraries that use the Java Persistence API (such as Hibernate, etc.).
spring-boot-starter-data-*: Provides support for MongoDB, Data-Rest or Solr.
spring-boot-starter-security: Provides all Spring-security dependencies.
spring-boot-starter-test: This starter includes spring-test dependencies and other test frameworks such as JUnit and Mockito, etc.
spring-boot-starter-web: This starter includes dependencies for web applications.

Maven build project pom code Collection code
<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>ygnet</groupId> 
    <artifactId>boot</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging> 
 

    <url>http://maven.apache.org</url> 
     
    <properties> 
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
        <java.version>1.7</java.version> 
    </properties> 
     
    <!-- Spring Boot 启动父依赖 --> 
    <parent> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-parent</artifactId> 
        <version>1.3.5.RELEASE</version> 
        <relativePath /> 
    </parent> 
    <dependencies> 
        <dependency> 
            <groupId>junit</groupId> 
            <artifactId>junit</artifactId> 
            <version>4.12</version> 
            <scope>test</scope> 
        </dependency> 
        <dependency> 
          <groupId>org.springframework</groupId> 
          <artifactId>spring-test</artifactId> 
          <version>4.2.6.RELEASE</version> 
        </dependency> 
             
        <!-- Spring Boot web依赖 --> 
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-web</artifactId> 
        </dependency> 
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter</artifactId> 
        </dependency>  
        <!--dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-test</artifactId> 
            <scope>test</scope> 
        </dependency--> 
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-jdbc</artifactId> 
        </dependency> 
        <dependency> 
            <groupId>org.postgresql</groupId> 
            <artifactId>postgresql</artifactId><scope>runtime</scope> 
        </dependency> 
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-actuator</artifactId> 
        </dependency> 
    </dependencies> 
    <build> 
        <pluginManagement>   
            <plugins>   
              <plugin>   
                <groupId>org.eclipse.m2e</groupId>   
                <artifactId>lifecycle-mapping</artifactId>   
                <version>1.0.0</version>   
                <configuration>   
                  <lifecycleMappingMetadata>   
                    <pluginExecutions>   
                      <pluginExecution>   
                        <pluginExecutionFilter>   
                          <groupId>org.apache.maven.plugins</groupId>   
                          <artifactId>maven-dependency-plugin</artifactId>   
                          <versionRange>[2.0,)</versionRange>   
                          <goals>   
                            <goal>copy-dependencies</goal>   
                          </goals>   
                        </pluginExecutionFilter>   
                        <action>   
                          <ignore />   
                        </action>   
                      </pluginExecution>   
                    </pluginExecutions>   
                  </lifecycleMappingMetadata>   
                </configuration>   
              </plugin>   
            </plugins>   
        </pluginManagement> 
        <plugins> 
            <!-- 打Jar包(META-INF) --> 
            <plugin> 
                <groupId>org.apache.maven.plugins</groupId>   
                <artifactId>maven-jar-plugin</artifactId>   
                <configuration>   
                    <archive>   
                        <manifest>   
                           <addClasspath>true</addClasspath>   
                           <classpathPrefix>lib/</classpathPrefix>   
                           <mainClass>yg.boot.App</mainClass>   
                        </manifest>   
                    </archive>   
                </configuration>   
            </plugin> 
            <!-- 项目资源文件 --> 
            <plugin>   
                <groupId>org.apache.maven.plugins</groupId>   
                <artifactId>maven-resources-plugin</artifactId>   
                <version>2.5</version>   
                <executions>   
                    <execution>   
                        <phase>compile</phase>   
                    </execution>   
                </executions>   
                <configuration>   
                    <encoding>${project.build.sourceEncoding}</encoding>   
                </configuration>   
            </plugin> 
            <!-- 是否启动测试 --> 
            <plugin> 
                <groupId>org.apache.maven.plugins</groupId>   
                <artifactId>maven-surefire-plugin</artifactId> 
                <version>2.17</version>  
                <configuration> 
                  <skipTests>true</skipTests>   
                </configuration> 
            </plugin> 
            <!-- 复制依赖包到项目lib文件夹下 --> 
            <plugin>   
                <groupId>org.apache.maven.plugins</groupId>   
                <artifactId>maven-dependency-plugin</artifactId>   
                <version>2.8</version>   
                <executions>   
                    <execution>   
                        <phase>package</phase>   
                        <goals>   
                            <goal>copy-dependencies</goal>   
                        </goals>   
                    </execution>   
                </executions> 
                <configuration> 
                    <outputDirectory>${project.basedir}/lib</outputDirectory> 
                    <includeScope>compile</includeScope>   
                </configuration>   
            </plugin> 
            <!-- Spring boot packaging--> 
            <plugin> 
                <groupId>org.springframework.boot</groupId> 
                <artifactId>spring-boot-maven-plugin</artifactId> 
            </plugin> 
        </plugins > 
    </build> 
</project> 
Controller
The mechanism provided by the Spring Boot framework is convenient for engineers to implement standard RESTful interfaces and write Controller code. First, we need to add the corresponding starter to the pom file, namely spring-boot-starter-web, corresponding to The xml code example is:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
The @RestController annotation is a collection of @Controller and @ResponseBody, indicating that this is a controller bean, and it is a REST-style controller that directly fills the return value of the function into the HTTP response body.
@RequestMapping("/test") indicates that the controller handles all "/test" URL requests, which function handles the specific processing, and should be distinguished according to the HTTP method: GET means query, POST means submit, PUT means update, DELETE means delete.
For the Restful design guide, please refer to:
The role of the RESTFul Controller. As you can see, I confuse a lot of business code in the code of the Controller. In fact, according to the article programmers must know about the evolution of the front-end, what the Controller layer should do is: Render and redirect the parameters of the request Select Model and Service to handle Session and Cookies, I basically agree with this point of view, plus at most OAuth authentication (implemented with an interceptor). The real business logic should be handled in a separate layer, that is, the common service layer;

Java code collection code
package yg.boot.action; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.web.bind .annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 
@RestController 
@EnableAutoConfiguration 
@RequestMapping("/test") 
public class AppController {     
    @RequestMapping("/sayhello") 
    public String sayHello(){ 
        return "Hello World!"; 
    } 


Spring Boot startup writes
@SpringBootApplication is this annotation is the sign of the entry of the application, and then there is a familiar main function, through SpringApplication .run(xxxApplication.class, args) to run the Spring Boot application. Open the SpringBootApplication annotation and you can find that it is composed of several other classes: @Configuration (equivalent to the xml configuration file in spring, using Java files for configuration can check type safety), @EnableAutoConfiguration (automatic configuration), @ComponentScan (Component scanning, which everyone is very familiar with, can automatically discover and assemble some beans)
    As you can see in the pom file, the scope of the org.postgresql library is runtime, that is, when the application starts, if Spring Boot is in If the existence of org.postgresql is detected under the classpath, the postgresql database connection

Application.properties code will be automatically configured. Collection code
# DataSource settings 
spring.datasource.url=jdbc:postgresql://localhost:5432/jcbk 
spring.datasource.username=jcbk 
spring.datasource.password=123456 
spring.datasource.driver-class-name=org.postgresql.Driver 
 
# Tomcat Server settings (ServerProperties) 
server.port= 9080 
server.address= 127.0.0.1 
server.sessionTimeout= 30 
server.contextPath= / 
 
# Tomcat specifics 
tomcat.accessLogEnabled= false 
tomcat.protocolHeader= x-forwarded-proto 
tomcat.remoteIpHeader= x-forwarded-for 
tomcat.basedir= 
tomcat.backgroundProcessorDelay=30 \# secs 

Java代码  收藏代码
package yg.boot; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
/**
* Hello world!
*/ 
@SpringBootApplication 
public class App {   
    public static void main(String[] args ){ 
        SpringApplication.run(App.class,args); 
    } 

Write After running the App
directly , the result is as shown below. After startup, visit http://localhost:9080/test/sayhello, and output Hello World!








The
project The maven-jar-plugin plugin can be used to package the project to generate boot-0.0.1-SNAPSHOT.jar. The spring-boot-maven-plugin plugin renames boot-0.0.1-SNAPSHOT.jar to boot-0.0.1-SNAPSHOT.jar.original, and then generates a new boot-0.0.1-SNAPSHOT.jar package with a directory structure of :
+---yg
          boot
+---org
          springframework
              boot
                 loader
+----lib
+----META-INF
+----application.properties

Meta-inf代码  收藏代码
Manifest-Version: 1.0 
Implementation-Vendor: Pivotal Software, Inc. 
Implementation-Title: Springboot 
Implementation-Version: 0.0.1-SNAPSHOT 
Implementation-Vendor-Id: ygnet 
Built-By: oy 
Build-Jdk: 1.7.0_45 
Class-Path: lib/spring-test-4.2.6.RELEASE.jar lib/spring-core-4.2.6.RE 
LEASE.jar lib/spring-boot-starter-web-1.3.5.RELEASE.jar lib/spring-bo 
ot-starter-tomcat-1.3.5.RELEASE.jar lib/tomcat-embed-core-8.0.33.jar  
lib/tomcat-embed-el-8.0.33.jar lib/tomcat-embed-logging-juli-8.0.33.j 
ar lib/tomcat-embed-websocket-8.0.33.jar lib/spring-boot-starter-vali 
dation-1.3.5.RELEASE.jar lib/hibernate-validator-5.2.4.Final.jar lib/ 
validation-api-1.1.0.Final.jar lib/jboss-logging-3.3.0.Final.jar lib/ 
classmate-1.1.0.jar lib/jackson-databind-2.6.6.jar lib/jackson-annota 
tions-2.6.6.jar lib/jackson-core-2.6.6.jar lib/spring-web-4.2.6.RELEA 
SE.jar lib/spring-aop-4.2.6.RELEASE.jar lib/aopalliance-1.0.jar lib/s 
pring-beans-4.2.6.RELEASE.jar lib/spring-context-4.2.6.RELEASE.jar li 
b/spring-webmvc-4.2.6.RELEASE.jar lib/spring-expression-4.2.6.RELEASE 
.jar lib/spring-boot-starter-1.3.5.RELEASE.jar lib/spring-boot-1.3.5. 
RELEASE.jar lib/spring-boot-autoconfigure-1.3.5.RELEASE.jar lib/sprin 
g-boot-starter-logging-1.3.5.RELEASE.jar lib/logback-classic-1.1.7.ja 
r lib/logback-core-1.1.7.jar lib/slf4j-api-1.7.21.jar lib/jcl-over-sl 
f4j-1.7.21.jar lib/jul-to-slf4j-1.7.21.jar lib/log4j-over-slf4j-1.7.2 
1.jar lib/snakeyaml-1.16.jar lib/spring-boot-starter-jdbc-1.3.5.RELEA 
SE.jar lib/tomcat-jdbc-8.0.33.jar lib/tomcat-juli-8.0.33.jar lib/spri 
ng-jdbc-4.2.6.RELEASE.jar lib/spring-tx-4.2.6.RELEASE.jar lib/postgre 
sql-9.4.1208.jre7.jar lib/spring-boot-starter-actuator-1.3.5.RELEASE. 
jar lib/spring-boot-actuator-1.3.5.RELEASE.jar 
Start-Class: yg.boot.App 
Created-By: Apache Maven 3.0.4 
Spring-Boot-Version: 1.3.5.RELEASE 
Main-Class: org.springframework.boot.loader.JarLauncher 
Archiver-Version: Plexus Archiver 
    Start-Class is the Spring boot startup class, and Main-Class is the main method entry
    . Please download the attachment
Springboot.rar (8.2 KB)
Download times: 9

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326644895&siteId=291194637