JCP(Java Community Process)Java社区进程组织发布的 Java EE 标准之一,任何声称遵循JPA标准的框架,都提供相同的JPA接口访问。保证JPA开发的企业应用能够经过少量的修改就能够在不同的JPA框架下运行
支持功能1. 大数据集2. 事务3. 并发等容器级事务
策略策略策略策略a@MappedSuperclass:标记这个基类存储表的共同字段,继承类直接继承即可@Entity:标记这个实体类是与数据库表映射的@Table:标记这个实体的表信息@Id:标记该属性是主键列@GeneratedValue:标记该主键的生成策略IDENTITY == 自增 == MySQL支持SEQUENCE == 序列 == Oracle支持AUTO == 程序自动帮我们选择策略TABLE == 序列 == JPA提供的一个机制,通过数据库表形式帮助主键自增 = 所有表共用一个自增字段=如下图@Query:自定义SQL语句 = 查询、修改、删除语句value:JPQL、SQL语句nativeQuery:默认false,JPQL语句查询,true本地SQL语句查询@Modify:用于标识 修改、删除的方法@Column:标记该列的信息
@GeneratedValue(strategy = GenerationType.TABLE)的介绍
核心方法核心方法核心对象0. Persistence:创建线程池1. EntityManagerFactory:类似线程池1. 内部维护数据库信息2. 缓存数据1. 维护所有实体管理类对象EntityManager2. EntityManager:类似数据库连接ConnectiongetTransaction:获取事务persist,remove,merge,find(真实对象,即调即查)、getReference(代理对象,用时在查):数据库表记录增删改查3. EntityTransaction:触发Connection事务,事务相关的操作
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();
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_39651356/article/details/125329389
内容来源于网络,如有侵权,请联系作者删除!