SSH电力项目二
Posted panzer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSH电力项目二相关的知识,希望对你有一定的参考价值。
底层方法封装(CommonDaoImpl类)
public class CommonDaoImpl<T> extends HibernateDaoSupport implements ICommonDao<T> { //泛型转化 Class entityClass = TUtils.getActualType(this.getClass()); /** * 如何来实现这个save方法:通过HibernateDaoSupport 来实现,需要注入sessionFactory */ @Resource public void setDi(SessionFactory sessionFactory){ this.setSessionFactory(sessionFactory); } @Override public void save(T entity) { this.getHibernateTemplate().save(entity); } public void update(T entity){ this.getHibernateTemplate().update(entity); } @Override public T findObjectById(Serializable id) { // Class entityClass = TUtils.getActualType(this.getClass()); return (T) this.getHibernateTemplate().get(entityClass, id); //entityClass 此处需要类型 } @Override public void deleteObjectByIds(Serializable... ids) { if(ids != null && ids.length > 0){ for(Serializable id:ids){ Object entity = this.findObjectById(id); this.getHibernateTemplate().delete(entity); } } // this.getHibernateTemplate().delete(entity); } @Override public void deleteObjectByCollection(List<T> list) { this.getHibernateTemplate().deleteAll(list); } }
测试类:
package junit; import java.io.Serializable; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.itheima.elec.bean.ElecText; import com.itheima.elec.dao.IElecTextDao; public class TestDao { @Test public void save(){ //加载spring容器 ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); IElecTextDao elecTextDao = (IElecTextDao) ac.getBean(IElecTextDao.SERVICE_NAME); ElecText elecText = new ElecText(); elecText.setTextName("aaaaaaaaaa"); elecText.setTextDate(new Date()); elecText.setTextRemark("aaaaaaaaaaaaaaa test"); elecTextDao.save(elecText); } @Test public void update(){ ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); IElecTextDao elecTextDao = (IElecTextDao) ac.getBean(IElecTextDao.SERVICE_NAME); ElecText elecText = new ElecText(); elecText.setTextID("8a80809359876a330159876a365b0001"); elecText.setTextName("TestDaoupdate"); elecText.setTextDate(new Date()); elecText.setTextRemark("TestDao test update"); elecTextDao.update(elecText); } @Test public void findObjectById(){ ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); IElecTextDao elecTextDao = (IElecTextDao) ac.getBean(IElecTextDao.SERVICE_NAME); Serializable id = "8a80809359876a330159876a365b0001"; ElecText elecText= elecTextDao.findObjectById(id); System.out.println(elecText.getTextName() + "+" + elecText.getTextRemark()); } @Test public void deleteObjectByIds(){ ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); IElecTextDao elecTextDao = (IElecTextDao) ac.getBean(IElecTextDao.SERVICE_NAME); // Serializable ids = "297e3503598229df0159822ac8000001"; Serializable[] ids = {"297e3503598229df0159822ac8000001","297e35035985f149015985f1ea770001"}; elecTextDao.deleteObjectByIds(ids); } @Test public void deleteObjectByCollection(){ ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); IElecTextDao elecTextDao = (IElecTextDao) ac.getBean(IElecTextDao.SERVICE_NAME); List<ElecText> list = new ArrayList<ElecText>(); ElecText elecText1 = new ElecText(); elecText1.setTextID("297e350359873d030159873d7ab20001"); ElecText elecText2 = new ElecText(); elecText2.setTextID("8a80809359876a330159876a365b0001"); ElecText elecText3 = new ElecText(); elecText3.setTextID("8a8080935987d7c5015987d7c8d40001"); list.add(elecText1); list.add(elecText2); list.add(elecText3); elecTextDao.deleteObjectByCollection(list); } }
以上是关于SSH电力项目二的主要内容,如果未能解决你的问题,请参考以下文章