关闭

spring boot 学习--04---mysql,jpa

标签: springmysqljdbcjpa
435人阅读 评论(0) 收藏 举报
分类:

mysql配置

  1. 在application.properties配置中新增mysql配置
#################################################
########mysql配置 -datasource
##################################################
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = 
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
  1. 在pom.xml新增依赖
<dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
</dependency>

JPA配置

  1. 在application.properties配置中新增JPA配置
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

2.在pom.xml中新增依赖

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

测试

1.在Demo类中,新增实体标志(@Entity)

package com.springboot.study.bean;

import javax.persistence.Entity;

/**
 * Demo 模型
 * @author like
 *
 */
@Entity
public class Demo {

    private long id;
    private String name;
    private String demo;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDemo() {
        return demo;
    }
    public void setDemo(String demo) {
        this.demo = demo;
    }



}

2.新建数据库test,运行项目,查看数据库中是否新建demo表

3.结果
这里写图片描述

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:89821次
    • 积分:1881
    • 等级:
    • 排名:千里之外
    • 原创:97篇
    • 转载:1篇
    • 译文:0篇
    • 评论:15条
    博客专栏
    最新评论