Spring_boot简单操作数据库

时间:2022-12-09 16:15:31

Spring_boot搭配Spring Data JPA简单操作数据库

spring boot 配置文件可以使用yml文件,默认spring boot 会加载resources目录的下的application.yml文件,yml文件严格使用tab缩进。

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 配置文件 链接数据库

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删除表再创建  update 保存原来数据
    show-sql: true

对于数据库的操作有点类似mabatis的mapper接口代理,需要自定义一个接口继承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>{
}

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);
    }
}

启动服务之后发送http://localhost:8080/addGirls?name=aaa&age=18请求会发现数据插入一条数据,http://localhost:8080/girls会将数据库中数据查询出来,修改也是调用save方法,删除delete方法,查询一个findOne。通过其他字段查询需要去自定义接口添加抽象方法findByxxx,xxx为字段名字。复杂查询百度

查找maven依赖:http://mvnrepository.com/    尽量使用发布时间相近的包,避免不兼容问题