Skip to content

Commit

Permalink
fix(jpa) 完善jpa实例
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhengjiaao committed Apr 15, 2024
1 parent a97db4e commit 30bef48
Show file tree
Hide file tree
Showing 104 changed files with 6,473 additions and 197 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
782 changes: 774 additions & 8 deletions starter-data/starter-data-jpa/README.md

Large diffs are not rendered by default.

Binary file added starter-data/starter-data-jpa/img/OneToMany.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
142 changes: 134 additions & 8 deletions starter-data/starter-data-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,22 @@
<artifactId>starter-data-jpa</artifactId>
<packaging>jar</packaging>

<properties>
<org.mapstruct.version>1.5.3.Final</org.mapstruct.version>
<lombok.version>1.18.20</lombok.version>
<!-- <querydsl.version>4.4.0</querydsl.version>-->
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- QueryDSL支持 -->
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
Expand Down Expand Up @@ -49,26 +60,141 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--swagger3 api docs-->

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.7.16</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<!-- 跳过安装到本地仓库中 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<!-- 跳过部署到私服上 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.hibernate.orm.tooling</groupId>
<artifactId>hibernate-enhance-maven-plugin</artifactId>
<version>${hibernate.version}</version>
<executions>
<execution>
<configuration>
<!--启用简单字段的延迟加载-->
<enableLazyInitialization>true</enableLazyInitialization>
</configuration>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- QueryDSL支持 -->
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<dependencies>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
</dependency>
</dependencies>
<executions>
<!-- 置插件生效的maven生命周期 -->
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<!-- 配置生成文件的目录 -->
<!--<outputDirectory>target/generated-sources/java/</outputDirectory>-->
<outputDirectory>src/generated-sources/java/</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
/**
* @Company: 上海数慧系统技术有限公司
* @Department: 数据中心
* @Author: 郑家骜[ào]
* @Email: [email protected]
* @Date: 2022-02-14 10:59
* @Since:
*/
package com.zja;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

/**
* http://localhost:8080/swagger-ui/index.html#/
* @swagger3: <a href="http://localhost:8080/swagger-ui/index.html">...</a>
* @author: zhengja
* @since: 2023/08/10 13:18
*/
@SpringBootApplication
public class JpaApplication {
public class JpaApplication extends SpringBootServletInitializer {

public static void main(String[] args) {
SpringApplication.run(JpaApplication.class, args);
}

}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(JpaApplication.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @Company: 上海数慧系统技术有限公司
* @Department: 数据中心
* @Author: 郑家骜[ào]
* @Email: [email protected]
* @Date: 2023-09-28 12:32
* @Since:
*/
package com.zja.audit;

import lombok.Data;

/**
* @author: zhengja
* @since: 2023/09/28 12:32
*/
@Data
public class AuditUser {
private String userId;
private String userName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @Company: 上海数慧系统技术有限公司
* @Department: 数据中心
* @Author: 郑家骜[ào]
* @Email: [email protected]
* @Date: 2023-09-27 17:27
* @Since:
*/
package com.zja.audit;

import com.zja.util.IdGeneratorUtil;
import org.springframework.data.domain.AuditorAware;
import org.springframework.util.ObjectUtils;

import java.util.Optional;

/**
* @author: zhengja
* @since: 2023/09/27 17:27
*/
public class AuditorAwareImplA implements AuditorAware<String> {

@Override
public Optional<String> getCurrentAuditor() {

//模拟当前登录用户
AuditUser currentUser = new AuditUser();
currentUser.setUserId(IdGeneratorUtil.objectId()); //当前用户id
currentUser.setUserName("李四");

if (ObjectUtils.isEmpty(currentUser)) {
return Optional.empty();
}

//一般项目中从spring security或token中获取
return Optional.of(currentUser.getUserId());
}

//示例:从Spring Security中获取当前用户名
/* private String getUserName() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null || !authentication.isAuthenticated()) {
return Optional.empty();
}
String username = authentication.getName();
return username;
}*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @Company: 上海数慧系统技术有限公司
* @Department: 数据中心
* @Author: 郑家骜[ào]
* @Email: [email protected]
* @Date: 2023-09-27 17:27
* @Since:
*/
package com.zja.audit;

import com.zja.util.IdGeneratorUtil;
import org.springframework.data.domain.AuditorAware;
import org.springframework.util.StringUtils;

import java.util.Optional;

/**
* @author: zhengja
* @since: 2023/09/27 17:27
*/
public class AuditorAwareImplB implements AuditorAware<String> {

@Override
public Optional<String> getCurrentAuditor() {
//当前用户id
String currentUserId = IdGeneratorUtil.objectId();

//一般从项目请求token中获取当前登录用户
return StringUtils.hasText(currentUserId) ? Optional.of(currentUserId) : Optional.of("system");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @Company: 上海数慧系统技术有限公司
* @Department: 数据中心
* @Author: 郑家骜[ào]
* @Email: [email protected]
* @Date: 2023-09-27 17:24
* @Since:
*/
package com.zja.audit;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

/**
* @author: zhengja
* @since: 2023/09/27 17:24
*/
@Configuration
//@EnableJpaAuditing
@EnableJpaAuditing(auditorAwareRef = "auditorProviderB") //若注册了多个实现,则通过 auditorAwareRef=“”进行选择
public class JpaAuditingConfig {

@Bean("auditorProviderA")
public AuditorAware<String> auditorProviderA() {
return new AuditorAwareImplA();
}

@Bean("auditorProviderB")
public AuditorAware<String> auditorProviderB() {
return new AuditorAwareImplB();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@
*/
package com.zja.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableJpaRepositories(basePackages = "com.zja.repositorys")
@EnableTransactionManagement
//可选的
//@Configuration
//@EnableJpaRepositories(basePackages = "com.zja.repositorys")
//@EnableTransactionManagement
public class JpaConfig {

}
Loading

0 comments on commit 30bef48

Please sign in to comment.