SpringBoot Series: Four, SpringBoot integrated JPA

JPA first thing to understand is not a product, it is a norm.

Jpa (Java Persistence API) is a Java Sun Crown persistence specification. It provides an object for Java developers / association mapping tools to manage relational data in Java applications.

It appears mainly to simplify the existing persistence ORM technology development and integration, the end of the current Hibernate, TopLink, JDO and other ORM framework for their own business situation

In pom.xml add references: jpa and mysql

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>


Add a database link configuration in the configuration file
when the connection string to specify the format and coding region or will lead to error and Chinese garbled

 

 

 

 

 

Then declare an entity class, add annotations to develop @Table table name, @ Entity class that it is an entity Bean

@Id indicated primary keys, @ GeneratedValue indicated primary key type, the figure is an example of the self-energizing primary key mysql

@Column specify the mapping of database column names, the same name, then you can not write

 

 

 

Create an interface inheritance JpaRepository, then we can use our framework for the packaged CURD 

 

 

 Call Interface look at the effect return

 

 Delete the rest of the modifications will not put up all the same.

Next is the JPA queries, JPA will generate a corresponding query based on the method name

 

 The controller layer to try

 

 Call about the interface, perfect return

 

Delete what is new with this routine, the following is a table

Keyword Sample JPQL snippet
And findByLastnameAndFirstname … where x.lastname = ?1 and x.firstname = ?2
Or findByLastnameOrFirstname … where x.lastname = ?1 or x.firstname = ?2
Is,Equals findByFirstnameIs,findByFirstnameEquals … where x.firstname = ?1
Between findByStartDateBetween … where x.startDate between ?1 and ?2
LessThan findByAgeLessThan … where x.age < ?1
LessThanEqual findByAgeLessThanEqual … where x.age ⇐ ?1
GreaterThan findByAgeGreaterThan … where x.age > ?1
GreaterThanEqual findByAgeGreaterThanEqual … where x.age >= ?1
After findByStartDateAfter … where x.startDate > ?1
Before findByStartDateBefore … where x.startDate < ?1
IsNull findByAgeIsNull … where x.age is null
IsNotNull, NotNull findByAge(Is)NotNull … where x.age not null
Like findByFirstnameLike … where x.firstname like ?1
NotLike findByFirstnameNotLike … where x.firstname not like ?1
StartingWith findByFirstnameStartingWith … where x.firstname like ?1 (parameter bound with appended %)
EndingWith findByFirstnameEndingWith … where x.firstname like ?1 (parameter bound with prepended %)
Containing findByFirstnameContaining … where x.firstname like ?1 (parameter bound wrapped in %)
OrderBy findByAgeOrderByLastnameDesc … where x.age = ?1 order by x.lastname desc
Not findByLastnameNot … where x.lastname <> ?1
In findByAgeIn(Collection ages) … where x.age in ?1
NotIn findByAgeNotIn(Collection age) … where x.age not in ?1
TRUE findByActiveTrue() … where x.active = true
FALSE findByActiveFalse() … where x.active = false
IgnoreCase findByFirstnameIgnoreCase … where UPPER(x.firstame) = UPPER(?1)

 

 

 

 The next tab is the JPA, JPA comes pagination allows us to directly use

 

 

 

Call Interface returns

 

 Out of the box

The next chapter, we talk about integration Mybaits

 

Guess you like

Origin www.cnblogs.com/Tassdar/p/11488021.html