当前位置:网站首页>JPA学习1 - 概述、JPA、JPA核心注解、JPA核心对象
JPA学习1 - 概述、JPA、JPA核心注解、JPA核心对象
2022-06-24 19:46:00 【嗯嗯**】
JPA
JCP(Java Community Process)Java社区进程组织发布的 Java EE 标准之一,任何声称遵循JPA标准的框架,都提供相同的JPA接口访问。保证JPA开发的企业应用能够经过少量的修改就能够在不同的JPA框架下运行
JPA-Hibernate集成代码
核心注解
@GeneratedValue(strategy = GenerationType.TABLE)的介绍
核心对象
简单使用

pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--hibernate对JPA规范的支持包-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.30.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.6.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<!-- transaction-type JTA:分布式事务,管理多个数据库的链接 RESOURCE_LOCAL:本地事务,单数据库形式 -->
<persistence-unit name="myJpa" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/lrc_blog_test?useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"/>
<property name="hibernate.show_sql" value="true"/>
<!-- value: create:@Entity对应的数据库有表,则删除在重新创建新表 update: @Entity对应的数据库有表,则不删除,无表则创建 none:什么都不操作 -->
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
</persistence>
Book.java
@Getter
@Setter
@Entity
@Table(name = "book")
@ToString
public class Book implements Serializable {
private static final long serialVersionUID = 2095940921263481761L;
/** 主键 - 记录插入自动填充主键处理{@path application.yml} */
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private String id;
@Column(name = "create_time")
private String createTime;
@Column(name = "update_time")
private String updateTime;
/** 记录是否被逻辑删除:0未删除 1逻辑删除 - 逻辑处理值定义{@path application.yml}*/
@Column(name = "is_del")
private Integer isDel;
/** 书籍状态;-1违规 0发布且公开 1发布且私密 - 默认0 */
@Column(name = "status")
private String status;
/** 书名 */
@Column(name = "name")
private String name;
/** 作者 */
@Column(name = "author")
private String author;
/** 作者国籍 */
@Column(name = "country")
private String country;
/** 下载地址 */
@Column(name = "download_url")
private String downloadUrl;
/** 文件类型 */
@Column(name = "file_type")
private String fileType;
/** 阅读感受 */
@Column(name = "reading_feeling")
private String readingFeeling;
/** 书籍分享者 - 昵称 */
@Column(name = "sharer")
private String sharer;
/** 书籍是否违规:0未违规 1违规 - 违规不可显示 */
@Column(name = "is_violation")
private Integer isViolation;
//书籍状态
public final static String STATUS_VIOLATION = "-1";
public final static String STATUS_PUBLISH_PUBLIC = "0";
public final static String STATUS_PUBLISH_PRIVATE = "1";
}
JpaDemoApplicationTests.java
public class JpaDemoApplicationTests {
EntityManagerFactory entityManagerFactory;
EntityManager entityManager;
EntityTransaction transaction;
@Before
public void beforeMethod() {
System.out.println("beforeMethod=============");
entityManagerFactory = Persistence.createEntityManagerFactory("myJpa");
entityManager = entityManagerFactory.createEntityManager();
transaction = entityManager.getTransaction();
//事务开始
transaction.begin();
}
@Test
public void contextLoads() {
System.out.println("contextLoads=============");
Book book = entityManager.find(Book.class, "08b756bca4e9644ba05cbf97fe03a5b0");
System.out.println(book);
//Article article = new Article();
//article.setTitle("来广东啦!!");
//entityManager.persist(article);
}
@After
public void afterMethod() {
System.out.println("afterMethod=============");
//提交事务
transaction.commit();
//释放资源
entityManager.close();
entityManagerFactory.close();
}
}

边栏推荐
- (Smooth)ScrollToPosition doesn't work properly with RecyclerView
- Hello C (I) -- basics of C language
- HMS core discovery Episode 13 live broadcast Preview - building the real world in mobile games
- 企业级~uni-app网络请求封装
- Assessment of public finance of Jilin University in March of the 22nd spring -00091
- HarmonyOS访问数据库实例(3)--用ORM Bee测下HarmonyOS到底有多牛
- Using external Libpcap library on ARM platform
- 去商场逛街
- Nominal resistance table of patch resistors with 5% and 1% accuracy
- 单调栈以及单调栈的应用
猜你喜欢

Monotone stack and its application

Yyds dry goods counting uses xshell to implement agent function

Simpledateformat concrete classes for formatting and parsing dates

Morris遍历

Morris traversal
![[JS] - [stack, team - application] - learning notes](/img/5b/b90ed8d3eb4fc0ab41c6ea8d092d0f.png)
[JS] - [stack, team - application] - learning notes

Fibonacci

Harmonyos accessing database instances (3) -- use ORM bee to test how good harmonyos is

2021-2022 China's financial digitalization "new" insight Industry Research Report

Huawei machine learning service speech recognition function enables applications to paint "sound" and color
随机推荐
如何化解35岁危机?华为云数据库首席架构师20年技术经验分享
7-6 铺设油井管道
选择类排序法
HarmonyOS访问数据库实例(3)--用ORM Bee测下HarmonyOS到底有多牛
Quickly build KVM virtual machine on # yyds dry goods inventory # physical machine
R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses exp function and coef function to obtain the corresponding odds rat
Modify stm32f030 clock source to internal crystal oscillator (HEI)
MySQL 表的增删查改
Start QT program
STM32CubeIDE SWV功能使用方法
7-7 solving mode problems
Common regular expressions
Solution of IP network broadcasting system in Middle School Campus - Design Guide for Campus Digital IP broadcasting system
(Smooth)ScrollToPosition doesn't work properly with RecyclerView
无鸟用的SAP PA证书,刚入行的同行可以考一考
22map introduction and API
Go language pointer, value reference and pointer reference
Andersen global strengthens the Middle East platform with Palestinian member companies
7-6 laying oil well pipeline
The R language uses the matchit package for propensity matching analysis and match The data function constructs the matched sample set, and performs Welch double sample t-test analysis and double inde