springboot of springboot pay jpa integration project

springboot integrated spring-jpa

The main contents:

1: spring boot and how integrated spring-jpa jpa first example query

Several commonly used as jpa annotations, lombok use annotations

2: how to set idea of ​​adding a dependency on the pom automatically when the association.

3: integration problems encountered and solutions such as Areas, jpa lazy loading problems mysql..

We follow three-tier model MVC, DAO layer design and development, Service-layer design and development, and Controller layer.

Let's take a DAO layer design and development, Spring boot project to create a basic framework is omitted here.

This article comes from: Kaige Java (kaigejava)

"Spring boot payment programs." Kaige personal blog: www.kaigejava.com

A: add the relevant jar dependent

Because to use the mysql, mysql related to the introduction and operation of database-related jar in POM.XML file. We use here is the spring-jpa to operate the database. Specific jra as follows:

<-! Mysql related dependencies ->

<dependency>

  <groupId>mysql</groupId>

  <artifactId>mysql-connector-java</artifactId>

</dependency>

<-! Jpa its dependencies ->

<dependency>

  <groupId>org.springframework.boot</groupId>

  <artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>


Teach you a idea how the coordinates entered in the pom file when Lenovo, as shown below: When the input atrifactid can think of, as shown below:

document_image_rId9.png

document_image_rId10.png

Operation: file -> settings interface, in turn found: build, execution, Deployment -> Bulid Tools -> maven -> Repositories as shown below:

document_image_rId11.png

Click to update it.

II: database connection configuration. We use here is yml format. FIG configured as follows:

document_image_rId12.png

spring:

 datasource:

   driver-class-name: com.mysql.cj.jdbc.Driver

   username: root

   password: 123456

   url: jdbc:mysql://localhost/springboot-wxpay?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false

 jpa:

   show-sql: true

说明:

spring.jpa.show-sql=true.这句意思是:打印sql语句。

在url后面添加serverTimeizone=UTC是为了解决时区错误的问题。

三:创建实体及测试

3.1:创建ProductCategory实体对象。使用spring-jpa方式:

document_image_rId13.png

说明:

@Entity:spring-jpa实体注解

@Data: lombok注解用于自动生产get/set方法的

@Id:jpa的主键注解

@GeneratedValue:注解生成策略

3.2:创建repository接口对象

document_image_rId14.png

说明:使用jpa的需要继承Jparepository这个对象(有多个,这里就用简单的)

其中泛型,我们查看源码:

document_image_rId15.png

T:实体对象的。也就是我们上面创建的ProductCategory对象

ID:实体对象的ID类型。我们使用的事Integer类型。所以这里就写Integer。

四:创建测试类,进行测试:

在IDEA中,创建测试类快捷键:选中类名之后,ctrl+shift+t。如下图:

document_image_rId16.png

document_image_rId17.png

运行结果:

document_image_rId18.png

在控制台上,我们可以看到hibernate打印的sql语句以及打印出查询的结果。说明springboot继承jpa成功。

如果出现could not initialize proyx的时候,如下图错误:


document_image_rId19.png

在实体上面添加@Proxy(lazy = false)

document_image_rId20.png

说明:@ToString 是直接添加toString方法的。


Guess you like

Origin blog.51cto.com/kaigejava/2439788