SpringBoot整合JPA
操作录像
SpringBoot整合JPA
视频传送门地址 >> https://www.bilibili.com/video/BV1Yb4y1R74y/
相关文章:
《配置maven仓库 手写maven项目 配置maven打包》
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.ray</groupId>
<artifactId>helloworld</artifactId>
<version>1.0-SNAPSHOT</version>
<name>helloworld</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
</dependencies>
<build>
</build>
</project>
Main.java
package org.ray.helloworld;
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);
}
}
BaseController.java
package org.ray.helloworld.controllers;
import org.ray.helloworld.entities.UsersEntity;
import org.ray.helloworld.services.BaseService;
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;
@RestController
public class BaseController {
@Autowired
private BaseService baseService;
@ResponseBody
@RequestMapping("/")
public String root() {
UsersEntity usersEntity = baseService.login();
return "<h1>hello world in springboot!</h1>";
}
}
UsersEntity.java
package org.ray.helloworld.entities;
import javax.persistence.*;
import java.util.Objects;
@Entity
@Table(name = "users", schema = "helloworld", catalog = "")
public class UsersEntity {
private int id;
private String userName;
private String userPwd;
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Basic
@Column(name = "user_name")
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Basic
@Column(name = "user_pwd")
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UsersEntity that = (UsersEntity) o;
return id == that.id &&
Objects.equals(userName, that.userName) &&
Objects.equals(userPwd, that.userPwd);
}
@Override
public int hashCode() {
return Objects.hash(id, userName, userPwd);
}
}
BaseService.java
package org.ray.helloworld.services;
import org.ray.helloworld.dao.UserRepository;
import org.ray.helloworld.entities.UsersEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BaseService {
@Autowired
private UserRepository userRepository;
public UsersEntity login() {
UsersEntity usersEntity = new UsersEntity();
usersEntity.setId(1);
userRepository.delete(usersEntity);
System.out.println(usersEntity.getId());
return usersEntity;
}
}
UserRepository.java
package org.ray.helloworld.dao;
import org.ray.helloworld.entities.UsersEntity;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<UsersEntity, Integer> {
}
application.properties
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/helloworld?useSSL=false&serverTimezone=UTC
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver