spring boot操作mysql数据库:自动建表,数据添加、查询和修改

1 mysql自动建表

(1)首先使用IntelliJ IDEA新建spring boot工程,然后在pom.xml中加入mysql的依赖:

  <!-- MYSQL -->
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
	</dependency>
  <!-- Spring Boot JPA -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-data-jpa</artifactId>
	</dependency>

(2)在application.properties中添加:

server.port=8080

# Hibernate 相关配置

## 方言
#hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
## 显示Sql
hibernate.show_sql=true
## 自动建表方式
#hibernate.hbm2ddl.auto= update

## 数据库连接
spring.datasource.url=jdbc:mysql://localhost:3306/test

## 用户名
spring.datasource.username=root

## 密码
spring.datasource.password=123456

## 数据库驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

## 建表方式
spring.jpa.properties.hibernate.hbm2ddl.auto=update

# 方言
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

关键的地方在建表方式设置上,只有写了这句才会自动建表:

## 建表方式
spring.jpa.properties.hibernate.hbm2ddl.auto=update

spring.jpa.properties.hibernate.hbm2ddl.auto有几种配置:

  • create:每次加载Hibernate时都会删除上一次生成的表,然后重新生成新表,即使两次没有任何修改也会这样执行,这就导致每次启动都是一个新的数据库,也是导致数据丢失的重要原因。

  • create-drop:每次加载Hibernate时都会生成表,但当SessionFactory关闭时,所生成的表将自动删除。

  • update:最常用的属性值,第一次加载Hibernate时创建数据表(前提是需要先有数据库),以后加载HIbernate时只会根据model更新,即使model已经删除了某些属性,数据表也不会随之删除字段。

  • validate:每次加载Hibernate时都会验证数据表结构,只会和已经存在的数据表进行比较,根据model修改表结构,但不会创建新表。

(3)添加Student.java类

package com.example.demo;

import javax.persistence.*;

@Entity
@Table(name = "student")
public class Student {
    //自增ID
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column(name = "name")
    private String name;

    @Column(name="age")
    private Integer age;

    //需要声明无参数的构造函数
    public Student(){  }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}

在IDEA中运行项目,将会看到在test数据库中生成了student数据表。

2 操作student数据表

新建一个接口StudentRepository,让它继承jpa:

package com.example.demo;

import org.springframework.data.jpa.repository.JpaRepository;

public interface StudentRepository extends JpaRepository<Student,Integer> {
}

新建控制器StudentController.java,用来与浏览器交互:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentRepository studentRepository;
    private static int cnt = 0;

    @RequestMapping("queryAll")
    @ResponseBody
    public List<Student> queryAll(){
        List<Student> list = new ArrayList<Student>();
        list = studentRepository.findAll();
        return list;
    }

    @RequestMapping("add")
    @ResponseBody
    public  void add(){
        Student user = new Student();
		user.setName("junjun"+(cnt++));
		user.setAge(6);
        studentRepository.save(user);
    }

    @RequestMapping("update")
    @ResponseBody
    public  void update(){
        Student user = studentRepository.findById(1).get();
		user.setName("duoduo");
        studentRepository.save(user);
    }

}

IDEA中运行项目,浏览器中打开http://localhost:8080/student/queryAll,可以看到此时没有数据

打开http://localhost:8080/student/add,然后再查询http://localhost:8080/student/queryAll,显示:

访问http://localhost:8080/student/update进行修改,再查询发现数据修改了:

  • 5
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值