记一次springboot+jpa+链接oracle的实例以及遇到的问题

解释IDEA mark directory as的作用 蓝色时代表文件可以编辑可以创建文件 黄色时候代表运行时不加载此文件 

 一,maven配置 jpa和oracle驱动



  org.hibernate
  hibernate-entitymanager
  5.0.3.Final



  
      com.oracle
      ojdbc6
      11.2.0.1.0
  


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

      <dependency>

          <groupId>net.sourceforge.nekohtmlgroupId>

          <artifactId>nekohtmlartifactId>

          <version>1.9.22version>

      dependency>

       

      <dependency>

          <groupId>org.springframework.bootgroupId>

          <artifactId>spring-boot-starter-thymeleafartifactId>

dependency>

创建application.application文件在目录 src/main/resoures  resoures 文件夹设置成蓝色 右键此文件夹 mark directory as 设置成socures boot 

-----

spring.jpa.database=oracle
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@10.10.206.98:1521/rhzxtest
spring.datasource.username=rhzxtest
spring.datasource.password=Fw6Me9TTgNDbeCG9
spring.datasource.max-actice=20

spring.jpa.show-sql=true
#spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
spring.jpa.properties.hibernate.hbm2ddl.auto=update
###################################
##### Java Persistence API Spring JPA
###################################

控制层:

package com.gao.Controller;

import com.gao.Inf.GaoRepository;
import com.gao.entity.Gam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


/*@ComponentScan(basePackages = "com.gao.*")
@EnableJpaRepositories(basePackages = "com.gao.*")
@EntityScan(basePackages = "com.gao.*")*/
@RestController
public class configController {
    @Resource
    private GaoRepository  gaoRepository;
    public GaoRepository getGaoRepository() {
        return gaoRepository;
    }

    public void setGaoRepository(GaoRepository gaoRepository) {
        this.gaoRepository = gaoRepository;
    }


    @RequestMapping(value = "/Gam")
    public List findAll() {
        return gaoRepository.findAll();
    }

}

实体类:

package com.gao.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
@Entity
/*@Table(name = "gaohaiming")*/
public class Gam implements Serializable {
    @Id
    @GeneratedValue
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Gam{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

接口:

package com.gao.Inf;

import com.gao.entity.Gam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface GaoRepository extends JpaRepository {
    @Override
    List findAll();
}

启动类:

package com.gao;


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

@EnableAutoConfiguration
@SpringBootApplication
/*@ComponentScan(basePackages = "com.gao.*")
@EnableJpaRepositories(basePackages = "com.gao.*")
@EntityScan(basePackages = "com.gao.*")*/
public class RunApp {
    public static void main(String[] args) {
        SpringApplication.run(RunApp.class, args);
    }
}

 

 

 

 

你可能感兴趣的:(java)