• 微服务 第六章 springboot 通过Spring-data-jpa 配置Oracle数据源(简单步骤)


    1、pom.xml新添加依赖包:

         <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
    

    2、在application.properties 文件中配置连接参数:

    spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
    spring.datasource.username=userstudy
    spring.datasource.password=userstudy
    spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
    spring.jpa.properties.hibernate.hbm2ddl.auto=update
    

    3、创建实体类Teacher,由于配置了hibernate.hbm2ddl.auto,在应用启动的时候框架会自动去数据库中创建对应的表。

    @Entity
    public class Teacher {
        @Id
        @GeneratedValue
        private Long id;
    
        @Column(nullable = false)
        private String name;
    @Column(nullable = false) private Integer age; public Teacher() { } public Teacher(String name, Integer age) { this.name = name; this.age = age; } //get set 方法 }

     4、创建Teacher的数据访问接口:

    public interface TeacherRepository extends JpaRepository<Teacher, Long> {
    
        Teacher findByName(String name);
    
        Teacher findByNameAndAge(String name, Integer age);
    
        Teacher save(Teacher teacher);
    }
    

     5、新建Controller类:

    @RestController
    @RequestMapping(value="/Teacher")
    public class TeacherController {
        @Autowired
        private TeacherRepository teacherRepository;
    
        @RequestMapping(value="/findByName")
        public Teacher findByName(String name){
            Teacher teacher=teacherRepository.findByName(name);
            return teacher;
        }
    
        @RequestMapping(value="/findAll")
        public List<Teacher> findAll(){
            List<Teacher> teacher=teacherRepository.findAll();
            return teacher;
        }
    }
    

     6、启动项目后测试:

  • 相关阅读:
    洛谷 1850 NOIP2016提高组 换教室
    2018牛客多校第三场 C.Shuffle Cards
    2018牛客多校第一场 B.Symmetric Matrix
    2018牛客多校第一场 A.Monotonic Matrix
    2018牛客多校第一场 D.Two Graphs
    2018宁夏邀请赛L Continuous Intervals
    2018宁夏邀请赛K Vertex Covers
    BZOJ
    HDU
    ACM International Collegiate Programming Contest, Egyptian Collegiate Programming Contest (ECPC 2015)
  • 原文地址:https://www.cnblogs.com/yaohuiqin/p/9394365.html
Copyright © 2020-2023  润新知