Springboot+hibernate+JPA environment construction

1. First create a new springboot project, and refer to the article for the construction process. The development steps of Maven project based on springboot (1) (2) (3) (4) https://blog.51cto.com/13082457/2431409

2. Add the required dependencies to the pom.xml file:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
</dependency>
        <!-- 引进JPA -->
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
        <!-- mysql连接的jar包 -->
    <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
    </dependency>
        <!-- servlet 依赖. -->
    <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
    </dependency>
3、application.properties文件中需要配置数据库连接等项
##视图解析
spring.mvc.view.prefix=/html/
spring.mvc.view.suffix=.jsp

##数据库连接
spring.jpa.hibernate.ddl-auto=update
#hibernate在数据库生成hibernate_sequence表
spring.jpa.hibernate.use-new-id-generator-mappings=true
spring.datasource.driver-class-taskname=com.mysql.jdbc.Driver
#加useSSL=true的原因是MySQL在高版本需要指明是否进行SSL连接。
#SSL协议提供服务主要 :
# 1)认证用户服务器,确保数据发送到正确的服务器;
# 2)加密数据,防止数据传输途中被窃取使用;
# 3)维护数据完整性,验证数据在传输过程中是否丢失;
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=true
spring.datasource.username=root
spring.datasource.password=

##显示sql语句
spring.jpa.properties.hibernate.show_sql=true
##tomcat##
server.tomcat.uri-encoding=UTF-8
#初始化连接
spring.datasource.tomcat.initial-size=10
#最大空闲连接
spring.datasource.tomcat.max-idle=20
#最小空闲连接
spring.datasource.tomcat.min-idle=5
#最大连接数量
spring.datasource.tomcat.max-active=1000
#是否在自动回收超时连接的时候打印连接的超时错误
spring.datasource.tomcat.log-abandoned=true
#是否自动回收超时连接
spring.datasource.tomcat.remove-abandoned=true
#超时时间(以秒数为单位)
spring.datasource.tomcat.remove-abandoned-timeout=180
##<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->
spring.datasource.tomcat.max-wait=1000
spring.datasource.tomcat.test-while-idle=true
#每隔五分钟检测空闲超过10分钟的连接
spring.datasource.tomcat.min-evictable-idle-time-millis=600000
spring.datasource.tomcat.time-between-eviction-runs-millis=300000

4. Next, we create a new controller class, define a method, start the project, and start successfully to prove that the springboot project has been completed.
Springboot+hibernate+JPA environment construction
5. Next, we need to configure how to access the database. JPA is based on Hibernate, so we create the following entity classes, which will automatically create tables.
Springboot+hibernate+JPA environment construction

6. The DAO layer only needs to define an interface, inheriting CrudRepository<UserTest,Integer>.
Springboot+hibernate+JPA environment construction
7. Create a new controller class and test it.
Springboot+hibernate+JPA environment construction
8. Enter the following address in the browser and visit
http://localhost:8080/testUser/getTestUserById/1

Guess you like

Origin blog.51cto.com/13082457/2542501