Springboot整合SpringDataJpa入门篇(一)

    • spring-data-Jpa简介                                                                                                                                                                                                                                                                                          首先我们已经知道JPA(Java Persistence  API),中文名是Java 持久层 Api,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。它的诞生是为了整合第三方ORM框架,建立统一的标准。ORM框架中最著名的就是Hibernate,在mybaits没有流行之前,Hibernate是程序员最喜欢用的持久层框架之一,功能也非常强大。而事实上,Jpa的实现都是用Hibernate做的。spring-data-jpa又是什么呢?从名字就可以看出是spring和JPA的整合。使用spring-data-jpa最大的好处在于,我们在进行开发的过程中,常用的功能,我们几乎不需要写一条sql语句。下面开始介绍springdatajpa的使用方法:
    • spring-data-Jpa
    1. 在pom文件中引入依赖

        <!--springdatajpa依賴-->
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-data-jpa</artifactId>
                  <version>2.1.3.RELEASE</version>
              </dependency>
              <!--mysql 依賴-->
              <dependency>
                  <groupId>mysql</groupId>
                  <artifactId>mysql-connector-java</artifactId>
                  <scope>runtime</scope>
              </dependency>  
    2. 在application.properties中加入连接数据库的条件,我这里使用的是MySQL。

      server.port=8081
      spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
      spring.datasource.username=root
      spring.datasource.password=123456
      spring.datasource.driver-class-name=com.mysql.jdbc.Driver
      
      #配置自动建表
      spring.jpa.hibernate.ddl-auto=update
      spring.jpa.show-sql=true
      

        

    3. 接下来是写持久层的代码,很简单只有实现JpaRepository接口就可以实现基本的查询功能,其中Student是实体类,Integer是主键的类型

      public interface StudentRepository extends JpaRepository<Student,Integer> {
      
      }
      

      Student.java的代码。@Entitry表示该类是实体类,@Table在MySQL创建的表名,项目一启动就会自动创建,@Data需要引入lombok,这里我是为了方便,可以不用写get/set方法。

      @Data
      @Entity
      @Table(name = "tb_student")
      @Component
      public class Student {
          @Id
          @GeneratedValue(strategy = GenerationType.AUTO)
          private Integer id ;
          @Column(name = "name",length = 50)
          private String name;
          @Column(name = "email",length = 100)
          private String email;
      }  

       controller层代码,我是跳过了业务层直接跳持久层了,当然实际开发是不会这样的。

      @RestController
      public class StudentController {
          @Autowired
          StudentRepository studentRepository;
           @Autowired
           Student student;
      
          @RequestMapping("/getAll")
          public List<Student> queryStudent(){
              List<Student> list  = new ArrayList<>();
              list = studentRepository.findAll();
              return list  ;
      
          }
           @RequestMapping("/getByID")
          public Optional<Student> queryStudentbyid() {
              List<Student> list = new ArrayList<>();
              Optional<Student> student= studentRepository.findById(1);
              return student;
          }
      
          @RequestMapping("/add")
         public Student addStudent(){
              student.setId(121111);
              student.setEmail("wewsdsdfs");
              student = studentRepository.save(student);
               return student;
         }
      
      }
      

        在浏览器访问的结果

      "id":1,"name":"qinda","email":"[email protected]"},{"id":2,"name":"haha","email":"121"},{"id":3,"name":null,"email":"wewsdsdfs"}]  
    4.  

原文地址:https://www.cnblogs.com/qinda/p/10965178.html

时间: 2024-08-30 16:16:39

Springboot整合SpringDataJpa入门篇(一)的相关文章

SpringBoot整合RabbitMQ入门~~

SpringBoot整合RabbitMQ 入门2020-01-12 创建生产者类,并且在yml配置文件中配置5要素连接MQ yml配置文件 spring: rabbitmq: host: xx.xx.xx.xx port: 5672 virtual-host: / username: 默认guest password: 默认guest 编写生产者代码 使用@Configuration 表名它是配置类 再类中声明三要素 //交换机名称 @Bean("itemTopicExchange")

SpringBoot 整合 MyBatis-Plus 入门体验

一.前言 本文小编将基于 SpringBoot 整合 MyBatis-Plus , MyBatis-Plus 是一个 MyBatis 的增强工具,在 MyBatis 的基础上做增强并且不改变原本功能 ~ 二.SpringBoot整合MyBatis-Plus 基本环境 spring-boot 2.1.8 mybatis-plus 2.2.0 mysql 5.7.24 maven项目 1.pom.xml中引入MyBatis-Plus相关依赖 下面直接贴出小编的整个文件内容以作参考,避免因为部分细节缺

Springboot(一):入门篇

什么是spring boot spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.用我的话来理解,就是spring boot其实不是什么新的框架,它默认配置了很多框架的使用方式,就像maven整合了所有的jar包,spring boot整合了所有的框架(不知道这样比喻是否合适). 使用spring boot有什么好处 其实就是简单.快速.方便!平时如果我

SpringBoot系列之入门篇(一)

前言 前面我们学习了Java基础和算法,接下来我们通过学习SpringBoot基础来强化代码书写能力,只有通过基础项目实践才能发现问题,在此过程中我们则可以进一步学习JVM虚拟机和线程知识,岂不两全其美,好了,话不多讲,我们开始. SpringBoot入门 在还未接触SpringBoot时,自我感觉是一个全新的框架,可能需要花费很多时间去学习,但当我了解完基础原理后,发现并不是全新学习,一切通过注解来进行配置从而实现IOC,相对.NET Core而言不过是在表达方式上使用不同罢了,可以理解为换汤

springboot整合springDataJPA(替代了MyBatis)

SpringDataJPA不需要表直接使用注解,是对JPA的封装. 需求:查询数据库---->查到数据---->展示到页面上 分析: 1.创建数据表:user表(此处建表,只是为了依赖导入失败,注解无法使用时备用) DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) DEFAULT NULL, `password` varc

七、springboot整合Spring-data-jpa(二)之通用DAO接口与添加自定义方法

@NoRepositoryBean:Spring Data Jpa在启动时就不会去实例化BaseRepository这个接口 1.通用接口: import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.repository.N

Spring Boot入门系列六( SpringBoot 整合thymeleaf)

SpringBoot 整合thymeleaf 一.什么是Thymeleaf模板 Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎.类似JSP,Velocity,FreeMaker等,它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用的模板引擎.与其它模板引擎相比,Thymeleaf最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个Web应用.它的功能特性如下: @Controller中的方法可以直接返回模板名称,接下来Thyme

Springboot 整合RabbitMq ,用心看完这一篇就够了

Springboot 整合RabbitMq ,用心看完这一篇就够了 https://blog.csdn.net/qq_35387940/article/details/100514134包括有rabbitMq相关的一些简单理论介绍,provider消息推送实例,consumer消息消费实例,Direct.Topic.Fanout的使用,消息回调.手动确认等. (但是关于rabbitMq的安装,就不介绍了)——————————————————————————————————————————————

【使用篇】SpringBoot整合Filter(二)

两种方式: 通过注解扫描完成 Filter 组件的注册 通过方法完成 Filter 组件的注册 一.通过注解扫描完成 Filter 组件的注册 1. 编写Filter类 /** * SpringBoot整合Filter方式一 * * 传统方式 * <filter> * <filter-name>FirstFilter</filter-name> * <filter-class>com.linhw.demo.filter.FirstFilter</fil