JPA (Java Persistence API) is a Java persistence specification officially proposed by Sun. It provides Java developers with an object/association mapping tool to manage relational data in Java applications. His appearance is mainly to simplify the existing persistent development work and integrate ORM technology, and end the current situation where ORM frameworks such as Hibernate, TopLink, and JDO are all engaged in their own business. It is worth noting that JPA is developed based on the full absorption of existing ORM frameworks such as Hibernate, TopLink, and JDO. It has the advantages of ease of use and strong scalability. Judging from the current development community's response, JPA has received great support and praise, including the development teams of Spring and EJB3.0. JPA is a set of specifications, not a set of products, so they are like Hibernate, TopLink, and JDO. If these products implement this JPA specification, then we can call them the implementation product of JPA.
Spring Data JPA is a JPA application framework encapsulated by Spring based on ORM framework and JPA specifications, which allows developers to access and operate data using minimalist code. It provides common functions including additions, deletions, modifications and searches, and is easy to expand! Learning and using Spring Data JPA can greatly improve development efficiency! spring data jpa allows us to free the operation of the DAO layer. Basically, all CRUDs can be implemented by relying on it. After writing a warehouse interface, you can inherit JpaRepository to achieve the most basic functions of adding, deleting, modifying and checking!
1 Add a package reference to the build.gradle file
compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('mysql:mysql-connector-java')2 Add mysql and jpa related settings in the source configuration file
spring: datasource: url: jdbc:mysql://127.0.0.1:3306/test username: root password: root driver-class-name: com.mysql.jdbc.Driver jpa: database: MYSQL show-sql: true #Show the SQL statements processed in the background hibernate: ddl-auto: update #Automatically check whether the entity and the database table are consistent. If it is inconsistent, the database table will be updated
3 Add database table entity, pay attention to some annotations, @Entity represents the data table entity, @Table can specify the data table name, @Id represents the primary key, @GeneratedValue primary key upgrade rule, @Column represents the data column name, etc.
import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;import lombok.Data;@Data@Entity@Table(name = "user")public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "Id") private int id; @Column(name = "Name") private String name;}4 Add a warehousing interface to the data table entity. Because JPA already has its own standard instance, we don’t need to write a simple CURD instance. If there are personalized operations, we will define the interface and implementation.
import com.example.springdemo.model.User;import org.springframework.data.jpa.repository.JpaRepository;public interface SqlUserRepository extends JpaRepository<User, Integer> {}5 Finally, add dependency injection fields in the controller, generally use @Autowired
@RestController@RequestMapping("/mysql")public class MysqlController { @Autowired private SqlUserRepository repository; /** * return list. * * @return */ @RequestMapping("/list") public List<User> dataList() { return repository.findAll(); }}Summarize
The above is how Spring Boot JPA is introduced to you, I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!