首页 > 其他分享 >Springboot简单整合JPA示例

Springboot简单整合JPA示例

时间:2023-01-12 18:11:14浏览次数:55  
标签:publisher publisher0 Springboot 示例 JPA 数据库 _. 注解 name

Springboot整合JPA
https://blog.csdn.net/wdy00000/article/details/123588201

文章目录

JPA技术

JPA 数据持久化的ORM全自动框架。ORM 框架(对象——关系映射)

常用注解

这里的一些注解解释如下:

  • @Entity 是一个类注解,用来注解该类是一个实体类用来进行和数据库中的表建立关联关系,首次启动项目的时候,默认会在数据中生成一个同实体类相同名字的表(table),也可以通过注解中的 name 属性来修改表(table)名称, 如@Entity(name=“user”) , 这样数据库中表的名称则是 user 。该注解十分重要,如果没有该注解首次启动项目的时候你会发现数据库没有生成对应的表。

  • @Table 注解也是一个类注解,该注解可以用来修改表的名字,该注解完全可以忽略掉不用,@Entity 注解已具备该注解的功能。

  • @Id 类的属性注解,该注解表明该属性字段是一个主键,该属性必须具备,不可缺少。

  • @GeneratedValue 该注解通常和 @Id 主键注解一起使用,用来定义主键的呈现形式,该注解通常有多种使用策略,先总结如下:

  • @GeneratedValue(strategy= GenerationType.IDENTITY) 该注解由数据库自动生成,主键自增型,在 mysql 数据库中使用最频繁,oracle 不支持。

  • @GeneratedValue(strategy= GenerationType.AUTO) 主键由程序控制,默认的主键生成策略,oracle 默认是序列化的方式,mysql 默认是主键自增的方式。

  • @GeneratedValue(strategy= GenerationType.SEQUENCE) 根据底层数据库的序列来生成主键,条件是数据库支持序列,Oracle支持,Mysql不支持。

  • @GeneratedValue(strategy= GenerationType.TABLE) 使用一个特定的数据库表格来保存主键,较少使用。

Springboot整合JPA

1.引入JPA依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
  • 1
  • 2
  • 3
  • 4

2.配置

文件位置resources——config包——application.yml
1.启动数据库 ,新建一个数据库 mybatis_db
2.自定义端口号:8099
3.配置数据源:mybatis_db
4.json时间处理
5.jpa基本设置

server:
  port: 8099
#数据源配置
spring:
  datasource:
    druid:    //配置数据源 druid  数据库名:mybatis_db 
      url: jdbc:mysql://localhost:3306/mybatis_db?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
      username: root
      password: 1234
      driver-class-name: com.mysql.cj.jdbc.Driver
  #json时间处理
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
  jpa:
    hibernate:
      ddl-auto: update   //自动更新
    show-sql: true    //日志中显示sql语句
logging:
  level:
    com.dyit.springboot: debug
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

3.启动类

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

4.实体类

映射数据库的表

@Entity
@Table(name = "Person_tab")  //表
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person {
    @Id   //主键
    @GeneratedValue(strategy = GenerationType.IDENTITY)//根据数据库的主键策略 主键自增
    @Column(name="person_id")  //行 行名
    private Integer id;
    @Column(name="person_name")
    private String name;
    @Column(name="person_gender")
    private String gender;
    @Column(name="person_age")
    private Integer age;
    @Column(name="person_phone")
    private String phone;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

5.定义接口和数据库交互(dao)

dao 与数据库交互的接口,用来处理数据库增删改查

@Repository注解
作为DAO对象(数据访问对象,Data Access Objects),这些类可以直接对数据库进行crud操作。

@Repository
public interface IAccountDao extends JpaRepository<Account,Integer> {
    Account findByUsername(String username);
    @Query(value = "SELECT account_username FROM account_tab",
            nativeQuery = true)
    String[] selectAllUsernames();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里插入图片描述
在这里插入图片描述

6.JPA中使用原生态的SQL语句

@Query注解 对数据库进行操作

value : 取值,要么使用原生SQL,要么使用JPQL
nativeQuery:表示是否采用原生SQL,设为true。诸如select * from tableName

@Repository
public interface IPublisherDao extends JpaRepository<Publisher,Integer> {
    @Query(value = "SELECT * FROM publisher_tab WHERE publisher_name=?1 AND publisher_loc=?2",nativeQuery = true)
    Publisher findByNamexAndLocx(String name,String loc);
}
  • 1
  • 2
  • 3
  • 4
  • 5

持久层(Persistence Layer),即实现数据持久化应用领域的一个逻辑层面,将数据使用者和数据实体相关联。

7.TestDemo

  • 添加居民信息
    residentsDao.save(residents)相当于把新创建的residents居民对象添加到数据库中,这就是持久化过程,在这个过程中主键也会自动添加

@Service
对Service层的注解,Service也叫服务层,被称为服务,可以理解就是对一个或多个DAO进行的再次封装,封装成一个服务,所以这里也就不会是一个原子操作了,需要事物控制。
service层具体要调用已经定义的dao层接口,封装service层业务逻辑有利于通用的业务逻辑的独立性和重复利用性。程序显得非常简洁。
这里调用了居民表的dao接口,使用@Autowired注解自动注入,同时新增一个居民信息,账户表也新增一个账户,调用loginDao接口

@Transactional表示事务的注解 (当前事务指的就是当前对数据库操作的一系列指令)。在执行完用户操作,自动comit
在这里插入图片描述

8.JPA的懒加载

JPA在进行多表查询时,采用懒加载,先加载一张表内容,再从另外一张表根据字段查询

#查询图书表中所有信息,先查图书表再根据出版社id查询出版社信息
Hibernate: select book0_.book_id as book_id1_0_, book0_.book_isbn as book_isb2_0_, book0_.book_publisher as book_pub4_0_, book0_.book_title as book_tit3_0_ from book_tab book0_
Hibernate: select publisher0_.publisher_id as publishe1_1_0_, publisher0_.publisher_loc as publishe2_1_0_, publisher0_.publisher_name as publishe3_1_0_ from publisher_tab publisher0_ where publisher0_.publisher_id=?
Hibernate: select publisher0_.publisher_id as publishe1_1_0_, publisher0_.publisher_loc as publishe2_1_0_, publisher0_.publisher_name as publishe3_1_0_ from publisher_tab publisher0_ where publisher0_.publisher_id=?
Hibernate: select publisher0_.publisher_id as publishe1_1_0_, publisher0_.publisher_loc as publishe2_1_0_, publisher0_.publisher_name as publishe3_1_0_ from publisher_tab publisher0_ where publisher0_.publisher_id=?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里插入图片描述

标签:publisher,publisher0,Springboot,示例,JPA,数据库,_.,注解,name
From: https://www.cnblogs.com/sunny3158/p/17047462.html

相关文章

  • springboot 自动配置 自动监控demo
    1、注解定义@Target({java.lang.annotation.ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic@interfaceUmp{publicabstractSt......
  • IDEA的Services中添加SpringBoot启动和npm启动
    1、添加SpringBoot启动点击页面最下方Services,在弹出框中点击左上角最右侧的+,在点击RunConfigurationType,在弹出框AddConfigurationType中找到SpringBoot点击......
  • 【SpringBoot】配置篇
      POM.XML<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"......
  • SpringBoot不同环境的配置文件讲解
    前言​ 源于工作中发现项目中的的application.yml有多样的application.yml,如下图所示:​ 了解过后发现是因为测试环境和生产环境一些配置可能会有差异,比如数据库的地址、......
  • springboot集成minio
    1、docker安装miniosudodockerrun-d--nameminio--restart=always-p9000:9000-p9001:9001-e"MINIO_ROOT_USER=minioroot"-e"MINIO_ROOT_PASSWORD=minioroot......
  • SpringBoot系列之Redis实现消息队列
    1前言本教程主要讲解的是如何在SpringBoot中用Redis实现消息队列。学习本教程的前提知识点是有SpringBoot、Redis、消息队列等基础。所以默认各位大佬已经掌握以上知识点。......
  • Springboot 整合JPA
    Springboot整合JPAhttps://blog.csdn.net/qq_21344887/article/details/123847180Jpa1.JPA是什么JavaPersistenceAPI:用于对象持久化的APIJavaEE5.0平台标......
  • Springboot使用JPA配置多数据源
    Springboot使用JPA配置多数据源https://zhuanlan.zhihu.com/p/299055754本人在最近的项目中,需要使用JPA配置两个数据源来完成一些功能,以此记录配置过程。方便以后使用:第......
  • OpenGL ES 2.0编程指导阅读笔记(二)你好,三角形:OpenGL ES 2.0示例
    本章覆盖以下内容:用EGL创建屏上表面加载顶点和片元着色器创建程序对象,附加顶点和片元着色器,并链接程序对象设置视点清除colorbuffer渲染一个简单图元使colorbuff......
  • SpringBoot+Mybatis-plus整合easyExcel批量导入Excel到数据库+导出Excel
    SpringBoot+Mybatis-plus整合easyExcel批量导入Excel到数据库+导出Excel 一、前言今天小编带大家一起整合一下easyExcel,之所以用这个,是因为easyExcel性能比较好,不会......