release- JPA定义基类

release-2024-08-08
huangyawei 4 months ago
parent eca7fad7fa
commit ae6c5a1b56

@ -42,18 +42,6 @@
<version>3.7.0</version> <version>3.7.0</version>
</dependency> </dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.4.2.Final</version> <!-- 替换为最新版本 -->
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.4.2.Final</version> <!-- 替换为最新版本 -->
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -203,23 +203,6 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version> <!-- 替换为最新版本 -->
<configuration>
<source>1.8</source> <!-- 替换为你的Java版本 -->
<target>1.8</target> <!-- 替换为你的Java版本 -->
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.4.2.Final</version> <!-- 替换为最新版本 -->
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

@ -60,15 +60,12 @@ public class JPAEntityListener {
jpaBasePojo.setUpdateUser(getCurrentUser().getUserId()); jpaBasePojo.setUpdateUser(getCurrentUser().getUserId());
jpaBasePojo.setUpdateUserName(getCurrentUser().getNickName()); jpaBasePojo.setUpdateUserName(getCurrentUser().getNickName());
jpaBasePojo.setUpdateTime(LocalDateTime.now()); jpaBasePojo.setUpdateTime(LocalDateTime.now());
}
@PreRemove if(jpaBasePojo.getDelFlag() == 2) {
public void setDeletedOn(JPABasePojo jpaBasePojo) { jpaBasePojo.setDelFlag(2);
log.info("Setting deleted on {}", jpaBasePojo); } else {
jpaBasePojo.setUpdateUser(getCurrentUser().getUserId()); jpaBasePojo.setDelFlag(0);
jpaBasePojo.setUpdateUserName(getCurrentUser().getNickName()); }
jpaBasePojo.setUpdateTime(LocalDateTime.now());
jpaBasePojo.setDelFlag(2); // 默认已删除
} }
private UserLoginDto getCurrentUser() { private UserLoginDto getCurrentUser() {

@ -1,11 +0,0 @@
package com.luoo.user.mapstruct;
import com.luoo.user.dto.store.StoreUpdateDto;
import com.luoo.user.pojo.Store;
import org.mapstruct.*;
@Mapper(componentModel = "spring")
public interface StoreMapper {
void updateStoreFromDto(StoreUpdateDto dto, @MappingTarget Store entity);
}

@ -2,14 +2,11 @@ package com.luoo.user.service;
import com.luoo.user.dao.StoreDao; import com.luoo.user.dao.StoreDao;
import com.luoo.user.dto.store.StoreUpdateDto; import com.luoo.user.dto.store.StoreUpdateDto;
import com.luoo.user.mapstruct.StoreMapper;
import com.luoo.user.pojo.Store; import com.luoo.user.pojo.Store;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;
import java.util.List; import java.util.List;
@ -25,11 +22,9 @@ public class StoreService {
private final StoreDao storeDao; private final StoreDao storeDao;
private final StoreMapper storeMapper;
public StoreService(StoreDao storeDao, StoreMapper storeMapper) { public StoreService(StoreDao storeDao) {
this.storeDao = storeDao; this.storeDao = storeDao;
this.storeMapper = storeMapper;
} }
// 增删改查 // 增删改查
@ -49,7 +44,7 @@ public class StoreService {
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void update(StoreUpdateDto storeUpdateDto) { public void update(StoreUpdateDto storeUpdateDto) {
Store oldStore = storeDao.findById(storeUpdateDto.getId()).orElseThrow(() -> new RuntimeException("Store not found")); Store oldStore = storeDao.findById(storeUpdateDto.getId()).orElseThrow(() -> new RuntimeException("Store not found"));
storeMapper.updateStoreFromDto(storeUpdateDto, oldStore); BeanUtils.copyProperties(storeUpdateDto, oldStore);
storeDao.save(oldStore); storeDao.save(oldStore);
} }
public Store getOne(String id) { public Store getOne(String id) {

Loading…
Cancel
Save