Getting started with springBoot

Environmental preparation

1. A competent text editor (eg Vim, Emacs, Sublime Text) or IDE (Eclipse, Idea Intellij)
2. Java environment (JDK 1.7 or above)
3. Maven 3.0+ (Eclipse and Idea IntelliJ built-in, if Can be installed without using an IDE and without using command line tools)

Generate a web project for the springBoot framework

idea can automatically generate the
address : http://start.spring.io/

Startup items (3 types)

1. Code running
2. Command: mvn spring-boot:run under the project
3. First compile java install
cd target
java -jar project name-0.0.1-SNAPSHOT.jar

Writing the application.yml file

application.properties is changed to application.yml.

1 、 application.yml 中

```
    cupSize: B
```

Get in java

```
@Value("${cupSize}")
    private String cupSize;
```

2. There are configuration parameters in the configuration parameters in application.yml

 cupSize: B
 age: 18
 content: "cupSize: ${cupSize},age: ${age}"

3. Objects in application.yml

```
girl:
 cupSize: B
 age: 18
```

You can create an object in java to accept configuration parameters

```
@Component
@ConfigurationProperties(prefix="girl")
public class GirlParam {

private String cupSize;

private Integer age;

public String getCupSize() {
    return cupSize;
}
public void setCupSize(String cupSize) {
    this.cupSize = cupSize;
}
public Integer getAge() {
    return age;
}
public void setAge(Integer age) {
    this.age = age;
}

}
“`

4. Reference other application-dev.yml files

spring:
  profiles:
    active: dev

Use of Controller

@Controller handles http requests
@RestController The newly added annotations after spring4, the original return json format requires @ResponseBody and @Controller

1. Controller returns the template

pom.xml needs to be added

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <scope>模板</scope>
        </dependency>

Templates are placed in this folder. This method is not recommended, because the current development is basically the separation of front and back ends.
write picture description here

database operations

Spring-data-jpa is required. JPA (Java Persistence API) defines a series of standards for object persistence. Currently, products that implement this specification include Hibernate, TopLinkD and so on.
I am using mysql database.

pom.xml needs to be added

<dependency>                            
    <groupId> org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <scope>data-jpa</scope>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>mysql</scope>
</dependency>

application.yml needs to be added

spring:
    datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/test
        username: root
        password: root

    jpa:
       hibernate:
         ddl-auto: update
       show-sql: true

Create a new class (database field)

The object class must have a no-argument constructor.

package com.yj.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity//对应数据库的一个
public class Girl {

    @Id
    @GeneratedValue//自增的
    private Integer id;

    private String cupSize;

    private Integer age;

    public Girl() {
    }

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getCupSize() {
        return cupSize;
    }
    public void setCupSize(String cupSize) {
        this.cupSize = cupSize;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325835186&siteId=291194637