当前位置:网站首页>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();
}
}

边栏推荐
- 257. detention of offenders
- Harmonyos accessing database instances (3) -- use ORM bee to test how good harmonyos is
- The R language uses the matchit package for propensity matching analysis and match The data function constructs the matched sample set, and judges the balance of all covariates in the sample after the
- Record a Webflux application memory leak troubleshooting
- The dplyr package select function of R language moves the specified data column in the dataframe data to the first column (the first column) in the dataframe data column
- [JS] - [linked list - application] - learning notes
- How to resolve the 35 year old crisis? Sharing of 20 years' technical experience of chief architect of Huawei cloud database
- [JS] - [string - application] - learning notes
- [JS] - [stack, team - application] - learning notes
- Fibonacci
猜你喜欢
What you must know about time series database!

Morris遍历

Scala IO reads binary files

Tomorrow is the PMP Exam (June 25). Have you understood all this?

Latest development of jetpack compose
![[JS] - [string - application] - learning notes](/img/dc/f35979b094f04c0ee13b3354c7741d.png)
[JS] - [string - application] - learning notes
![[basic knowledge] ~ half adder & full adder](/img/06/7f1ede65dca527c8630285b587a4ba.png)
[basic knowledge] ~ half adder & full adder

Mirror image of sword finger offer binary tree

First person singular reading notes

Is there really something wrong with my behavior?
随机推荐
376. 機器任務
Fibonacci
Understanding openstack network
HMS core discovery Episode 13 live broadcast Preview - building the real world in mobile games
7-2 后序+中序序列构造二叉树
选择类排序法
First person singular reading notes
7-7 digital triangle
抖音实战~实现App端视频上传与发布
Assessment of public finance of Jilin University in March of the 22nd spring -00091
Volcano becomes spark default batch scheduler
Simpledateformat concrete classes for formatting and parsing dates
Inventory of data governance status of six major banks: governance architecture, data standards and data middle office (April 2022)
R语言使用MatchIt包进行倾向性匹配分析、使用match.data函数构建匹配后的样本集合、对匹配后的样本的不同分组对应的目标变量的均值进行Welch双样本t检验分析、双独立样本t检验
Today's sleep quality record 79 points
R language uses GLM function to build Poisson log linear regression model, processes three-dimensional contingency table data to build saturation model, uses summary function to obtain model summary s
企业数据防泄露解决方案分享
7-5 maximal submatrix sum problem
376. machine tasks
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