Spring_boot简单操作数据库

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

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

pom.xml

<dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.11version>
      <scope>testscope>
    dependency>
    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-webartifactId>
      <version>1.5.7.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-data-jpaartifactId>
      <version>1.5.7.RELEASEversion>
    dependency>
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>5.1.6version>
    dependency>
    <dependency>
      <groupId>javax.persistencegroupId>
      <artifactId>persistence-apiartifactId>
      <version>1.0version>
    dependency>
    <dependency>
      <artifactId>slf4j-apiartifactId>
      <groupId>org.slf4jgroupId>
      <version>1.7.10version>
    dependency>

    <dependency>
      <groupId>ch.qos.logbackgroupId>
      <artifactId>logback-classicartifactId>
      <version>1.2.3version>
      <exclusions>
        <exclusion>
          <artifactId>slf4j-apiartifactId>
          <groupId>org.slf4jgroupId>
        exclusion>
      exclusions>
    dependency>

    <dependency>
      <groupId>ch.qos.logbackgroupId>
      <artifactId>logback-coreartifactId>
      <version>1.2.3version>
    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{
}

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 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/    尽量使用发布时间相近的包,避免不兼容问题

你可能感兴趣的:(Spring_boot简单操作数据库)