Skip to content

Instantly share code, notes, and snippets.

public void processWithSharedTransaction() {
// 创建并绑定 EntityManager 到 ThreadLocal
EntityManager entityManager = entityManagerFactory.createEntityManager();
TransactionSynchronizationManager.bindResource(entityManagerFactory,
new EntityManagerHolder(entityManager));
// 启动事务
TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
try {
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
### 需求分析
1. 功能模块划分
- **码头管理**:涉及码头信息维护(名称、位置等)以及基础信息配置(提前购票天数、载具上船支持、预计航行时长) 、码头信息配置(出发和到达码头设置)。
- **航班配置**:涵盖开航时间按周配置(一天可多个开航时间)、价格信息配置(船票规格及最大容量、载具票价)。船票根据提前购票时间和开航时间自动生成,支持临时加开和停航。
- **船次管理**:包含船次列表查询(根据航班配置生成可售卖船次)、船次详情查看(含购票乘客信息)、临时加开与停航操作(停航时已购票用户自动退款并短信通知)。
- **订单管理**:有船票订单信息查看(购票船次、购票人、数量等)、使用核销(后台手动操作)。
- **基础设置**:主要是船票购票须知以富文本形式填写。
- **用户小程序功能**:首页可查询船次、购买船票(选船次、规格、数量,填乘客信息,需读购买须知);“我的” 页面能查看订单、发起退款、展示核销码。
- **商家小程序功能**:工作台涵盖码头管理、航班配置、船次管理、船票订单等与商家运营端类似功能,方便商家移动端操作。
public class NullToEmptyUtils {
public static void convertNullFieldsToEmpty(Object target) {
if (target == null) return;
Field[] fields = target.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
try {
Object value = field.get(target);
if (value == null && field.getType() == String.class) {
```java
@Aspect
@Component
@Slf4j
public class NullToEmptyAspect {
@Before("execution(* org.springframework.data.repository.Repository+.*find*(..))")
public void beforeRepositoryQuery(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
for (Object arg : args) {
@8629303
8629303 / gist:4407ac87b3cf13c336791431eddd95f5
Last active May 14, 2025 03:54
JPA 在保存或者更新的时候,遇到null需要转换成空字符串
在 JPA 使用过程中,如果你希望 在保存或更新实体时自动将 null 转换为 ""(空字符串),可以通过以下几种方式实现:
✅ 推荐方式一:使用 @PrePersist 和 @PreUpdate 生命周期回调
这是最推荐的做法,可以在实体类内部统一处理字段。
示例: