saveOrUpdate(Object entity)
引數說明:public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext("cfg/XMLConfig.xml"); //載入組態檔 Dao dao = (Dao)context.getBean("dao"); //建立Dao TbUser user = new TbUser(); user.setName("儲存的使用者"); user.setSex("男"); user.setAge(20); HibernateTemplate htm = dao.getHibernateTemplate(); htm.saveOrUpdate(user); //執行儲存方法 }
public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext("cfg/XMLConfig.xml"); //載入組態檔 Dao dao = (Dao)context.getBean("dao"); //建立Dao HibernateTemplate htm = dao.getHibernateTemplate(); TbUser user = (TbUser)htm.get(TbUser.class,new Integer(1)); if(user==null) return; user.setName("更新的使用者"); htm.saveOrUpdate(user); //執行更新方法 }