JPA(Java Persistence API)是Sun官方提出的Java持久化规范,用来方便大家操作数据库。
真正干活的可能是Hibernate,TopLink等等实现了JPA规范的不同厂商,默认是Hibernate。
本知识演示如何在Springboot中快捷方便地使用JPA。详细看百度百科
JDK1.8 、IDEA 2018 、Maven 3.5 、Mysql8.0.15
create database testjpa;
新建表
use testjpa;
CREATE TABLE category_ (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(30),
PRIMARY KEY (id)
) DEFAULT CHARSET=UTF8;
添加四条数据
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');
新建HelloController类验证环境
package com.eknaij.springbootjpa;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hellojpa")
public String helloSpringBoot() {
return "Hello Spring Boot JPA";
}
}
运行,在浏览器输入http://localhost:8080/hellojpa
可以看到如下信息:
添加如下依赖:
<!-- 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>
<!-- mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version><!--我装的是8.0.15版本-->
</dependency>
<!-- jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
新增数据库链接必须的参数
spring.jpa.properties.hibernate.hbm2ddl.auto=update
表示会自动更新表结构,包括创建表(所以之前不新建表也是可以的)
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/testjpa?useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=666666
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
由于我用得是Mysql8.0 所以启动类是 com.mysql.cj.jdbc.Driver如果是5.0+的版本请改成com.mysql.jdbc.Driver
package com.eknaij.springbootjpa.pojo;
import javax.persistence.*;
@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;
}
}
@Entity 注解表示这是个实体类
@Table(name = “category_”) 表示这个类对应的表名是 category_ ,注意有下划线
@Id 表明主键
@GeneratedValue(strategy = GenerationType.IDENTITY) 表明自增长方式
@Column(name = “id”) 表明对应的数据库字段名
package com.eknaij.springbootjpa.dao;
import com.eknaij.springbootjpa.pojo.Category;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CategoryDAO extends JpaRepository<Category,Integer> {
}
dao接口CategoryDAO,继承了JpaRepository,并且提供泛型
JpaRepository 这个父接口,就提供了CRUD, 分页等等,可以直接拿来用
package com.eknaij.springbootjpa.controller;
import com.eknaij.springbootjpa.dao.CategoryDAO;
import com.eknaij.springbootjpa.pojo.Category;
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 java.util.List;
@Controller
public class CategoryController {
@Autowired
CategoryDAO categoryDAO;
@RequestMapping("/listCategory")
public String listCategory(Model model) throws Exception {
List<Category> list=categoryDAO.findAll();
model.addAttribute("lists", list);
return "listCategory";
}
}
JSP页面内容如下,为了简化没有设置任何样式:
<%@ 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="${list}" var="c" varStatus="st">
<tr>
<td>${c.id}</td>
<td>${c.name}</td>
</tr>
</c:forEach>
</table>
在浏览器输入http://127.0.0.1:8080/listCategory
看到如下信息则表示成功:
接下来我们讲一下CRUD操作以及分页。
springBoot+jpa的增删差改操作(CRUD)+mysq8.0