日期:2014-05-20 浏览次数:20778 次
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> </bean> <bean id="postdaohib" class="daoimpl.PostsDaoHibImple" > <property name="sessionFactory" ref="sessionFactory"></property> </bean>
public class PostsDaoHibImple extends HibernateDaoSupport implements PostsDao { public void del(int id) { HibernateTemplate ht=super.getHibernateTemplate(); Posts p=ht.get(Posts.class, id); try { ht.delete(p); System.out.println("删除"); ht.flush(); } catch(Exception e){ System.out.println("删除出错"); } } public static void main(String[] args) { ApplicationContext c = new ClassPathXmlApplicationContext("applicationContext.xml"); PostsDaoHibImple p=(PostsDaoHibImple) c.getBean("postdaohib"); p.del(35); } }