मैं स्प्रिंग एमवीसी में वेब लिख रहा हूं। मैंने जेनेरिक डीएओ का उपयोग करके सभी डीएओ लिखा। अब मैं अपनी सेवा कक्षाओं को फिर से लिखना चाहता हूं। मैं "सामान्य सेवा" कैसे लिख सकता हूं?स्प्रिंग एमवीसी: जेनेरिक डीएओ और सर्विस क्लासेस
/* ################################# DAO ################################ */
package net.example.com.dao;
import java.util.List;
public interface GenericDao<T> {
public T findById(int id);
public List<T> findAll();
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")
public abstract class GenericHibernateDaoImpl<T extends Serializable> implements GenericDao<T> {
private Class<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();
}
}
/* ------------------------------------------------------ */
package net.example.com.dao;
import 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;
import 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 = :name")
.setString("name", name).uniqueResult();
}
@Override
public Country findByCode(String code) {
return (Country) getCurrentSession()
.createQuery("FROM Country WHERE code = :code")
.setString("code", code).uniqueResult();
}
}
/* ################################# DAO ################################ */
और सेवाएं::
/* ################################# SERVICE ################################ */
package net.example.com.service;
import java.util.List;
public interface GenericManager<T> { // GenericManager<T> is the same as GenericDao<T>
public T findById(int id);
public List<T> findAll();
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;
import net.example.com.dao.GenericDao;
@Service
public abstract class GenericManagerImpl<T> implements GenericManager<T> {
@Autowired
protected GenericDao<T> dao;
@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;
import net.example.com.entity.Country;
public interface CountryManager extends GenericDao<Country> { // CountryManager is the same as 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> implements CountryManager {
@Override
public List<Country> findAll() {
return dao.findAll();
}
public Country findById(int id) {
return dao.findById(id);
}
@Override
public Country findByName(String name) {
return dao.findByName(name); // compiler (and Eclipse) do not see findByName !!!!!!!!!
}
@Override
public Country findByCode(String code) {
return dao.findByCode(code); // compiler (and Eclipse) do not see 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 ################################ */
संकलक (और ग्रहण) findByName
और findByCode
तरीकों नहीं दिख रहा है
मेरी DAOs रहे हैं। मैं समझता हूं क्यों। लेकिन मैं इसे कैसे लिख सकता हूं?
मैं http://pastebin.com/rN1C0i3w और यह अभी भी – martin
:-(ठीक है, मैं कई गलतियों को देख सकते हैं (कुछ मेरे हैं काम नहीं करता है :-)): डीएओ प्रकार को आपके जेनेरिक मैनेजर में एक सामान्य प्रकार के रूप में पारित किया जाना चाहिए (मेरे उत्तर में संपादित कोड देखें) और @ इंजेक्शन एनोटेशन को न भूलें। मैंने अपना जवाब अपडेट किया। – bmeurant
कुछ भी नहीं भूलना सुनिश्चित करें, आपको वास्तव में पहले दो पेस्टों को देखना चाहिए ... – bmeurant