加入商家密码CRUD操作

This commit is contained in:
myh 2023-11-11 20:18:44 +08:00
parent f8123c6e52
commit 02dddf6699
3 changed files with 51 additions and 5 deletions

View File

@ -0,0 +1,18 @@
package com.example.springdemo.dao;
import com.example.springdemo.entities.password.MerchantsPassword;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
@Repository
public interface MerchantsPasswordRepository
extends JpaRepository<MerchantsPassword, Long> {
@Query("update MerchantsPassword MerchantsPwd set MerchantsPwd.password = ?1 where MerchantsPwd.merchants.id = ?2")
void updatePassword(String password, Long merchantID);
//删除账户时删除密码
@Query("delete from MerchantsPassword MerchantsPwd where MerchantsPwd.merchants.id = ?1")
void deletePasswordById(Long merchantID);
}

View File

@ -1,11 +1,12 @@
package com.example.springdemo.service;
import com.example.springdemo.entities.Merchants;
import com.example.springdemo.entities.password.MerchantsPassword;
import java.util.Optional;
public interface MerchantsService {
Merchants addMerchants(Merchants merchant);
Merchants addMerchants(Merchants merchant, MerchantsPassword merchantsPassword);
void deleteMerchantsById(Long id);
@ -16,4 +17,6 @@ public interface MerchantsService {
Merchants findById(Long id);
Merchants findByName(String name);
void updatePassword(String password, Long id);
}

View File

@ -1,9 +1,13 @@
package com.example.springdemo.service;
import com.example.springdemo.dao.MerchantsPasswordRepository;
import com.example.springdemo.dao.MerchantsRepository;
import com.example.springdemo.entities.Merchants;
import com.example.springdemo.entities.password.MerchantsPassword;
import jakarta.annotation.Resource;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@ -11,18 +15,31 @@ import java.util.List;
public class MerchantsServiceImpl implements MerchantsService {
@Resource
private MerchantsRepository merchantsRepository;
@Resource
private MerchantsPasswordRepository merchantsPasswordRepository;
@Override
public Merchants addMerchants(Merchants merchant) {
return merchantsRepository.save(merchant);
@Modifying
@Transactional(rollbackFor = Exception.class)
//添加商户时添加密码
public Merchants addMerchants(Merchants merchant, MerchantsPassword merchantsPassword) {
Merchants m = merchantsRepository.save(merchant);
merchantsPasswordRepository.save(merchantsPassword);
return m;
}
@Override
public void deleteMerchantsById(Long id) {
merchantsRepository.deleteById(id);
@Modifying
@Transactional(rollbackFor = Exception.class)
//删除商户时删除密码
public void deleteMerchantsById(Long merchantID) {
merchantsRepository.deleteById(merchantID);
merchantsPasswordRepository.deletePasswordById(merchantID);
}
@Override
@Modifying
@Transactional(rollbackFor = Exception.class)
public Merchants updateMerchants(Merchants merchant) {
return merchantsRepository.save(merchant);
}
@ -41,4 +58,12 @@ public class MerchantsServiceImpl implements MerchantsService {
public Merchants findByName(String name) {
return merchantsRepository.findByName(name);
}
@Override
@Modifying
@Transactional(rollbackFor = Exception.class)
//修改密码
public void updatePassword(String password, Long merchantID) {
merchantsPasswordRepository.updatePassword(password, merchantID);
}
}