搭建一个简易的springBoot+SpringDataJpa+Oracle项目

开发工具:Eclipse+maven

pom.xml

  <parent>
  		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-parentartifactId>
		<version>1.3.2.RELEASEversion>
  parent>
  <packaging>jarpackaging>
 <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-jpaartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
 
       
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
        	<groupId>com.oraclegroupId>
        	<artifactId>ojdbc6artifactId>
        	<version>11.2.0.3version>
        dependency>
    dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

搭建一个简易的springBoot+SpringDataJpa+Oracle项目_第1张图片

springBoot启动类

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


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

实体类


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

@Entity
@Table(name="STUDENT")
public class Student {
	@Id
	@GeneratedValue
	private Integer stunum;
	private Integer age;
	private String stuname;
	public Student() {
	}
	public Student(Integer stunum, Integer age, String stuname) {
		this.stunum = stunum;
		this.age = age;
		this.stuname = stuname;
	}
	public Integer getStunum() {
		return stunum;
	}
	public void setStunum(Integer stunum) {
		this.stunum = stunum;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getStuname() {
		return stuname;
	}
	public void setStuname(String stuname) {
		this.stuname = stuname;
	}
}

DAO

package com.student.dao;

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

import com.student.domian.Student;

public interface StudentRepository extends JpaRepository<Student,Integer> {

}

Controller

package com.student.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.student.dao.StudentRepository;
import com.student.domian.Student;

@RestController
@RequestMapping("/student")
public class StudentController {
	@Autowired
	private StudentRepository studentRepository;
	 @RequestMapping("/findAll")
	    public List<Student> findAll() {
	        List<Student> students = studentRepository.findAll();
	        return students;
	    }
}

配置文件application.properties

spring.datasource.url=jdbc:oracle:thin:@localhost:1521:XE
 
spring.datasource.username=yourName
 
spring.datasource.password=yourPassword
 
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
 
spring.jpa.properties.hibernate.hbm2ddl.auto=update
 
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
 
spring.jpa.show-sql= true
 
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
 
server.port = 9000

oracle 建表语句

create table student(
stunum number(4) primary key,
age number(4),
stuname varchar2(9)
)

insert into student values (1,30,'lisi');
insert into student values (2,31,'zhangsan');
...

接下来启动和访问:

搭建一个简易的springBoot+SpringDataJpa+Oracle项目_第2张图片

大工告成!^ ^

你可能感兴趣的:(数据库)