什么是jpa?
JPA(Java Persistence API)是一种规范,是是Sun官方提出的Java持久化规范,而他的出现主要是为了简化现有的持久化开发工作和整合ORM技术,并且其是在充分吸收了现有Hibernate等ORM框架的基础发展。
什么是Hibernate?
一个开源的对象关系映射框架,对JDBC进行了非常轻量级的对象封装
可以生成SQL语句,自动执行
将POJO与数据简历映射关系,是一个全自动ORM框架
什么是ORM?
对象关系映射(Object Relational Mapping,简称ORM)是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术。 简单的说,ORM是通过使用描述对象和数据库之间映射的元数据,将java程序中的对象自动持久化到关系数据库中
小小练习
![](https://img.haomeiwen.com/i12234015/10a2c20900b96085.png)
练习的目的:
练习使用spring-boot-jpa规范和Hibernate框架完成一次较小的项目,来熟悉规范使用的过程。
练习过程:
1、对pom文件进行添加依赖。
jpa依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
mysql依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
lombok依赖
<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>
web依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
thymeleaf框架依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
bootstrap
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7-1</version>
</dependency>
热部署
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
2、application.properties文件
spring.datasource.url=jdbc:mysql://localhost:3306/springdata_test(此地为你的数据库名字)?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
##连接池中最大的活跃连接数
spring.datasource.tomcat.max-active=20
##连接池中最大、最小的空闲连接数
spring.datasoure.max-idle=8
spring.datasoure.min-idle=8
##初始化连接数
spring.datasoure.initial=10
spring.jpa.database=mysql
# 显示SQL语句
spring.jpa.show-sql=true
##指定DDL mode (none, validate, update, create, create-drop)
spring.jpa.properties.hibernate.hbm2ddl.auto=update
3、entity对象层
![](https://img.haomeiwen.com/i12234015/05fd72143c7e4145.png)
![](https://img.haomeiwen.com/i12234015/99babd2069fcb906.png)
4.数据持久层DAO
![](https://img.haomeiwen.com/i12234015/cd600713f356ef32.png)
![](https://img.haomeiwen.com/i12234015/9d6398cedf1cb656.png)
5、Service实现层
![](https://img.haomeiwen.com/i12234015/3798e50388a5e3d7.png)
![](https://img.haomeiwen.com/i12234015/015295fd060949dc.png)
![](https://img.haomeiwen.com/i12234015/8a4a973f878ab715.png)
![](https://img.haomeiwen.com/i12234015/ac86b14fd0852561.png)
6、Controller层
![](https://img.haomeiwen.com/i12234015/3f261c9415b1b3fe.png)
大致后端就是这样,前端可以自己编写想要的界面,我的如下:
简书页面:
![](https://img.haomeiwen.com/i12234015/9627a19286f08e9e.png)
简书详情页面:
![](https://img.haomeiwen.com/i12234015/9ced3f42d84e859f.png)
效果图:
![](https://img.haomeiwen.com/i12234015/7df6fb0d9cf8b1e2.png)
![](https://img.haomeiwen.com/i12234015/07e2494c19d0ba97.png)
网友评论