For more information, click to understand: https://how2j.cn/k/springboot/springboot-jpa/1648.html
table of Contents
Step 1: JPA concept
Step 2: Create a database
Step 3: create table
Step 4: prepare data
Step 5: Run first, see the effect, and then learn
Step 6: imitation and troubleshooting
Step 7: Based on the previous knowledge points
Step 8: application.properties
Step 9: pom.xml
Step 10: Category
Step 11: CategoryDAO
Step 12: CategoryController
Step 13: listCategory.jsp
Step 14: restart the test
Step 15: more about JPA
Step 1: JPA concept
JPA (Java Persistence API) is a Java persistence specification officially proposed by Sun to facilitate the operation of the database.
The real work may be Hibernate, TopLink, etc. different vendors that implement the JPA specification, the default is Hibernate.
This knowledge demonstrates how to use JPA in Springboot quickly and easily.
Step 2: Create a database
Create a database, the name is how2java
create database how2java; |
Step 3: create table
Create a classification table, the fields are very simple, just id and name
use how2java; CREATE TABLE category_ ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(30), PRIMARY KEY (id) ) DEFAULT CHARSET=UTF8; |
Step 4: prepare data
Insert 4 data
insert into category_ values(null,'category 1'); insert into category_ values(null,'category 2'); insert into category_ values(null,'category 3'); insert into category_ values(null,'category 4'); |
Step 5: Run first, see the effect, and then learn
The old rule is to download the runnable project in the download area (click to enter) first , configure it and run it, and then learn what steps have been taken to achieve this effect.
Test address:
http: //127.0.0.1:8080/listCategory |
Note: The startup method is unique to Springboot, directly run the main method of the class: com.how2java.springboot.Application .
Step 6: imitation and troubleshooting
After ensuring that the runnable project can run correctly, follow the steps of the tutorial and imitate the code again.
The imitation process will inevitably have code discrepancies, resulting in the failure to obtain the expected running results. At this moment, compare the correct answer (runnable project) with your own code to locate the problem.
In this way, learning is effective and troubleshooting is efficient , which can significantly increase the speed of learning and cross all barriers on the learning path.
It is recommended to use diffmerge software for folder comparison. Compare your own project folder with my runnable project folder.
This software is very powerful, you can know which two files in the folder are wrong, and you can clearly mark them.
Here is a green installation and usage tutorial: diffmerge download and usage tutorial
Step 7: Based on the previous knowledge points
This knowledge point is developed based on error handling , so it is best to learn the previous content, otherwise some content may not be understood
Step 8: application.properties
Added required parameters for database link
spring.jpa.properties.hibernate.hbm2ddl.auto=update |
Indicates that the table structure will be updated automatically, so this step of creating the table is actually unnecessary~
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp spring.datasource.url=jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=admin spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.properties.hibernate.hbm2ddl.auto=update |
Step 9: pom.xml
Add support for mysql and jpa
<!-- mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version> 5.1 . 21 </version> </dependency> <!-- jpa--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> |
<? 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 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion >4.0.0</ modelVersion > < groupId >com.how2java</ groupId > < artifactId >springboot</ artifactId > < version >0.0.1-SNAPSHOT</ version > < name >springboot</ name > < description >springboot</ description > < packaging >war</ packaging > < parent > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-parent</ artifactId > < version >1.5.9.RELEASE</ version > </ parent > < dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-tomcat</ artifactId > </ dependency > < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >3.8.1</ version > < scope >test</ scope > </ dependency > <!-- servlet依赖. --> < dependency > < groupId >javax.servlet</ groupId > < artifactId >javax.servlet-api</ artifactId > </ dependency > < dependency > < groupId >javax.servlet</ groupId > < artifactId >jstl</ artifactId > </ dependency > <!-- tomcat的支持.--> < dependency > < groupId >org.apache.tomcat.embed</ groupId > < artifactId >tomcat-embed-jasper</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-devtools</ artifactId > < optional >true</ optional > <!-- 这个需要为 true 热部署才有效 --> </ dependency > <!-- mysql--> < dependency > < groupId >mysql</ groupId > < artifactId >mysql-connector-java</ artifactId > < version >5.1.21</ version > </ dependency > <!-- jpa--> < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-data-jpa</ artifactId > </ dependency > </ dependencies > < properties > < java.version >1.8</ java.version > </ properties > < build > < plugins > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > </ plugin > </ plugins > </ build > </ project > |
Step 10: Category
增加一个包:com.how2java.springboot.pojo,然后创建实体类Category。
@Entity 注解表示这是个实体类
@Table(name = "category_") 表示这个类对应的表名是 category_ ,注意有下划线哦
@Id 表明主键
@GeneratedValue(strategy = GenerationType.IDENTITY) 表明自增长方式
@Column(name = "id") 表明对应的数据库字段名
package com.how2java.springboot.pojo; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table (name = "category_" ) public class Category {
@Id @GeneratedValue (strategy = GenerationType.IDENTITY) @Column (name = "id" ) private int id; @Column (name = "name" ) private String name; public int getId() {
return id; } public void setId( int id) {
this .id = id; } public String getName() {
return name; } public void setName(String name) {
this .name = name; } } |
步骤 11 : CategoryDAO
增加一个包:com.how2java.springboot.dao,然后创建dao接口CategoryDAO,继承了JpaRepository,并且提供泛型<Category,Integer> 表示这个是针对Category类的DAO,Integer表示主键是Integer类型。
JpaRepository 这个父接口,就提供了CRUD, 分页等等一系列的查询了,直接拿来用,都不需要二次开发的了。
package com.how2java.springboot.dao; import org.springframework.data.jpa.repository.JpaRepository; import com.how2java.springboot.pojo.Category; public interface CategoryDAO extends JpaRepository<Category,Integer>{
} |
步骤 12 : CategoryController
增加一个包:com.how2java.springboot.web,然后创建CategoryController 类。
1. 接受listCategory映射
2. 然后获取所有的分类数据
3. 接着放入Model中
4. 跳转到listCategory.jsp中
package com.how2java.springboot.web; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.how2java.springboot.dao.CategoryDAO; import com.how2java.springboot.pojo.Category; @Controller public class CategoryController {
@Autowired CategoryDAO categoryDAO; @RequestMapping ( "/listCategory" ) public String listCategory(Model m) throws Exception {
List<Category> cs=categoryDAO.findAll(); m.addAttribute( "cs" , cs); return "listCategory" ; } } |
步骤 13 : listCategory.jsp
Use jstl to traverse the collection passed from CategoryController : cs.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> < table align = 'center' border = '1' cellspacing = '0' > < tr > < td >id</ td > < td >name</ td > </ tr > < c:forEach items = "${cs}" var = "c" varStatus = "st" > < tr > < td >${c.id}</ td > < td >${c.name}</ td > </ tr > </ c:forEach > </ table > |
Step 14: restart the test
Because the dependency of the jar package is added to pom.xml, only the hot deployment of Springboot itself will not work, you have to restart it manually.
Then visit the test address:
http: //127.0.0.1:8080/listCategory |
Observe the effect as shown in the figure
Step 15: more about JPA
There is a JPA CRUD and paging at the back , you can learn more about JPA
For more information, click to understand: https://how2j.cn/k/springboot/springboot-jpa/1648.html