问题描述
我在Spring MVC中写web。我使用Generic DAO编写了所有DAO。现在我想重写我的Service类。如何写通用服务?有我的DAO:
/ * ################################# DAO ########### ##################### * /
package net.example.com.dao;
import java.util.List;
public interface GenericDao< T> {
public T findById(int id);
public List< T>找到所有();
public void update(T entity);
public void save(T entity);
public void delete(T entity);
}
/ * ----------------------------------- ------------------- * /
package net.example.com.dao;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
@Scope(prototype)
公共抽象类GenericHibernateDaoImpl< T extends Serializable>实现GenericDao< T> {
私人课程< T> clazz中;
@Autowired
private SessionFactory sessionFactory;
public final void setClazz(Class< T> clazzToSet){
this.clazz = clazzToSet;
}
@SuppressWarnings(unchecked)
public T findById(int id){
return(T)getCurrentSession()。get(clazz,id) ;
}
@SuppressWarnings(unchecked)
public List< T> findAll(){
return getCurrentSession()。createQuery(FROM+ clazz.getName())。list();
}
public void update(T entity){
getCurrentSession()。update(entity);
}
public void save(T entity){
getCurrentSession()。save(entity);
}
public void delete(T entity){
getCurrentSession()。delete(entity);
}
protected final session getCurrentSession(){
return sessionFactory.getCurrentSession();
}
}
/ * ------------------------------ ------------------------ * /
包net.example.com.dao;
导入net.example.com.entity.Country;
public interface CountryDao extends GenericDao< Country> {
public Country findByName(String name);
public Country findByCode(String code);
}
/ * ------------------------------- ----------------------- * /
package net.example.com.dao;
import org.springframework.stereotype.Repository;
导入net.example.com.entity.Country;
@Repository
public class CountryDaoImpl extends GenericHibernateDaoImpl< Country> implements CountryDao {
@Override
public Country findByName(String name){
return(Country)getCurrentSession()
.createQuery(FROM Country WHERE name =名称)
.setString(name,name).uniqueResult();
$ b @Override
public Country findByCode(String code){
return(Country)getCurrentSession()
.createQuery(FROM Country WHERE code =:code)
.setString(code,code).uniqueResult();
}
}
/ * ########################## ####### DAO ################################ * /
$ b 和服务:
/ * ################################# SERVICE ################ ################ * /
package net.example.com.service;
import java.util.List;
公共接口GenericManager< T> {// GenericManager< T>与GenericDao< T>相同。
public T findById(int id);
public List< T>找到所有();
public void update(T entity);
public void save(T entity);
public void delete(T entity);
}
/ * ----------------------------------- ------------------- * /
package net.example.com.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
导入net.example.com.dao.GenericDao;
@Service
公共抽象类GenericManagerImpl< T>实现GenericManager< T> {
@Autowired
保护GenericDao< T>道;
@Override
public T findById(int id){
return dao.findById(id);
}
@Override
public List< T> findAll(){
return dao.findAll();
}
@Override
public void update(T entity){
dao.update(entity);
}
@Override
public void save(T entity){
dao.save(entity);
}
@Override
public void delete(T entity){
dao.delete(entity);
}
}
/ * ---------------------------------- -------------------- * /
package net.example.com.dao;
导入net.example.com.entity.Country;
公共接口CountryManager扩展了GenericDao< Country> {// CountryManager与CountryDao相同
public Country findByName(String name);
public Country findByCode(String code);
}
/ * ----------------------------------- ------------------- * /
package net.example.com.service;
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import net.example.com.dao.CountryDao;
import net.example.com.entity.Country;
@Service
@Transactional
public class CountryManagerImpl extends GenericManagerImpl< Country>实现CountryManager {
@覆盖
公共列表< Country> findAll(){
return dao.findAll();
}
public国家findById(int id){
返回dao.findById(id);
}
@Override
public Country findByName(String name){
return dao.findByName(name); //编译器(和Eclipse)没有看到findByName !!!!!!!!!
}
@Override
public Country findByCode(String code){
return dao.findByCode(code); //编译器(和Eclipse)没有看到findByCode !!!!!!!!!
}
@Override
public void save(Country country){
dao.save(country);
}
@Override
public void delete(Country country){
dao.delete(country);
}
@Override
public void update(Country country){
dao.update(country);
}
}
/ * -------------------------- ---------------------------- * /
/ * ########## ####################### SERVICE ##########################编译器(和Eclipse)没有看到
###### * /
> findByName
和 findByCode
方法。我明白为什么。但是我该如何重写呢? 解决方案