mysql 5.7和8.0区别_Springboot2.0学习4 一步步实现MySQL操作

14777cfba106b1e7e7eb98fdefa838bf.png

一、说明

  • 本文环境需要:gradle idea jdk1.8+ mysql

二、创建项目

1. idea新建gradle项目

gradle 引用 mysql spring-boot

plugins {    id 'org.springframework.boot' version '2.1.7.RELEASE'    id 'io.spring.dependency-management' version '1.0.8.RELEASE'    id 'java'}group 'com.test'version '1.0-SNAPSHOT'sourceCompatibility = 1.8repositories {    mavenCentral()}dependencies {    testCompile group: 'junit', name: 'junit', version: '4.12'    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'    implementation 'org.springframework.boot:spring-boot-starter-web'    runtimeOnly 'mysql:mysql-connector-java'    testImplementation 'org.springframework.boot:spring-boot-starter-test'}

2. main/resources/application.properties文件

spring.jpa.hibernate.ddl-auto=updatespring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/testspring.datasource.username=springuserspring.datasource.password=ThePassword

说明:

spring.jpa.hibernate.ddl-auto

  • ddl-auto:create----每次运行该程序,没有表格会新建表格,表内有数据会清空
  • ddl-auto:create-drop----每次程序结束的时候会清空表
  • ddl-auto:update----每次运行程序,没有表格会新建表格,表内有数据不会清空,只会更新
  • ddl-auto:validate----运行程序会校验数据与数据库的字段类型是否相同,不同会报错

3. 实体类 User.java

package com.example.accessingdatamysql;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;@Entity // This tells Hibernate to make a table out of this classpublic class User {    @Id    @GeneratedValue(strategy=GenerationType.AUTO)    private Integer id;    private String name;    private String email;    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 String getEmail() {        return email;    }    public void setEmail(String email) {        this.email = email;    }}

4. 创建Repository

package com.example.accessingdatamysql;import org.springframework.data.repository.CrudRepository;import com.example.accessingdatamysql.User;// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository// CRUD refers Create, Read, Update, Deletepublic interface UserRepository extends CrudRepository {}

Controller里可以使用userRepository来进行数据库操作。

5. 创建MainController

package com.example.accessingdatamysql;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;@Controller // This means that this class is a Controller@RequestMapping(path="/demo") // This means URL's start with /demo (after Application path)public class MainController {    @Autowired // This means to get the bean called userRepository    // Which is auto-generated by Spring, we will use it to handle the data    private UserRepository userRepository;    @PostMapping(path="/add") // Map ONLY POST Requests    public @ResponseBody String addNewUser (@RequestParam String name            , @RequestParam String email) {        // @ResponseBody means the returned String is the response, not a view name        // @RequestParam means it is a parameter from the GET or POST request        User n = new User();        n.setName(name);        n.setEmail(email);        userRepository.save(n);        return "Saved";    }    @GetMapping(path="/all")    public @ResponseBody Iterable getAllUsers() {        // This returns a JSON or XML with the users        return userRepository.findAll();    }}

6. 程序入口 AccessingDataMysqlApplication

package com.example.accessingdatamysql;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class AccessingDataMysqlApplication {    public static void main(String[] args) {        SpringApplication.run(AccessingDataMysqlApplication.class, args);    }}

创建好的项目结构:

946e79735ec3e56895cc57cddce40c69.png

三、MySQL 新建测试数据库

5bc733d29ca1586b36fb8dbfc2dad957.png

四、测试

新增数据

 curl localhost:8080/demo/add -d name=First -d email=someemail@someemailprovider.com
c145a9d87a3f2ca6baf2845408a7b290.png

查看数据

在浏览器打开: http://localhost:8080/demo/all

显示:

[{"id":10,"name":"First","email":"someemail@someemailprovider.com"}]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值