SPRING-DATA-JPA: Simplifying the process of database access
Knowledge Reserve: SPRING-DATA-JPA is based on hibernate, so you need to know about hibernate.
First, establish your own database (test) and table (user), the properties of the table, such as
Second, establish the entity class "User.java"
Import Javax.persistence.Column;
Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.Id;
@Entity
Public class User {
@Id
@GeneratedValue
private Long ID;
@Column (nullable = false)
private String name;
@Column (nullable = false)
private Integer Age ;
Public User () {
super ();
}
Public User (String name, Integer age ) {
super ();
This . name = name;
This . Age = Age ;
}
Public Long getId () {
return ID;
}
public void SetId (Long id) {
This . id = ID;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
This . name = name;
}
Public Integer Getage () {
return age ;
}
public void Setage (Integer age ) {
This . Age = Age ;
}
}
Iii. application.properties Adding database Link Properties
spring.datasource.url=jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf-8 &usessl=false
Spring.datasource.username=Root
spring.datasource.password=123456
Spring.datasource.driver-class-name=Com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=Validate
Iv. Creating a database access class
Import Org.springframework.data.jpa.repository.JpaRepository;
Import Org.springframework.data.jpa.repository.Query;
Import Org.springframework.data.repository.query.Param;
Import Com.zh.SpringBootDemo.domain.User;
Public Interface Userrepository extends Jparepository<user, long> {
User findbyname (String name);
User findbynameandage (String name, Integer age );
@Query("from User u where u.name=:name")
User Finduser (@Param ("name") String name);
}
SPRING-DATA-JPA the access method based on the method name resolution or through the @query configuration.
The specific use of the function is very powerful, the use of small partners to learn it alone.
Spring Boot Learning Advanced Notes (ii)-SPRING-DATA-JPA