Spring saveOrUpdate method: saves or updates entity objects
•
Java
saveOrUpdate(Object entity)
Example 1
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); //执行保存方法
}
Example 2
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); //执行更新方法
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
