Spring Boot 4--连接oracle数据库案例

1、数据库的操作

Spring-Data-Jpa----->oracle

1)什么是Jpa?

JPA(Java persistence API)定义了一系列对象持久化的标准,目前实现这一规范的产品有Hibernate、TopLink等

JPA的主要实现由hibernate、eclipseLink和openJPA

Spring Boot 4--连接oracle数据库案例_第1张图片

2)restful api设计

Spring Boot 4--连接oracle数据库案例_第2张图片

2、案例

代码:

pom.xml

1)在pom.xml中的中添加spring-boot-starter-data-jpa的依赖

jpa的依赖:


	org.springframework.boot
	spring-boot-starter-data-jpa

2)添加oracle驱动ojdbc

手动添加oracle

cmd输入mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=11.1.0.6.0 -Dpackaging=jar -Dfile=ojdbc14.jar

同理,也在中添加


	com.oracle
	ojdbc14
	11.1.0.6.0
application.yml( src/main/resources包下)
datasource:
      driver-class-name: oracle.jdbc.OracleDriver
      url: jdbc:oracle:thin:@192.168.6.25:1521:orcl
      username: scott
      password: tiger
jpa:
  hibernate:
     ddl-auto: update
     show-sql: true
Girl.java

package com.example;
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 Girl setId(Integer id) {
        this.id = id;
        return this;
    }
    public String getCupSize() {
        return CupSize;
    }
    public Girl setCupSize(String cupSize) {
        CupSize = cupSize;
        return this;
    }
    public Integer getAge() {
        return age;
    }
    public Girl setAge(Integer age) {
        this.age = age;
        return this;
    }
}
GirlRepository.java(接口)
package com.example;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface GirlRepository extends JpaRepository {
    //通过年龄来检查
    public List findByAge(Integer age);
}
GirlController.java

package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
public class GirlController {
    @Autowired
    private GirlRepository girlRepository;

    /**查询所有女生列表*/
    @GetMapping(value = "/girls")
    public List girlList(){
        return girlRepository.findAll();
    }
    /**添加一个女生*/
    @PostMapping(value="/girls")
    public Girl girlAdd(@RequestParam("cupSize") String cupSize,
                          @RequestParam("age") Integer age){
        Girl girl=new Girl();
        girl.setCupSize(cupSize);
        girl.setAge(age);
        return girlRepository.save(girl);
    }
    //查询一个女生
    @GetMapping(value = "/girls/{id}")
    public Girl girlFindOne(@PathVariable("id")Integer id){
        return girlRepository.findOne(id);
    }
    //更新
    @PutMapping(value = "/girls/{id}")
    public Girl girlUpdate(@PathVariable("id") Integer id,
                           @RequestParam("cupSize") String cupSize,
                           @RequestParam("age") Integer age){
        Girl girl=new Girl();
        girl.setId(id);
        girl.setCupSize(cupSize);
        girl.setAge(age);
        return girlRepository.save(girl);
    }
    //删除
    @DeleteMapping(value="/girls/{id}")
    public void girlDelete(@PathVariable("id") Integer id){
        girlRepository.delete(id);
    }

    //通过年龄查询女生列表
    @GetMapping(value = "/girls/age/{age}")
    public  List girlListByAge(@PathVariable("age")Integer age){
        return girlRepository.findByAge(age);
    }
}

DemoApplication.java

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}






 
  

你可能感兴趣的:(Spring Boot 4--连接oracle数据库案例)