SSH实例(3)
src文件夹的结构如下:
clasDao文件:
package com.school.service; import java.util.List; import com.school.dao.ClasDAO; import com.school.entity.Clas; public class ClasServiceImpl implements ClasService{ private ClasDAO clasDAO; public void setClasDAO(ClasDAO ClasDAO){ this.clasDAO=ClasDAO; } public void saveClas(Clas Clas) { if(clasDAO.findById(Clas.getId())==null) clasDAO.save(Clas); } public Clas getClas(String name) { return clasDAO.getClas(name); } public void deleteClas(int id) { if(clasDAO.findById(id)!=null) clasDAO.delete(id); } public void updateClas(Clas clas) { if(clasDAO.findById(clas.getId())!=null) clasDAO.update(clas); } public Clas findClasById(int id) { return clasDAO.findById(id); } public List<Clas> findAll() { return clasDAO.findAll(); } }
ClasDAOImpl文件:
package com.school.dao; import java.util.List; import com.school.dao.ClasDAO; import com.school.entity.Clas; import org.hibernate.Query; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; public class ClasDAOImpl extends HibernateDaoSupport implements ClasDAO{ public void save(Clas user) { this.getHibernateTemplate().save(user); } public Clas getClas(String name) { String hsql="from User u where u.name='"+name+"'"; Clas result=(Clas)((Query) this.getHibernateTemplate().find(hsql)).uniqueResult(); return result; } public void delete(int id) { this.getHibernateTemplate().delete(findById(id)); } public void update(Clas user){ this.getHibernateTemplate().update(user); } public Clas findById(int id) { Clas user = (Clas) this.getHibernateTemplate().get(Clas.class, id); return user; } @SuppressWarnings("unchecked") public List<Clas> findAll() { String queryString = "from Clas"; List<Clas> list =this.getHibernateTemplate().find(queryString); return list; } }
clasService文件:
package com.school.service; import java.util.List; import com.school.entity.Clas; public interface ClasService { void saveClas(Clas Clas); Clas getClas(String name); void deleteClas(int id); void updateClas(Clas Clas); Clas findClasById(int id); List<Clas> findAll(); }
ClasServiceImpl文件:
package com.school.service; import java.util.List; import com.school.dao.ClasDAO; import com.school.entity.Clas; public class ClasServiceImpl implements ClasService{ private ClasDAO clasDAO; public void setClasDAO(ClasDAO ClasDAO){ this.clasDAO=ClasDAO; } public void saveClas(Clas Clas) { if(clasDAO.findById(Clas.getId())==null) clasDAO.save(Clas); } public Clas getClas(String name) { return clasDAO.getClas(name); } public void deleteClas(int id) { if(clasDAO.findById(id)!=null) clasDAO.delete(id); } public void updateClas(Clas clas) { if(clasDAO.findById(clas.getId())!=null) clasDAO.update(clas); } public Clas findClasById(int id) { return clasDAO.findById(id); } public List<Clas> findAll() { return clasDAO.findAll(); } }