Spring_boot simple operation database

Spring_boot with Spring Data JPA to easily operate the database

The spring boot configuration file can use the yml file. By default, spring boot will load the application.yml file in the resources directory. The yml file is strictly indented with tabs.

pom.xml

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>1.5.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
      <version>1.5.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.6</version>
    </dependency>
    <dependency>
      <groupId>javax.persistence</groupId>
      <artifactId>persistence-api</artifactId>
      <version>1.0</version>
    </dependency>
    <dependency>
      <artifactId>slf4j-api</artifactId>
      <groupId>org.slf4j</groupId>
      <version>1.7.10</version>
    </dependency>

    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.2.3</version>
      <exclusions>
        <exclusion>
          <artifactId>slf4j-api</artifactId>
          <groupId>org.slf4j</groupId>
        </exclusion>
      </exclusions>
    </dependency>

    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
      <version>1.2.3</version>
    </dependency>
  </dependencies>

 

application.yml configuration file link database

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/njy
    username: root
    password: root
  jpa:
    hibernate:
      ddl-auto: update #create delete the table and then create update to save the original data
    show-sql: true

The operation of the database is somewhat similar to the mapper interface proxy of mabatis, you need to customize an interface to inherit JpaRepository

package ni.jun.yang;

import ni.jun.yang.bean.Girl;
import org.springframework.data.jpa.repository.JpaRepository;

public interface GirlRepository extends JpaRepository<Girl, Integer>{
}

Implementing logic in Contrller

package ni.jun.yang;

import ni.jun.yang.bean.Girl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
public class GirlController {

    @Autowired
    private GirlRepository girlRepository;
    @GetMapping(value = "/girls")
    public List<Girl> selectAll(){
        return girlRepository.findAll();
    }

    @GetMapping(value = "/addGirls")
    public Girl selectAll(@RequestParam String name, @RequestParam Integer age){
        Girl girl = new Girl();
        girl.setAge(age);
        girl.setName(name);
        return girlRepository.save(girl);
    }
}

After starting the service, send a request of http://localhost:8080/addGirls?name=aaa&age=18 to find that the data is inserted into a piece of data, http://localhost:8080/girls will query the data in the database, and the modification is also called the save method. Remove the delete method and query a findOne. To query by other fields, you need to add the abstract method findByxxx to the custom interface, where xxx is the field name. Complex query Baidu

Guess you like

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