Springboot from getting started to getting started (five orm-jpa-1)

wwz_ henu 2022-02-13 05:19:53 阅读数:496

springboot getting started getting started

One . add to  dependency

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

Two . To configure JPA

server:
port: 80
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
username: root
password: root # Database name 、 Change the user name and password to your own
type: com.alibaba.druid.pool.DruidDataSource
druid:
minIdle: 5 # Minimum number of idle connections
initialSize: 5 # Initialize connection size
maxActive: 5 # maximum connection
stat-view-servlet:
login-username: admin
login-password: 123456
jpa:
database: MySQL
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
show-sql: true
hibernate:
ddl-auto: none

3、 ... and . establish entity

1. establish entity package

2. Configure data sources

 

 3. Generate entity

stay entity package Will generate UserEntity.java file , The file includes the table name 、 Field 、 Get field method 、 Setting field method, etc

package com.example.demo.entity;
import javax.persistence.*;
@Entity
@Table(name = "user", schema = "test", catalog = "")
public class UserEntity {
private int id;
private String name;
private String email;
@Id
@Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Basic
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "email")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserEntity that = (UserEntity) o;
if (id != that.id) return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
if (email != null ? !email.equals(that.email) : that.email != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (email != null ? email.hashCode() : 0);
return result;
}
}

  Four . establish repository

1. establish repository package

2. establish IUserRepository.java

package com.example.demo.repository;
import com.example.demo.entity.UserEntity;
import org.springframework.data.jpa.repository.JpaRepository;
public interface IUserRepository extends JpaRepository<UserEntity,Integer> {
}

5、 ... and . Create a route and access

package com.example.demo;
import com.example.demo.repository.IUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class Demo1Application {
@Autowired
private IUserRepository userRepository;
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
@GetMapping("/user")
public String user() {
// findById Receive primary key value ,1 Demonstrate the data primary key for the database
return userRepository.findById(1).get().getEmail();
}
}

copyright:author[wwz_ henu],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130519502370.html