Springboot基础篇(二)根据实体类生成数据库表

来源:互联网 发布:我当鸟人的那几年 知乎 编辑:程序博客网 时间:2024/06/15 01:43

最近越来越迷茫了,不知道自己追求的是什么,好像外界发生的任何事都能影响到自己,可能过了这段时间就好,决定在用一个星期去学springboot的开发,最近在学习上的焦虑也越来越大了。
2017年8月7日


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.Driverspring.datasource.url=jdbc:mysql://localhost:3306/dbgirl?characterEncoding=utf8spring.datasource.username=rootspring.datasource.password=rootspring.jpa.show-sql= truespring.jpa.hibernate.ddl-auto=updatespring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialectspring.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;    }}
原创粉丝点击