JPA (Java Persistence API) 定义了一系列对象持久化的标准,目前实现这一规范的产品有Hibernate、TopLink等
直接上练习
项目结构
项目结构.png
一、配置
在pom.xml中添加
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency>
在 application.yml 里 (这里用的 yaml 配置文件,一定要注意缩进,不然后果严重)
spring.jpa.hibernate.ddl-auto 有五个值
spring:profiles:active: proddatasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/testusername: rootpassword: 123456jpa:hibernate:ddl-auto: updateshow-sql: true
在ManIfo里面
@Entity 数据库映射实体
@Id 表明表里的id (一般主键什么的)
@GeneratedValue 自增长
package com.alun;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;/*** Created by Administrator on /5/29.*/@Entitypublic class ManInfo {@Id@GeneratedValueprivate Integer id;private String age;private String nickname;public ManInfo() {}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public String getNickname() {return nickname;}public void setNickname(String nickname) {this.nickname = nickname;}}