简单快速的用SpringBoot集成JPA

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36481052/article/details/79226073
使用SpringBoot集成Jpa时,应该先了解一下JPA时干什么的,有什么有优势。

JPA:

  JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。结束现在Hibernate,TopLink,JDO等ORM框架各自为营的局面。值得注意的是,JPA是在充分吸收了现有Hibernate,TopLink,JDO等ORM框架的基础上发展而来的,具有易于使用,伸缩性强等优点。Sun引入新的JPA ORM规范出于两个原因:其一,简化现有Java EE和Java SE应用开发工作;其二,Sun希望整合ORM技术,实现天下归一。

优势:

1)标准化
JPA 是 JCP 组织发布的 Java EE 标准之一,因此任何声称符合 JPA 标准的框架都遵循同样的架构,提供相同的访问API,这保证了基于JPA开发的企业应用能够经过少量的修改就能够在不同的JPA框架下运行。
2)容器级特性的支持
JPA框架中支持大数据集、事务、并发等容器级事务,这使得 JPA 超越了简单持久化框架的局限,在企业应用发挥更大的作用。简单方便JPA的主要目标之一就是提供更加简单的编程模型:在JPA框架下创建实体和创建Java 类一样简单,没有任何的约束和限制,只需要使用 javax.persistence.Entity进行注释,JPA的框架和接口也都非常简单,没有太多特别的规则和设计模式的要求,开发者可以很容易的掌握。JPA基于非侵入式原则设计,因此可以很容易的和其它框架或者容器集成。
3)查询能力
JPA的查询语言是面向对象而非面向数据库的,它以面向对象的自然语法构造查询语句,可以看成是Hibernate HQL的等价物。JPA定义了独特的JPQL(Java Persistence Query Language),JPQL是EJB QL的一种扩展,它是针对实体的一种查询语言,操作对象是实体,而不是关系数据库的表,而且能够支持批量更新和修改、JOIN、GROUP BY、HAVING 等通常只有 SQL 才能够提供的高级查询特性,甚至还能够支持子查询。
4)高级特性
JPA 中能够支持面向对象的高级特性,如类之间的继承、多态和类之间的复杂关系,这样的支持能够让开发者最大限度的使用面向对象的模型设计企业应用,而不需要自行处理这些特性在关系数据库的持久化。


首先得引入SpringBoot集成JPA得jar文件:
        <!--JPA-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
第一步:构建StudentEntity实体类: 
import javax.persistence.*;

//这里的别名要和你数据库中的致(它类似于Hibernate)
@Entity(name="student")
public class StudentEntity {

    //id
    @Id
    @GeneratedValue  //自增长
    @PrimaryKeyJoinColumn //主键
    private Integer id;
    //姓名
    @Column
    private String name;
    //年龄
    @Column
    private Integer age;
    //性别
    @Column
    private String sex;
    //地址
    @Column
    private String address;
    //是否删除(0:未删除,1:已删除)
    @Column
    private Integer isDelete;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getIsDelete() {
        return isDelete;
    }

    public void setIsDelete(Integer isDelete) {
        this.isDelete = isDelete;
    }
}
第二步:构建StudentDao接口,继承JpaRepository<T,ID>:
(T:你的实体类名称。ID:你主键ID的数据类型)
package com.demo.dao;

import com.demo.entity.StudentEntity;
import org.springframework.data.jpa.repository.JpaRepository;

public interface StudentDao extends JpaRepository<StudentEntity,Integer> {

}
第三步:StudentService接口:
package com.demo.service;

import com.demo.entity.StudentEntity;

import java.util.List;


/**
 * SpringBoot 集成JPA
 * */
public interface StudentService {

    //JPA 查询所有学生信息
    List findStudent();

    //JPA 查询单个学生信息
    StudentEntity findById(Integer id);

}
第四步:StudentServiceImpl类:
package com.demo.service.impl;

import com.demo.dao.StudentDao;
import com.demo.entity.StudentEntity;
import com.demo.service.StudentService;
import org.springframework.stereotype.Service;

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

@Service("studentServices")
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentDao studentDao;


    //xml 查询
    @Override
    public List findStudent() {
        //执行查询
        List list = studentDao.findAll();
        //返回结果
        return list;
    }

    @Override
    public StudentEntity findById(Integer id) {
        //执行查询
        StudentEntity entity = studentDao.findOne(id);
        //返回结果
        return entity;
    }
}
第五步:StudentController接口:
package com.demo.controller;

public interface StudentController {



    //查询数据(XML)
    String findStudent();


    String findById();

}
第六步:StudentController类:
package com.demo.controller.impl;

import com.demo.controller.StudentController;
import com.demo.entity.StudentEntity;
import com.demo.service.StudentService;
import com.demo.util.dataUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
public class StudentControllerImpl implements StudentController {

    @Resource
    private StudentService studentServices;



    //myBatis 查询数据(XML)
    @RequestMapping("/find")
    @Override
    public String findStudent() {
        //执行查询
        List list = studentServices.findStudent();
        //组装返回数据
        List newList = dataUtil.getData(list);
        //System.out.println("jjjj:"+newList);
        //返回结果
        return newList.toString();
    }

    @RequestMapping("/findId")
    @Override
    public String findById() {
        Integer id = 2;
        StudentEntity entity = studentServices.findById(id);
        String str = entity.getId()+entity.getName()+entity.getAge()+entity.getSex()+entity.getAddress();
        return str;
    }


}
    这就搞完了,你们可以试试,请路过的大神多多指教,如果此文帮到您了,请帮我点个赞,谢谢您。。。。。

猜你喜欢

转载自blog.csdn.net/qq_36481052/article/details/79226073