首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Hibernate事例(添加,修改,删除,获得结果集)

2012-08-21 
Hibernate例子(添加,修改,删除,获得结果集)关键词: Hibernate,添加,修改,删除,结果集1.添加public int ins

Hibernate例子(添加,修改,删除,获得结果集)
关键词: Hibernate,添加,修改,删除,结果集

1.添加

public int insertNewOneUser(UserVo user) {Session session = sessionFactory.openSession();Transaction tx = null;try {tx = session.beginTransaction();session.save(user);tx.commit();} catch (Exception e) {tx.rollback();e.printStackTrace();} finally {session.close();}return 0;}


2.修改
public int updateUser(UserVo user) {Session session = sessionFactory.openSession();Transaction tx = null;try {tx = session.beginTransaction();session.update(user);tx.commit();} catch (Exception e) {tx.rollback();e.printStackTrace();} finally {session.close();}return 0;}


3.删除
public int deleteUser(UserVo user) {Session session = sessionFactory.openSession();Transaction tx = null;try {tx = session.beginTransaction();session.delete(user);tx.commit();} catch (Exception e) {tx.rollback();e.printStackTrace();} finally {session.close();}return 0;}


4.获得结果集
public UserVo selectUserFromId(int id) {Session session = sessionFactory.openSession();Transaction tx = null;try {tx = session.beginTransaction();Query query = session.createQuery("from UserVo where id = :id");query.setInteger("id", id);return (UserVo) query.list().get(0);} catch (Exception e) {tx.rollback();e.printStackTrace();} finally {session.close();}return null;}


5.Hibernate配置
<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-mapping PUBLIC          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="com.zakisoft.hib.UserVo" table="user2"><id name="id" column="ID" type="int"><generator column="NAME" type="string" not-null="true"/><property name="password" column="PASSWORD" type="string" not-null="true"/></class></hibernate-mapping>


6.源码和例子下载
http://www.zakisoft.com/?p=95

热点排行