使用JPA根据实体类生成数据库表

springboot数据库(一)-springboot-JPA
JPA:springboot -jpa:数据库的一系列的定义数据持久化的标准的体系

学习的目的是:
利用springboot实现对数据库的操作

第一步:添加springboot-data-jpa和数据库的依赖关系

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
   </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

第二步:编写propertiys文件和yml文件的配置;

jpa:
hibernate:
show-sql: true
ddl-auto: create

第三步:实体类中使用的注解:

@Entity 实体类的注解
@Id 映射到表格中id的属性
@Gernertervalue 添加其自增的属性;

第四步:启动项目是否生成表格

补充的知识点:

根据实体类生成数据库的表配置文件有俩种方式分别是yml和properties文件进行配置

yml文件:

spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/facemap
username: root
password: root

jpa:
    hibernate:
        ddl-auto: update
        show-sql: true

properties文件的写法:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/dbgirl?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root

spring.jpa.show-sql= true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jackson.serialization.indent_output=false

有更加详细介绍
参考网址;
http://blog.csdn.net/u010429286/article/details/52777046

实体类的写法:

package com.example.demo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;

@Entity //实体类的注解
public class Girl {

@Id //@id注意选择这个javax.persistence
@GeneratedValue
private  Integer  id;

private  String   cupSize;

private  Integer   age;


public Girl() {
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getCupSize() {
    return cupSize;
}

public void setCupSize(String cupSize) {
    this.cupSize = cupSize;
}

public Integer getAge() {
    return age;
}

public void setAge(Integer age) {
    this.age = age;
}

}

项目启动好后就可以看到生成的表与类名相同
在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值