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

边栏推荐
- Adding, deleting, querying and modifying MySQL tables
- 明天就是PMP考试了(6月25日),这些大家都了解了吗?
- throttle-debounce.js:一个小型的防抖节流函数库
- 抖音实战~项目关联UniCloud
- 7-9 treasure hunt route
- Tiktok practice ~ project associated unicloud
- 7-8 ladder cloud vertical
- Simpledateformat concrete classes for formatting and parsing dates
- Hydropower project construction scheme based on 3D GIS Development
- 去商场逛街
猜你喜欢

Adding, deleting, querying and modifying MySQL tables

Morris traverse

安装IBM CPLEX学术版 academic edition | conda 安装 CPLEX
Record a Webflux application memory leak troubleshooting

中学校园IP网络广播系统解决方案-校园数字IP广播系统方案设计指南

7-6 铺设油井管道

还在用 SimpleDateFormat 做时间格式化?小心项目崩掉

国内有哪些好的智能家居品牌支持homekit?

Chapter VI skills related to e-learning 5 (super parameter verification)

go 语言指针,值引用和指针引用
随机推荐
Basic data type
Idea creation module prompt already exists
Classic interview questions and answers for embedded engineers
How to use stm32subeide SWV function
Websocket long link pressure test
7-2 求解买股票问题
Helix distance of point
Harmonyos accessing database instances (3) -- use ORM bee to test how good harmonyos is
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 最大子矩阵和问题
Morris traverse
What good smart home brands in China support homekit?
js监听页面或元素scroll事件,滚动到底部或顶部
还在用 SimpleDateFormat 做时间格式化?小心项目崩掉
R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用summary函数获取模型汇总统计信息、解读模型系数交互作用及其显著性
First person singular reading notes
六大行数据治理现状盘点:治理架构、数据标准与数据中台(2022.04)
R language uses the polR function of mass package to build an ordered multi classification logistic regression model, and uses exp function, confint function and coef function to obtain the confidence
R语言使用epiDisplay包的aggregate函数将数值变量基于因子变量拆分为不同的子集,计算每个子集的汇总统计信息、自定义FUN参数为多个统计量函数名称的列表计算多个统计量
7-8 循环日程安排问题