package com.learn.service; import com.learn.entity.Student; import com.learn.service.jpa.StudentJpaService; import com.learn.service.mp.StudentMpService; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Page; import org.springframework.stereotype.Service; import java.util.List; import java.util.Optional; @Service public class StudentServiceSelector { private final StudentJpaService jpa; private final StudentMpService mp; @Value("${orm.mode:jpa}") private String mode; public StudentServiceSelector(StudentJpaService jpa, StudentMpService mp) { this.jpa = jpa; this.mp = mp; } private boolean useJpa() { return !"mp".equalsIgnoreCase(mode); } public Student add(Student s) { return useJpa() ? jpa.add(s) : mp.add(s); } public List list() { return useJpa() ? jpa.list() : mp.list(); } public Page listPage(int page, int size) { return useJpa() ? jpa.listPage(page, size) : mp.listPage(page, size); } public Student getById(Long id) { return useJpa() ? jpa.getById(id) : mp.getById(id); } public List search(String kw) { return useJpa() ? jpa.search(kw) : mp.search(kw); } public Optional update(Long id, Student s) { return useJpa() ? jpa.update(id, s) : mp.update(id, s); } public boolean delete(Long id) { return useJpa() ? jpa.delete(id) : mp.delete(id); } public long count() { return useJpa() ? jpa.count() : mp.count(); } public long countExcellent(int min) { return useJpa() ? jpa.countExcellent(min) : mp.countExcellent(min); } public String getActiveMode() { return useJpa() ? "JPA" : "MyBatis-Plus"; } }