Spring Data JPA
JPA and Spring Data JPA
Reference: https://blog.csdn.net/cd546566850/article/details/107180272/ Have the most intuitive understanding of JPA and Spring Data JPA through this article
Springboot integrates Spring Data JPA
- pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.9</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.liwang</groupId> <artifactId>jpa</artifactId> <version>0.0.1-SNAPSHOT</version> <name>jpa</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
- application.properties
spring.datasource.url=jdbc:mysql:///test2?useUnicode=true &characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=root # The type of database to use spring.jpa.database=mysql spring.jpa.database-platform=mysql # show sql spring.jpa.show-sql=true # When a new field is added to the java entity class, when the application is re-run, the field will be added to the new column in the database table, but the pre-existing column or constraint will not be removed spring.jpa.hibernate.ddl-auto=update # Configure the database dialect. By default, the myisam engine is used to create database tables, and the specified dialect is to use the innodb engine spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
- Create a Book class
@Entity(name = "t_book") public class Book {<!-- --> @Id @GeneratedValue(strategy = GenerationType. IDENTITY) private Long id; private String name; private String author; @Override public String toString() {<!-- --> return "Book{" + "id=" + id + ", name='" + name + '\'' + ", author='" + author + '\'' + '}'; } 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 String getAuthor() {<!-- --> return author; } public void setAuthor(String author) {<!-- --> this.author = author; } }
- At this time, test run project, check whether the book table is generated under the database test2
The operation is successful and the table is created
- Create BookDao interface to implement JpaRepository
public interface BookDao extends JpaRepository<Book, Long> {<!-- --> }
- Test save data to database
@SpringBootTest class JpaApplicationTests {<!-- --> @Autowired BookDao bookDao; @Test void contextLoads() {<!-- --> Book b = new Book(); b.setName("Romance of the Three Kingdoms"); b.setAuthor("Luo Guanzhong"); bookDao. save(b); } }
Refresh data table
7. Test query, delete, update
@Test void test01() {<!-- --> // Query all data, now add multiple records to the database List<Book> list = bookDao. findAll(); System.out.println("list = " + list); System.out.println("-------------------"); // query by id Optional<Book> book = bookDao.findById(3L); System.out.println("book = " + book); }
- Test pagination
@Test void test03() {<!-- --> // Paging query page number starts from 0 PageRequest pageRequest = PageRequest.of(0, 3, Sort.by(Sort.Order.asc("id"))); Page<Book> page = bookDao. findAll(pageRequest); System.out.println("Total records = " + page.getTotalElements()); System.out.println("total pages = " + page.getTotalPages()); System.out.println("Data found = " + page.getContent()); }
At this point, the simple use of Spring Data Jpa is over. Continue to update the use of custom data operations in the future.
You can check out my personal blog:
Website: https://www.fuzm.wang / https://liwangc.gitee.io
————————–
As a beginner, I have not mastered a lot of knowledge, forgive me, please point out any mistakes, in order to improve, thank you! . There will be new learning in the follow-up, and we will continue to add it.