一、创建Maven
1、在pom.xml中添加如下数据
点击刷下按钮
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2、在Java中添加一个com.example包,并在包中创建一个名为Main的类
3、向Main类中添加数据
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class,args);
}
}
4、在resources下创建一个.yml 命名为application
并且添加数据如下
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/restaurant?serverTimezone=Asia/Shanghai
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
mybatis:
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
5、在com.example下添加一个entity的包
在包下创建一个名为User的类
6、在User类中添加数据如下:
import java.util.Date;
public class User {
/**
* 主键
* BIGINT -> Long
*/
private Long id;
/**
* 名字
* varchar -> String
*/
private String name;
private String password;
private String sex;
private int phoneNum;
/**
* 创建时间
* datetime -> java.util.Date
*/
private Date createTime;
private Date updateTime;
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(int phoneNum) {
this.phoneNum = phoneNum;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
}
7、在com.example下添加一个mapper的包
在包下创建名为UserMapper的接口
8、向接口中添加数据
import org.apache.catalina.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Update;
@Mapper
public interface UserMapper {
// @Insert 添加数据
@Insert("INSERT INTO user (name, sex, password, phone_num, create_time, update_time) VALUES (#{name}, #{sex}, #{password}, #{phoneNum}, #{createTime}, #{updateTime})")
int save(User user);
// @Update 修改数据
@Update("UPDATE user SET name=#{name},password=#{password},phone_num=#{phoneNum},sex=#{sex},create_time=#{createTime},update_time=#{updateTime} WHERE id=#{id}")
int update(User user);
}
9、在com.example包下创建一个constroller的包
并且添加一个UserController类
向UserController中添加数据如下:
import com.example.entity.User;
import com.example.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
@Controller
@ResponseBody
public class UserController {
@Autowired
private UserMapper userMapper;
@PostMapping("/save")
public int save(@RequestBody User user){
Date date = new Date();
user.setCreateTime(date);
user.setUpdateTime(date);
return userMapper.save(user);
}
// 修改数据 @PutMapping
@PutMapping("/update")
public int update(@RequestBody User user){
return userMapper.update(user);
}
}
二、打开Postman
1、创建一个文件夹
2、给表命名:学生信息
Create
3、在学生信息文件夹上右击鼠标
Add Folder
输入命名
Create
4、点击Add requests
在Nanm上命名
点击Save to New School
这样Postman就新建完成,
在做如下修改:
1、输入主机地址:http://localhost:8080/save
2、将输入主机地址前面的GET修改成Post
3、点击Body
raw
Text改为JSON
这样就配置完成
三、可以向数据库中添加测试
1、需要先启动Main
2、向Postman中添加数据
3、点击Send
5.打开HeidiSQL,打开user表的数据可以查看有没有添加成功
这样JAVA对数据的添加操作就完成了,修改数据同理