Week 1-8: Spring Boot 学习计划完整项目
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
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<Student> list() { return useJpa() ? jpa.list() : mp.list(); }
|
||||
|
||||
public Page<Student> 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<Student> search(String kw) { return useJpa() ? jpa.search(kw) : mp.search(kw); }
|
||||
|
||||
public Optional<Student> 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"; }
|
||||
}
|
||||
Reference in New Issue
Block a user