Spring Data JPA
build.gradle
plugins {
id 'org.springframework.boot' version '2.7.0'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.insight.into.life'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'mysql:mysql-connector-java'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
}
tasks.named('test') {
useJUnitPlatform()
}
添加
org.springframework.boot:spring-boot-starter-web
org.springframework.boot:spring-boot-starter-data-jpa
mysql:mysql-connector-java
application.yml
server:
port: 9000
logging:
level:
org.springframework.security: info
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/auth-center?useUnicode=true&characterEncoding=utf8&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: 123456
jpa:
hibernate:
ddl-auto: update
spring.jpa.hibernate.ddl-auto
的参数可选项有
- none:使用 mysql 数据库时,默认值为 none,对数据库表不会产生任何影响
- update:Hibernate 根据实体变更表结构
- create:每次都会重新创建表,但是应用程序关闭时,不会删除表
- create-drop:创建表,应用程序关闭时,删除表。使用内存数据库,比如 H2 时,默认此选项
使用 mysql 在生产环境中,将 `spring.jpa.hibernate.ddl-auto` 设置为 `none` 是比较好的选择。
实体
package com.insight.into.life.auth.center.domain;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author Zhang_Xiang
* @since 2022/5/16 15:17:25
*/@Entity
@Getter
@Setter
@ToString
@RequiredArgsConstructor
public class Member implements UserDetails {
@Id
@Column(name = "id", nullable = false)
private Long id;
private String loginAccount;
private String password;
private LocalDateTime lastLoginTime;
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return new ArrayList<>() {
{
add(() -> "read");
add(() -> "write");
}
};
}
@Override
public String getPassword() {
return password;
}
@Override
public String getUsername() {
return loginAccount;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
Repository
创建自定义仓储
package com.insight.into.life.auth.center.repository;
import com.insight.into.life.auth.center.domain.Member;
import org.springframework.data.repository.CrudRepository;
import java.util.Optional;
/**
* @author Zhang_Xiang
* @since 2022/5/20 15:36:41
*/public interface MbrRepository extends CrudRepository<Member, Long> {
Optional<Member> findByLoginAccount(String loginAccount);
}