Spring 基于ORacle JPA的增删改查



1、项目大致结构:

2、配置数据源  application.properties文件

server.port=8011
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html   


############JPA##############
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1526:sid
spring.datasource.username=****
spring.datasource.password=****

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

3Pom.xml文件



	4.0.0

	com.example
	Springboot1
	2.0.0
	jar

	SpringBoot1
	Spring Boot1

	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.0.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

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

	 	
			com.h2database
			h2
			runtime
		 
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
		
			 org.springframework.boot
			 spring-boot-devtools
			 true 
        
		
		
		
		
		 org.springframework.security
		 spring-security-test
		 test 
		
		  
          org.springframework.boot  
          spring-boot-starter-data-jpa   
           
		  
		   com.oracle  
		    ojdbc14  
		    10.2.0.4.0  
       
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


4、建立实例  entity
package com.example.Spring.Boot1.entity;

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

@Entity  //注明是一个JPA实体
public class Book {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)  //说明这是这个实体的唯一标识  这个是代表id可以自增
	private int id;
	private String reader;
	private String title;
	
	public int getId() {
			return id;
		}
	public void setId(int id) {
			this.id = id;
		}
	public String getReader() {
			return reader;
		}
	public void setReader(String reader) {
			this.reader = reader;
		}
	public String getTitle() {
			return title;
		}
	public void setTitle(String title) {
			this.title = title;
		}		
}

5、新建一个接口继承JpaRepository  JpaRepository


package com.example.Spring.Boot1.JpaRepository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

import com.example.Spring.Boot1.entity.Book;

//仓库接口
public interface BookRepository extends JpaRepository { 
	
/*
 * 上面继承了泛型接口  需要两个参数  一个仓库的领域对象类型  一个是ID的属性	
 */
	
	//通过id查所有的  

	List findAllById(int id);
	
	//找到某对象的id
	Book  findDataById(int id);
}

6、在controller

package com.example.Spring.Boot1.controller;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.Spring.Boot1.JpaRepository.BookRepository;
import com.example.Spring.Boot1.entity.Book;


@RestController    //Spring mvc的控制层  现在要返回的是一个页面所以不能再用@RestController,而用普通的@Controller
public class BookController {
	@Autowired
	private  BookRepository bookRepository; //定义book的数据仓库
	 
	 /**query  查询所有book列表*/  
	   @GetMapping(value = "/book")  
	    public List bookList(){  
	        return bookRepository.findAll();  //这个是book的仓库中自带的方法
	        
	    }  	
	   
	   
	 /** insert  新增book表数据  这个需要用到postman调用测试*/  	
	   @PostMapping(value="/book/add")  
	   public Book addBook(@RequestParam("reader") String reader,  
			               @RequestParam("title") String title){  
			          Book book = new Book();  
			          book.setReader(reader);  
			          book.setTitle(title);  
			          return bookRepository.save(book);  
		 }  
	    /** 
	     * 通过id查询
	        * @param id 
	        * @return 
	        */  

	     @GetMapping(value="/book/getdata/{id}")
	     public List getBookById(@PathVariable("id")  int id){
	    	 return bookRepository.findAllById(id);
	     }
	     
	   
	   /*
	    * 
	    *  删除book  by id
	    * */
	   
	   @DeleteMapping(value="/book/deleteBook/{id}")
	   public void deleteByCap(@PathVariable("id") int id){  
		   bookRepository.delete(bookRepository.findDataById(id));  
		}  
	   
	   
	/** 
     * update更新一个book  by id  
	 * @return 
     * @return 
     */  
  
 @PutMapping(value ="/book/updateBook/{id}")  
   public Book updBookById(@PathVariable("id") int id,@RequestParam( "reader") String reader,@RequestParam("title") String title){  
	  System.out.println("reader"+reader+"title"+title);
	 
	  Book book=new Book();  
	   book.setReader(reader);  
	   book.setTitle(title);
       book.setId(id);
	   return bookRepository.save(book);  
     
 }  
 
  
}




你可能感兴趣的:(Spring,boot)