SpringBoot:HTML和MySQL数据交互实例

1.新建一个demo3工程,选择web、jpa、mysql,一路next,finish

2.在demo上右键,新建一个包,命名为entity 

3.在entity下面新建一个Java class类,命名为Record,输入以下代码

package com.example.demo.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
//不指定表名,那么数据库表的名字叫就叫record
public class Record {
    @Id //指定主键
    @GeneratedValue
    private Long id;

    //    @Column(length = 32) //指定username的长度
    private String username;
    @Column(length = 64)
    private String password;
    @Column(length = 32)
    private String telephone;

    public void setId(Long id){this.id = id;}
    public Long getId(){return id;}
    public void setUsername(String username){this.username = username;}
    public String getUsername(){return username;}
    public void setPassword(String password){this.password = password;}
    public String getPassword(){return password;}
    public void setTelephone(String telephone){this.telephone = telephone;}
    public String getTelephone(){return telephone;}
}

4. 在entity下面新建一个接口RecordRepository(下图拼写错误,是RecordRepository),接口代码如下

package com.example.demo.entity;

import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;

@Transactional
//Record是实体类,Long是数据存储的主键
public interface RecordRepository extends CrudRepository<Record,Long> {

}

 5.新建一个controller包,包下新建一个RecordController类,类中代码如下

package com.example.demo.controller;

import com.example.demo.entity.Record;
import com.example.demo.entity.RecordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class RecordController {
    @Autowired
    private RecordRepository recordRepository;

    @RequestMapping(value = "/addRecord",method = RequestMethod.POST) //POST(CREATE):在服务器新建一个资源,调用insert操作
    public String feedback(Record record){
        recordRepository.save(record);
        return "result";
    }
}

 6.在resource文件夹下新建一个file文件,直接命名为application.yml,里面放入如下代码

spring:
  thymeleaf:
    prefix: classpath:/templates/

  datasource:
    url: jdbc:mysql://localhost:3306/info?serverTimezone=UTC
    username: root
    password: 123456

  jpa:
    hibernate:
      ddl-auto: update
      use-new-id-generator-mappings: false
    show-sql: true

 7.在pom.xml文件中新添加依赖

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

 8. 在templates文件夹下新建两个html文件:index.html和result.html

index.html

<!DOCTYPE html>
<html>
<head>
	<title>学习html+springboot+mysql的交流方式</title>
</head>
<style type="text/css">
	
</style>
<body>
<div class="div1" align="center">
	<form class="form1" action="addRecord" method="post">
		<!-- ID:<input class="id" type="text" name="id" placeholder="请输入ID"><br> -->
		账号:<input type="text" name="username" placeholder="请输入用户名"><br>
		密码:<input type="password" name="password" placeholder="请输入密码"><br>
		号码:<input type="text" name="telephone" placeholder="请输入手机号码"><br><br>

		<input type="submit" value="提交">
		<input type="reset" name="重置">
	</form>
</div>
</body>
</html>

 result.html

<!DOCTYPE html>
<html>
<head>
	<title>返回结果</title>
</head>
<style type="text/css">
	a{
		text-decoration: none;
	}
</style>
<body>
<h1>添加成功,<a href="./index">点此返回</a></h1>
</body>
</html>

Finally

确保mysql和navicate都安装好了,在navicate中新建local连接,在local上右键,新建info数据库,然后运行IDEA即可

 访问localhost:8080,都输入hello,点击提交。之后在navicate中刷新,可以发现info数据库自动创建了一个record表,且个字段都已经添加进去了

发布了126 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_36880027/article/details/104341671