SPRINGBOOT系列教材 (十三)- 持久层支持 - SPRINGBOOT中如何运用JPA,简单例子
步骤 1 : JPA概念
JPA(Java Persistence API)是Sun官方提出的Java持久化规范,用来方便大家操作数据库。
真正干活的可能是Hibernate,TopLink等等实现了JPA规范的不同厂商,默认是Hibernate。
本知识演示如何在Springboot中快捷方便地使用JPA。
步骤 2 : 创建数据库
创建数据库,名称是 how2java
create database how2java;
步骤 3 : 创建表
创建个分类表,字段很简单,就id和name
use how2java;
CREATE TABLE category_ (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(30),
PRIMARY KEY (id)
) DEFAULT CHARSET=UTF8;
步骤 4 : 准备数据
插入4条数据
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’);
步骤 8 : application.properties
新增数据库链接必须的参数
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/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
步骤 9 : pom.xml
增加对mysql和jpa的支持
**
mysql
mysql-connector-java
5.1.21
org.springframework.boot
spring-boot-starter-data-jpa
**
pom.xml:
4.0.0
com.how2java
springboot
0.0.1-SNAPSHOT
springboot
springboot
war
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
junit
junit
3.8.1
test
javax.servlet
javax.servlet-api
javax.servlet
jstl
org.apache.tomcat.embed
tomcat-embed-jasper
org.springframework.boot
spring-boot-devtools
true
mysql
mysql-connector-java
5.1.21
org.springframework.boot
spring-boot-starter-data-jpa
1.8
org.springframework.boot
spring-boot-maven-plugin
步骤 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,并且提供泛型
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
}
步骤 12 : CategoryController
增加一个包:com.how2java.springboot.web,然后创建CategoryController 类。
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 cs=categoryDAO.findAll();
m.addAttribute("cs", cs);
return "listCategory";
}
}
步骤 13 : listCategory.jsp
用jstl遍历从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”%>
id | name |
${c.id} | ${c.name} |
http://127.0.0.1:8080/listCategory