springboot+hibernate+JPA环境搭建

1、首先新建一个springboot项目,搭建过程参考文章基于springboot的Maven项目开发步骤(1)(2)(3)(4)https:/P M J J 2 1 K [ X/blog.51cto.com/13082457/2o r ^ % ~431409

2、pom.xml文件中加入需要的] K 8 O依赖:

<depeY A & _ ? u + a 6ndency>
<% - R Q `groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引进JPA --&( I [ 5gt;
<dependency>
<groupId&gj j 2t;orP * ) rg.springframework.boot</group% s / W q o D DId>
<artifactId>spring-boot-starter-data s ) % % d #a-jpa</artifactId>` q Z
</dependency&g= ! . ? F * J [t;
<!-3 - ? 6 _ G A z n- mysql连接h y 0 ` x的jar包 -->
<dl M A w x . Cependency>
<groupId>mysqlD y e = 8</groupId>
<artifactId>mysql-connector-java</artifactId>
</| 8 0 = [ # P Fdependency>
<!-- servlet 依赖. -->
<dependency>
<M ^ o 8 : H;groupId>javax.servlet</groupId>P ^ o # h
<ar) N m 5 }tifactIdQ # K>jstl</ai 8 q 6 ;  ; :rtifactId>
</depende$ - b  3 Vncy>
3、application.properties文件中需要配置数据` E a 7 8 3 P = }库连接6 . F ( P J K ? y等项
##视图解析
spd } 4 M 7ring.mvc.view.prefix=/html/
spring.mvc.view.suffix=.jsp
##数据库连接
spring.jpa.hibernate.ddD F P Ql-auto=update
#hibernaY D Ite在数据库生成hibernate_sequence表
spring.jpa.h. q 7 mibernate.use-new-id-generator-mappings=true
spring.datasource.driver-class-taskname=com.myss 7 D 2 % e 5 + tql.jdbc.Driver
#加useSSL=true的原因是M: , ` O `ySQL在高版本需要指明是否进行SSL连接。
#SSL协议提供f q / T服务主要 :
# 1)认证用户服务器,确保数据发送到正确的服务器;
# 2)加密数据,防止数据传输途中被窃取使用;
# 3)维护数据完整性,验证数据在传输过程中是否丢失;
spring.datasource.url=jdbc:mysql:/o ^ m  A ` T X/12W 6 $7.0.0.1:3306/t3 x o A } R : 8 /est?characterEncoding=utf8&useSSL=true
spring.datasource.username=root
spring.datasource.pa7 c I w =ssword=
##显示sql语句
spring.jpa.properties.hibernat+ # : M % i + me.show_sql=true
#k 0 K } | f % H#tomcat##
server.tomcat.uri-encoding=UTF-8
#初始化连接
spri+ $ J 1 M Mng.datasource.tomcat.initial-size=10
#最大空闲连接
spring.datasource.tomcat.max-idle=20
#最小空闲连接
spring.datasource.tomcat.min-idle=5
#最大连接数量
spring.datasource.tomcat.max-active=1000
#是否在自动回收超时连接的时候打印连接的超时错误
spring.O / @ @datasource.tomcat.log-abandoned=true
#是否自动回收超时连接
spring.dW h D y ratasource.tomcat.remove-abandoned=true
#超时时间(以秒数为单位)
spring= X k T E k G }.datas$ m @ Nource.tomcat.remove-abandoned-timeout=18R O E @ 7 1 2 y0
##<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->
spring.datasource.tomcat.max-wait=1000
spring.datasource.tomcat.t| !  Z |est-while-idle=true
#每隔五分钟9 d H ) j s w ?检测空闲超过10分钟的连接
spring.datasource.tomcat.min-evictable-idle-timek : 2 P 4-millis=600000
sC ` f h L 6 0pring.datasource.tomcam ^ v B O 3 ~ Ety R D n Q ) 6.tiU 5 l h % r i &me-between-eviction-runs-millis=& 8  s { C300000

4、接1 q m t G下来,我们新~ Z 8 x建一个controllP } A S % }er类,定义一个方法,启动项目,启动成功证明Q E X M j { B wspringboo# ) $ z { = [t项4 ( !目已经完成。
springboot+hibernate+JPA环境搭建
5、接下来,我们需要配置如何访问数据库。JPA是基于Hibernate的,因此我们建立以下实体类,会自动创建表。
springboot+hibernate+JPA环境搭建

6、DAO层只需要定义一个接口,继承CrudRepository<UserTest,Integer>即可。
springboot+hibernate+JPA环境搭建
7、新建一: { | 0 A c p ( v个controller类,测试一下
springboot+hibernate+JPA环境搭建
8、在浏览器里输入以下地址s ~ A /,访问一下
http://localhost:808. ~ & [ m + w0/testU& ( =ser/getTestUserById/1