Spring Boot Learning Advanced Notes (ii)-SPRING-DATA-JPA

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.